Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Move Over Rails. Here Comes Merb. : Page 2

Merb is potentially much faster than Rails, and the combination of Merb and Ruby can provide the performance most web apps require. Find out how Merb can deliver a 2-4X speed increase over Rails and why that makes it better for certain applications.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

Your First Merb Web Application: Filtered Ruby News
The demo Merb app you will develop is for Ruby feeds, but you can repurpose it to use feeds for any topic that interests you. You will start by creating a data model for storing RSS feeds. When you installed the Merb gem (which also installed the "default Merb stack"), it placed several command-line utilities on your PATH. You use one of these utilities (merb-gen) to generate your Merb application (see Listing 1).

By default, merb-gen app generates an application using the DataMapper object relational mapper (ORM) framework, which is similar to ActiveRecord. I am a huge fan of ActiveRecord, but DataMapper has a more abstract interface for backend data sources (databases, RSS/Atom, flat file, etc.) and it's thread safe (more on DataMapper later).

Normally, the first thing that you need to do after generating a new Merb application is to edit the config/init.rb file. In this case, you will use the defaults set by the application generator as is, but going through the generated file (see Listing 2) is still worthwhile. I have added comments to explain what is in this file (you will not see my comments when you run the application generator and edit the conf/init.rb file).

You need only two model classes, FeedSource and Feed, and I initially generated a model for this application with a string attribute for a blog post URL:

merb-gen resource feed_source url:string merb-gen resource feed url:string

Later, you will refactor this application to keep instances of the Feed class in memory. I eventually made the following manual edits to the FeedSource class to add the additional class attribute feed_type:

class FeedSource include DataMapper::Resource property :id, Serial property :url, String property :feed_type, String # 'rss' or 'atom' end

I also made these manual edits to the Feed class to add the additional attributes title and posted_date:

class Feed include DataMapper::Resource property :id, Serial property :url, String property :title, String property :posted_date, Date end

By default, Merb defines a user model class for you when you generate a new resource. After editing the model file(s), in development mode you can wipe out any existing database and recreate the development database using:

rake db:automigrate

Running this rake task created the two sqlite3 database tables for the FeedSource and Feed model classes.

While working on this application, I kept editing the app/models/feed.rb file. Here is the app/models/feed.rb file for the first version of the demo web application (remember to run rake db:automigrate after you change the model files during development):

class Feed include DataMapper::Resource property :id, Serial property :url, String property :title, String property :content, String property :feed_name, String property :posted_date, Date end

The feed_sources table contains seed RSS feed URLs for spidering. I used the following commands to add a row to this table for development and testing:

mark$ sqlite3 dev.db sqlite> insert into feed_sources ('url', 'feed_type') values ('http://markwatson.com/blog/atom.xml', 'atom');

You are going to start Merb in the interactive (irb like) mode:

$mark merb -i irb(main):001:0> FeedSource.find_by_sql("select * from feed_sources").collect {|f| f.url} ~ select * from feed_sources => ["http://markwatson.com/blog/atom.xml"] irb(main):002:0> FeedSource.all ~ SELECT "id", "url" FROM "feed_sources" ORDER BY "id" => [#] irb(main):003:0> FeedSource.all.collect {|f| f.url} ~ SELECT "id", "url" FROM "feed_sources" ORDER BY "id" => ["http://markwatson.com/blog/atom.xml"] irb(main):004:0>

Note that DataMapper is printing the generated SQL.

Removing Unused Code from the Application
Usually when you use merg-gen resource ..., you will use the generated model file, the controller, and the view files. However, this demo application is a single-page web site that does not need an admin interface, so there is quite a lot of clean up that you can do. Figure 2 shows the application with the unused files, and Figure 3 shows the application after I removed these files.

Figure 2. The Application with the Unused Files: There is quite a lot of clean up that you can do on the demo application.
Figure 3. The Application After I Removed the Unused Files: You will notice the demo application has many fewer unused files than new Merb applications that you generate.

Because Merb is itself a small and modular code base, I am motivated to clean up all the unused bits of any application built on Merb. When you download the demo web application for this article, you will notice it has many fewer unused files than new Merb applications that you generate.

The next section walks through the initial implementation of a prototype for the demo application.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date