Skip to content

Commit

Permalink
#12 | Added 'where' method to base
Browse files Browse the repository at this point in the history
  • Loading branch information
kalarani committed Jun 30, 2012
1 parent 3b31f90 commit 85f9574
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/scrapify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
require 'net/http'
require 'scrapify/base'
require 'scrapify/scraper'
require 'scrapify/exceptions'
require 'json'
require 'jsonify'
9 changes: 9 additions & 0 deletions lib/scrapify/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def define_finders
attributes = Hash[attribute_names.map {|attribute| [attribute, send("#{attribute}_values")[index]]}]
self.new(attributes)
end

define_singleton_method :where do |conditions = {}|
raise Scrapify::AttributeDoesNotExist.new(conditions.keys - attribute_names) unless conditions.keys.all?{|key| attribute_names.include?(key) }
indices = conditions.collect do |attribute, value|
send("#{attribute}_values").each_with_index.find_all{|attr_val, index| attr_val == value}.collect(&:last)
end
common_indices = indices.reduce {|a, b| a & b}
common_indices.collect{|index| find_by_index(index)}
end
end

def define_count(key_attribute)
Expand Down
3 changes: 3 additions & 0 deletions lib/scrapify/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Scrapify
class AttributeDoesNotExist < StandardError; end
end
10 changes: 10 additions & 0 deletions spec/shared/finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
shared_examples_for '#finder' do |klass_or_object, conditions|
it 'should fetch objects based on conditions' do
pizza = klass_or_object.where(conditions).first
pizza.name.should == 'chicken golden delight'
pizza.image_url.should == 'golden.jpg'
pizza.price.should == '4.56'
pizza.ingredients.should be_empty
pizza.ingredient_urls.should == ['chicken.html', 'delight.html']
end
end
16 changes: 16 additions & 0 deletions spec/shared/scrapify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,20 @@
].to_json
end
end

it_behaves_like '#finder', klass_or_object, :name => 'chicken golden delight'
it_behaves_like '#finder', klass_or_object, :image_url => 'golden.jpg'
it_behaves_like '#finder', klass_or_object, :price => '4.56'
it_behaves_like '#finder', klass_or_object, :ingredient_urls => ['chicken.html', 'delight.html']
it_behaves_like '#finder', klass_or_object, :name => 'chicken golden delight', :image_url => 'golden.jpg', :price => '4.56', :ingredient_urls => ['chicken.html', 'delight.html']

it 'should return empty array if object is not found' do
klass_or_object.where(:name => 'does not exist').should be_empty
end

it 'should throw exception if attribute is not defined' do
lambda {
klass_or_object.where(:some_attribute => 'chicken golden delight')
}.should raise_error(Scrapify::AttributeDoesNotExist)
end
end

0 comments on commit 85f9574

Please sign in to comment.