If you happen to work with Service Oriented Architectures. There might be many instances where you will end up creating multiple standalone applications. If you are in the Ruby ecosystem you will find that there is no standard structure your application. Like, for instance if you go about creating a standalone Java applications, the structure does appear like:
Most articles that you would come across suggest that, you could give your application some structure with a structure similar to gem. No better than the bundler gem to create your application. We followed the same approach but just added a some more change to the structure. If you are using RVM the first step would be to select the Ruby version and the appropriate gemset.
Next, we will need to install the
bundler gem. To check the if
Now we are ready to create our project. This will create your project directory. Now Let’s take a look at the project directory structure that got generated.
Since we want to leverage the niceties of ActiveRecord in the project and add its as a dependency. We will use
mysql as our backend. Under the project directory finds the
standalone_app.gemspec file add the following dependencies.
It’s time to load the required files in the classpath lets require the gem files. We will require the files at two places. One in the
Rakefile where the ActiveRecord rake tasks can run independently. Second in the
standalone_app/lib/standalone_app.rb. In the
Rakefile we will make the following changes:
You will need to add the following code in the
rake file to use the
rake db:... tasks in the project.
The code above will include ActiveRecord configurations first setting the ENV for the application, followed by configuring the path of the database configuration file, then pointing the migrations directory
standalone_app/db/migrations directory and seeds
standalone_app/db/seeds.rb, set the fixtures path, Rest of it is including the
active_record rake tasks.
Next, we will get ActiveRecord connections in the application too.
Time to configure the logger. We will create a file called
Structure of your project:
Since we will add models to application, we will add the
standalone_app/db, add the
standalone_app/db/migrate directory to add the migrations. We will also, add
standalone_app/db/seeds.rb for the seed data. We will also, add a directory like,
standalone_app/app/models to add your models. For the logger, we will create
standalone_app/log and then add the
Your business logic can reside in the lib/standalone_app/<files…>
When you are developing a production level application and with active_record. There are a couple nice to haves. One is access to the console like you get in rails.
Let’s create a
The first file we create is
setup. Here are the contents of the file.
To get the console working, we will first add a
standalone_app/bin directory to the project. Then inside it we will go about creating a file
To access the console
you will just:
When we want to run the application we can start and daemonize the application. Let’s go ahead and create a file:
That’s it we are all set. You could find the source code of the project at: Github
Follow me on Twitter