Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement Nibbler#to_hash

  • Loading branch information...
commit f98f0171e4cb4eef7577c9f195083f90015bdcac 1 parent e60f93e
@mislav authored
Showing with 19 additions and 0 deletions.
  1. +19 −0 lib/nibbler.rb
View
19 lib/nibbler.rb
@@ -42,6 +42,15 @@ def parse
self
end
+ def to_hash
+ converter = lambda { |obj| obj.respond_to?(:to_hash) ? obj.to_hash : obj }
+ self.class.rules.keys.inject({}) { |hash, name|
+ value = send(name)
+ hash[name.to_sym] = Array === value ? value.map(&converter) : converter[value]
+ hash
+ }
+ end
+
protected
# `delegate` is optional, but should respond to `call` or `parse`
@@ -180,6 +189,16 @@ def search(selector)
@blog.navigation_items.should == %w[Home About Help]
end
+ it "should convert to hash" do
+ hash = @blog.to_hash
+ hash[:navigation_items].should == %w[Home About Help]
+ hash[:title].should == "Maximum awesome"
+ article = hash[:articles].first
+ article[:title] == "First article"
+ article.key?(:link).should be_true
+ article[:link].should be_nil
+ end
+
it "should have title, pubdate for first article" do
article = @blog.articles[0]
article.title.should == 'First article'
Please sign in to comment.
Something went wrong with that request. Please try again.