om 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/container_spec.rb +88 -0
- data/lib/om/xml/accessors.rb +1 -1
- data/lib/om/xml/container.rb +8 -2
- data/om.gemspec +5 -2
- data/spec/integration/rights_metadata_integration_example_spec.rb +67 -0
- data/spec/unit/accessors_spec.rb +1 -0
- metadata +6 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.8
|
data/container_spec.rb
ADDED
@@ -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
|
data/lib/om/xml/accessors.rb
CHANGED
data/lib/om/xml/container.rb
CHANGED
@@ -9,8 +9,10 @@ module OM::XML::Container
|
|
9
9
|
# @xml String, File or Nokogiri::XML::Node
|
10
10
|
# @tmpl ActiveFedora::MetadataDatastream
|
11
11
|
# 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.
|
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
|
14
16
|
tmpl.ng_xml = xml
|
15
17
|
else
|
16
18
|
tmpl.ng_xml = Nokogiri::XML::Document.parse(xml)
|
@@ -18,6 +20,10 @@ module OM::XML::Container
|
|
18
20
|
return tmpl
|
19
21
|
end
|
20
22
|
|
23
|
+
def xml_template
|
24
|
+
Nokogiri::XML::Document.parse("")
|
25
|
+
end
|
26
|
+
|
21
27
|
end
|
22
28
|
|
23
29
|
# Instance Methods -- These methods will be available on instances of classes that include this module
|
data/om.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{om}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Matt Zumwalt"]
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
"README.rdoc",
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
|
+
"container_spec.rb",
|
27
28
|
"lib/om.rb",
|
28
29
|
"lib/om/xml.rb",
|
29
30
|
"lib/om/xml/accessors.rb",
|
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
|
|
38
39
|
"spec/fixtures/mods-3-2.xsd",
|
39
40
|
"spec/fixtures/mods_articles/hydrangea_article1.xml",
|
40
41
|
"spec/fixtures/test_dummy_mods.xml",
|
42
|
+
"spec/integration/rights_metadata_integration_example_spec.rb",
|
41
43
|
"spec/spec.opts",
|
42
44
|
"spec/spec_helper.rb",
|
43
45
|
"spec/unit/accessors_spec.rb",
|
@@ -55,7 +57,8 @@ Gem::Specification.new do |s|
|
|
55
57
|
s.rubygems_version = %q{1.3.7}
|
56
58
|
s.summary = %q{OM (Opinionated Metadata): A library to help you tame sprawling XML schemas like MODS.}
|
57
59
|
s.test_files = [
|
58
|
-
"spec/
|
60
|
+
"spec/integration/rights_metadata_integration_example_spec.rb",
|
61
|
+
"spec/spec_helper.rb",
|
59
62
|
"spec/unit/accessors_spec.rb",
|
60
63
|
"spec/unit/container_spec.rb",
|
61
64
|
"spec/unit/generator_spec.rb",
|
@@ -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
|
data/spec/unit/accessors_spec.rb
CHANGED
@@ -60,6 +60,7 @@ describe "OM::XML::Accessors" do
|
|
60
60
|
before(:each) do
|
61
61
|
article_xml = fixture( File.join("mods_articles", "hydrangea_article1.xml") )
|
62
62
|
@sample = AccessorTest.from_xml(article_xml)
|
63
|
+
@sample.stubs(:ox_namespaces).returns("oxns"=>"http://www.loc.gov/mods/v3")
|
63
64
|
end
|
64
65
|
|
65
66
|
describe '#accessor' do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: om
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 8
|
10
|
+
version: 0.1.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Zumwalt
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- README.rdoc
|
110
110
|
- Rakefile
|
111
111
|
- VERSION
|
112
|
+
- container_spec.rb
|
112
113
|
- lib/om.rb
|
113
114
|
- lib/om/xml.rb
|
114
115
|
- lib/om/xml/accessors.rb
|
@@ -123,6 +124,7 @@ files:
|
|
123
124
|
- spec/fixtures/mods-3-2.xsd
|
124
125
|
- spec/fixtures/mods_articles/hydrangea_article1.xml
|
125
126
|
- spec/fixtures/test_dummy_mods.xml
|
127
|
+
- spec/integration/rights_metadata_integration_example_spec.rb
|
126
128
|
- spec/spec.opts
|
127
129
|
- spec/spec_helper.rb
|
128
130
|
- spec/unit/accessors_spec.rb
|
@@ -168,6 +170,7 @@ signing_key:
|
|
168
170
|
specification_version: 3
|
169
171
|
summary: "OM (Opinionated Metadata): A library to help you tame sprawling XML schemas like MODS."
|
170
172
|
test_files:
|
173
|
+
- spec/integration/rights_metadata_integration_example_spec.rb
|
171
174
|
- spec/spec_helper.rb
|
172
175
|
- spec/unit/accessors_spec.rb
|
173
176
|
- spec/unit/container_spec.rb
|