-
-
Save robotarmy/6d54448d70b07a126c51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ModelLoader | |
"string" | |
[31m toad should call create_by_node and attributes_from_node (FAILED - 1)[0m | |
"nokogiri_xml_element" | |
[32m toad should work with a node too[0m | |
1) ModelLoader toad should call create_by_node and attributes_from_node | |
Failure/Error: @foo.should_receive(:toad_nokogiri_xml_document) | |
[31m(<Foo (class)>).toad_nokogiri_xml_document(any args)[0m | |
[31m expected: 1 time[0m | |
[31m received: 0 times[0m | |
[90m # ./spec/lib/model_loader_spec.rb:21[0m | |
[90m # /Users/o_o/.rvm/gems/ree-1.8.7-2010.01/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `inject'[0m | |
Finished in 0.0373 seconds | |
[31m2 examples, 1 failures[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ModelLoader | |
def toad(object,&block) | |
klass = object.class.name.gsub('::','_').downcase | |
p klass | |
send("toad_#{klass}",object,&block) | |
end | |
def toad_string(str,&block) | |
toad(Nokogiri::XML(str),&block) | |
end | |
def this_model | |
name.downcase | |
end | |
def toad_nokogiri_xml_element(node,&block) | |
toad(node.document,&block) | |
end | |
def create_by_node(node) | |
attr = attributes_from_node(node) | |
o = Object.const_get(this_model.capitalize).find_or_create_by_fifa_id(attr) | |
yield o, node if block_given? | |
o | |
end | |
def toad_nokogiri_xml_document(doc,&block) | |
objects = [] | |
doc.css(this_model).each do |object| | |
objects << create_by_node(object,&block) | |
end | |
if objects.size > 1 | |
objects | |
else | |
objects.first | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
class Foo | |
class << self | |
include ModelLoader | |
def attributes_from_node | |
{} | |
end | |
def find_or_create_by_fifa_id | |
end | |
end | |
end | |
describe ModelLoader do | |
before(:all) do | |
@foo = Foo | |
end | |
it "toad should call create_by_node and attributes_from_node" do | |
@foo.should_receive(:toad_string) | |
@foo.should_receive(:toad_nokogiri_xml_document) | |
@foo.should_receive(:create_by_node) | |
@foo.should_receive(:attributes_from_node) | |
@foo.should_receive(:find_or_create_by_fifa_id) | |
@foo.toad("<foos><foo></foo></foos>") | |
end | |
it "toad should work with a node too" do | |
@foo.should_receive(:toad_nokogiri_xml_element) | |
doc = Nokogiri::XML("<foos><foo></foo></foos>") | |
doc.search('foo').each do |n| | |
@foo.toad(n) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment