Skip to content

Instantly share code, notes, and snippets.

@robotarmy
Created June 28, 2010 18:16
Show Gist options
  • Save robotarmy/6d54448d70b07a126c51 to your computer and use it in GitHub Desktop.
Save robotarmy/6d54448d70b07a126c51 to your computer and use it in GitHub Desktop.
ModelLoader
"string"
 toad should call create_by_node and attributes_from_node (FAILED - 1)
"nokogiri_xml_element"
 toad should work with a node too
1) ModelLoader toad should call create_by_node and attributes_from_node
Failure/Error: @foo.should_receive(:toad_nokogiri_xml_document)
(<Foo (class)>).toad_nokogiri_xml_document(any args)
 expected: 1 time
 received: 0 times
 # ./spec/lib/model_loader_spec.rb:21
 # /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'
Finished in 0.0373 seconds
2 examples, 1 failures
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
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