Skip to content

Commit 5afd98e

Browse files
committed
added support for #xml_template, fixed a bug in .retrieve()
1 parent 5e59584 commit 5afd98e

File tree

4 files changed

+164
-3
lines changed

4 files changed

+164
-3
lines changed

container_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2+
require "nokogiri"
3+
require "om"
4+
5+
describe "OM::XML::Container" do
6+
7+
before(:all) do
8+
class ContainerTest
9+
include OM::XML::Container
10+
end
11+
end
12+
13+
before(:each) do
14+
@container = ContainerTest.from_xml("<foo><bar>1</bar></foo>")
15+
end
16+
17+
it "should add .ng_xml accessor" do
18+
@container.should respond_to(:ng_xml)
19+
@container.should respond_to(:ng_xml=)
20+
end
21+
22+
describe "new" do
23+
it "should populate ng_xml with an instance of Nokogiri::XML::Document" do
24+
@container.ng_xml.class.should == Nokogiri::XML::Document
25+
end
26+
end
27+
28+
describe '#xml_template' do
29+
it "should return an empty xml document" do
30+
ContainerTest.xml_template.to_xml.should == "<?xml version=\"1.0\"?>\n"
31+
end
32+
end
33+
34+
describe "#from_xml" do
35+
it "should accept a String, parse it and store it in .ng_xml" do
36+
Nokogiri::XML::Document.expects(:parse).returns("parsed xml")
37+
container1 = ContainerTest.from_xml("<foo><bar>1</bar></foo>")
38+
container1.ng_xml.should == "parsed xml"
39+
end
40+
it "should accept a File, parse it and store it in .ng_xml" do
41+
file = fixture(File.join("mods_articles", "hydrangea_article1.xml"))
42+
Nokogiri::XML::Document.expects(:parse).returns("parsed xml")
43+
container1 = ContainerTest.from_xml(file)
44+
container1.ng_xml.should == "parsed xml"
45+
end
46+
it "should accept Nokogiri nodes as input and leave them as-is" do
47+
parsed_xml = Nokogiri::XML::Document.parse("<foo><bar>1</bar></foo>")
48+
container1 = ContainerTest.from_xml(parsed_xml)
49+
container1.ng_xml.should == parsed_xml
50+
end
51+
it "should initialize from #xml_template if no xml is provided" do
52+
ContainerTest.expects(:xml_template).returns("fake template")
53+
ContainerTest.from_xml.ng_xml.should == "fake template"
54+
end
55+
end
56+
57+
describe ".to_xml" do
58+
it "should call .ng_xml.to_xml" do
59+
@container.ng_xml.expects(:to_xml).returns("ng xml")
60+
@container.to_xml.should == "ng xml"
61+
end
62+
63+
it 'should accept an optional Nokogiri::XML Document as an argument and insert its fields into that (mocked test)' do
64+
doc = Nokogiri::XML::Document.parse("<test_xml/>")
65+
mock_new_node = mock("new node")
66+
doc.root.expects(:add_child).with(@container.ng_xml.root).returns(mock_new_node)
67+
result = @container.to_xml(doc)
68+
end
69+
70+
it 'should accept an optional Nokogiri::XML Document as an argument and insert its fields into that (functional test)' do
71+
doc = Nokogiri::XML::Document.parse("<test_xml/>")
72+
@container.to_xml(doc).should == "<?xml version=\"1.0\"?>\n<test_xml>\n <foo>\n <bar>1</bar>\n </foo>\n</test_xml>\n"
73+
end
74+
75+
it 'should add to root of Nokogiri::XML::Documents, but add directly to the elements if a Nokogiri::XML::Node is passed in' do
76+
mock_new_node = mock("new node")
77+
mock_new_node.stubs(:to_xml).returns("foo")
78+
79+
doc = Nokogiri::XML::Document.parse("<test_document/>")
80+
el = Nokogiri::XML::Node.new("test_element", Nokogiri::XML::Document.new)
81+
doc.root.expects(:add_child).with(@container.ng_xml.root).returns(mock_new_node)
82+
el.expects(:add_child).with(@container.ng_xml.root).returns(mock_new_node)
83+
@container.to_xml(doc).should
84+
@container.to_xml(el)
85+
end
86+
end
87+
88+
end

