Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
implement Nibbler#to_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 29, 2010
1 parent e60f93e commit f98f017
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/nibbler.rb
Expand Up @@ -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`
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit f98f017

Please sign in to comment.