lib/om/xml/accessors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def retrieve(*pointers)
200200
if xpath.nil?
201201
return nil
202202
else
203-
return ng_xml.xpath(xpath, "oxns"=>"http://www.loc.gov/mods/v3")
203+
return ng_xml.xpath(xpath, ox_namespaces)
204204
end
205205
end
206206

lib/om/xml/container.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ module ClassMethods
99
# @xml String, File or Nokogiri::XML::Node
1010
# @tmpl ActiveFedora::MetadataDatastream
1111
# Careful! If you call this from a constructor, be sure to provide something 'ie. self' as the @tmpl. Otherwise, you will get an infinite loop!
12-
def from_xml(xml, tmpl=self.new) # :nodoc:
13-
if xml.kind_of? Nokogiri::XML::Node
12+
def from_xml(xml=nil, tmpl=self.new) # :nodoc:
13+
if xml.nil?
14+
tmpl.ng_xml = self.xml_template
15+
elsif xml.kind_of? Nokogiri::XML::Node
1416
tmpl.ng_xml = xml
1517
else
1618
tmpl.ng_xml = Nokogiri::XML::Document.parse(xml)
1719
end
1820
return tmpl
1921
end
2022

23+
def xml_template
24+
Nokogiri::XML::Document.parse("")
25+
end
26+
2127
end
2228

2329
# Instance Methods -- These methods will be available on instances of classes that include this module
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2+
require "nokogiri"
3+
require "om"
4+
5+
describe "OM::XML::Accessors" do
6+
7+
before(:all) do
8+
class RightsMDTest
9+
10+
include OM::XML
11+
12+
root_property :rightsMetadata, "rightsMetadata", "http://hydra-collab.stanford.edu/schemas/rightsMetadata/v1", :schema=>"http://github.com/projecthydra/schemas/tree/v1/rightsMetadata.xsd"
13+
14+
property :access, :path=>"access",
15+
:subelements=>[:machine],
16+
:convenience_methods => {
17+
:human_readable => {:path=>"human"}
18+
}
19+
20+
property :edit_access, :variant_of=>:access, :attributes=>{:type=>"edit"}
21+
22+
property :machine, :path=>"machine",
23+
:subelements=>["group","person"]
24+
25+
generate_accessors_from_properties
26+
# Generates an empty Mods Article (used when you call ModsArticle.new without passing in existing xml)
27+
def self.xml_template
28+
builder = Nokogiri::XML::Builder.new do |xml|
29+
xml.rightsMetadata(:version=>"0.1", "xmlns"=>"http://hydra-collab.stanford.edu/schemas/rightsMetadata/v1") {
30+
xml.copyright {
31+
xml.human
32+
}
33+
xml.access(:type=>"discover") {
34+
xml.human
35+
xml.machine
36+
}
37+
xml.access(:type=>"read") {
38+
xml.human
39+
xml.machine
40+
}
41+
xml.access(:type=>"edit") {
42+
xml.human
43+
xml.machine
44+
}
45+
}
46+
end
47+
return builder.doc
48+
end
49+
end
50+
end
51+
52+
before(:each) do
53+
@sample = RightsMDTest.from_xml(nil)
54+
end
55+
56+
describe "update_properties" do
57+
it "should update the declared properties" do
58+
pending "nesting is too deep..."
59+
@sample.retrieve(*[:edit_access, :machine, :person]).length.should == 0
60+
@sample.update_properties([:edit_access, :machine, :person]=>"user id").should == {"edit_access_machine_person"=>{"-1"=>"user id"}}
61+
debugger
62+
@sample.retrieve(*[:edit_access, :machine, :person]).length.should == 1
63+
@sample.retrieve(*[:edit_access, :machine, :person]).first.text.should == "user id"
64+
end
65+
end
66+
67+
end

0 commit comments

Comments
 (0)