opinionated-xml 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/om.rb +9 -0
- data/lib/om/xml.rb +23 -0
- data/lib/om/xml/accessors.rb +130 -0
- data/lib/om/xml/container.rb +32 -0
- data/lib/{opinionated-xml/ox.rb → om/xml/properties.rb} +10 -60
- data/lib/om/xml/property_value_operators.rb +111 -0
- data/lib/om/xml/validation.rb +63 -0
- data/opinionated-xml.gemspec +26 -14
- data/spec/fixtures/RUBRIC_mods_article_template.xml +89 -0
- data/spec/fixtures/mods_articles/hydrangea_article1.xml +90 -0
- data/spec/fixtures/test_dummy_mods.xml +17 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/accessors_spec.rb +156 -0
- data/spec/unit/container_spec.rb +60 -0
- data/spec/unit/{opinionated-xml_spec.rb → properties_spec.rb} +13 -66
- data/spec/unit/property_value_operators_spec.rb +245 -0
- data/spec/unit/validation_spec.rb +78 -0
- data/spec/unit/xml_spec.rb +21 -0
- metadata +40 -13
- data/lib/opinionated-xml.rb +0 -9
- data/lib/opinionated-xml/ox_property_values_helper.rb +0 -62
- data/spec/helpers/ox_property_values_helper_spec.rb +0 -55
- data/spec/unit/ox_integration_spec.rb +0 -124
@@ -0,0 +1,21 @@
|
|
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 XMLTest
|
9
|
+
include OM::XML
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
it "should automatically include the other modules" do
|
15
|
+
XMLTest.included_modules.should include(OM::XML::Container)
|
16
|
+
XMLTest.included_modules.should include(OM::XML::Accessors)
|
17
|
+
XMLTest.included_modules.should include(OM::XML::Validation)
|
18
|
+
XMLTest.included_modules.should include(OM::XML::Properties)
|
19
|
+
XMLTest.included_modules.should include(OM::XML::PropertyValueOperators)
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opinionated-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Matt Zumwalt
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-06-20 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: nokogiri
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
27
30
|
segments:
|
28
31
|
- 0
|
29
32
|
version: "0"
|
@@ -33,9 +36,11 @@ dependencies:
|
|
33
36
|
name: facets
|
34
37
|
prerelease: false
|
35
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
36
40
|
requirements:
|
37
41
|
- - ">="
|
38
42
|
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
39
44
|
segments:
|
40
45
|
- 0
|
41
46
|
version: "0"
|
@@ -45,9 +50,11 @@ dependencies:
|
|
45
50
|
name: rspec
|
46
51
|
prerelease: false
|
47
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
48
54
|
requirements:
|
49
55
|
- - ">="
|
50
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 13
|
51
58
|
segments:
|
52
59
|
- 1
|
53
60
|
- 2
|
@@ -59,9 +66,11 @@ dependencies:
|
|
59
66
|
name: mocha
|
60
67
|
prerelease: false
|
61
68
|
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
62
70
|
requirements:
|
63
71
|
- - ">="
|
64
72
|
- !ruby/object:Gem::Version
|
73
|
+
hash: 43
|
65
74
|
segments:
|
66
75
|
- 0
|
67
76
|
- 9
|
@@ -73,9 +82,11 @@ dependencies:
|
|
73
82
|
name: ruby-debug
|
74
83
|
prerelease: false
|
75
84
|
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
76
86
|
requirements:
|
77
87
|
- - ">="
|
78
88
|
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
79
90
|
segments:
|
80
91
|
- 0
|
81
92
|
version: "0"
|
@@ -98,18 +109,27 @@ files:
|
|
98
109
|
- README.rdoc
|
99
110
|
- Rakefile
|
100
111
|
- VERSION
|
101
|
-
- lib/
|
102
|
-
- lib/
|
103
|
-
- lib/
|
112
|
+
- lib/om.rb
|
113
|
+
- lib/om/xml.rb
|
114
|
+
- lib/om/xml/accessors.rb
|
115
|
+
- lib/om/xml/container.rb
|
116
|
+
- lib/om/xml/properties.rb
|
117
|
+
- lib/om/xml/property_value_operators.rb
|
118
|
+
- lib/om/xml/validation.rb
|
104
119
|
- opinionated-xml.gemspec
|
105
120
|
- spec/fixtures/CBF_MODS/ARS0025_016.xml
|
121
|
+
- spec/fixtures/RUBRIC_mods_article_template.xml
|
106
122
|
- spec/fixtures/mods-3-2.xsd
|
123
|
+
- spec/fixtures/mods_articles/hydrangea_article1.xml
|
107
124
|
- spec/fixtures/test_dummy_mods.xml
|
108
|
-
- spec/helpers/ox_property_values_helper_spec.rb
|
109
125
|
- spec/spec.opts
|
110
126
|
- spec/spec_helper.rb
|
111
|
-
- spec/unit/
|
112
|
-
- spec/unit/
|
127
|
+
- spec/unit/accessors_spec.rb
|
128
|
+
- spec/unit/container_spec.rb
|
129
|
+
- spec/unit/properties_spec.rb
|
130
|
+
- spec/unit/property_value_operators_spec.rb
|
131
|
+
- spec/unit/validation_spec.rb
|
132
|
+
- spec/unit/xml_spec.rb
|
113
133
|
has_rdoc: true
|
114
134
|
homepage: http://github.com/mediashelf/opinionated-xml
|
115
135
|
licenses: []
|
@@ -120,28 +140,35 @@ rdoc_options:
|
|
120
140
|
require_paths:
|
121
141
|
- lib
|
122
142
|
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
123
144
|
requirements:
|
124
145
|
- - ">="
|
125
146
|
- !ruby/object:Gem::Version
|
147
|
+
hash: 3
|
126
148
|
segments:
|
127
149
|
- 0
|
128
150
|
version: "0"
|
129
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
130
153
|
requirements:
|
131
154
|
- - ">="
|
132
155
|
- !ruby/object:Gem::Version
|
156
|
+
hash: 3
|
133
157
|
segments:
|
134
158
|
- 0
|
135
159
|
version: "0"
|
136
160
|
requirements: []
|
137
161
|
|
138
162
|
rubyforge_project:
|
139
|
-
rubygems_version: 1.3.
|
163
|
+
rubygems_version: 1.3.7
|
140
164
|
signing_key:
|
141
165
|
specification_version: 3
|
142
166
|
summary: A library to help you tame sprawling XML schemas like MODS.
|
143
167
|
test_files:
|
144
|
-
- spec/helpers/ox_property_values_helper_spec.rb
|
145
168
|
- spec/spec_helper.rb
|
146
|
-
- spec/unit/
|
147
|
-
- spec/unit/
|
169
|
+
- spec/unit/accessors_spec.rb
|
170
|
+
- spec/unit/container_spec.rb
|
171
|
+
- spec/unit/properties_spec.rb
|
172
|
+
- spec/unit/property_value_operators_spec.rb
|
173
|
+
- spec/unit/validation_spec.rb
|
174
|
+
- spec/unit/xml_spec.rb
|
data/lib/opinionated-xml.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
require "open-uri"
|
2
|
-
require "logger"
|
3
|
-
|
4
|
-
class OX::ParentNodeNotFoundError < RuntimeError; end
|
5
|
-
module OX::PropertyValuesHelper
|
6
|
-
|
7
|
-
def property_values(lookup_args)
|
8
|
-
result = []
|
9
|
-
lookup(lookup_args).each {|node| result << node.text }
|
10
|
-
return result
|
11
|
-
end
|
12
|
-
|
13
|
-
def property_values_append(opts={})
|
14
|
-
parent_select = opts[:parent_select]
|
15
|
-
parent_index = opts[:parent_index]
|
16
|
-
template = opts[:template]
|
17
|
-
new_values = opts[:values]
|
18
|
-
|
19
|
-
# If template is a string, use it as the template, otherwise use it as arguments to builder_template
|
20
|
-
unless template.instance_of?(String)
|
21
|
-
template_args = Array(template)
|
22
|
-
if template_args.last.kind_of?(Hash)
|
23
|
-
template_opts = template_args.delete_at(template_args.length - 1)
|
24
|
-
else
|
25
|
-
template_opts = {}
|
26
|
-
end
|
27
|
-
template = self.class.builder_template( template_args, template_opts )
|
28
|
-
end
|
29
|
-
|
30
|
-
new_values = Array(new_values)
|
31
|
-
# template = self.class.builder_template(property_ref)
|
32
|
-
|
33
|
-
parent_select = Array(parent_select)
|
34
|
-
parent_nodeset = lookup(parent_select[0], parent_select[1])
|
35
|
-
|
36
|
-
if parent_index.kind_of?(Integer)
|
37
|
-
parent_node = parent_nodeset[parent_index]
|
38
|
-
elsif parent_index.kind_of?(Symbol) && parent_nodeset.respond_to?(parent_index)
|
39
|
-
parent_node = parent_nodeset.send(parent_index)
|
40
|
-
end
|
41
|
-
|
42
|
-
if parent_node.nil?
|
43
|
-
raise OX::ParentNodeNotFoundError, "Failed to find a parent node to insert values into based on :parent_select #{parent_select.inspect} with :parent_index #{parent_index.inspect}"
|
44
|
-
end
|
45
|
-
|
46
|
-
builder = Nokogiri::XML::Builder.with(parent_node) do |xml|
|
47
|
-
new_values.each do |builder_new_value|
|
48
|
-
builder_arg = eval('"'+ template + '"') # this inserts builder_new_value into the builder template
|
49
|
-
eval(builder_arg)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# Nokogiri::XML::Node.new(builder.to_xml, foo)
|
54
|
-
|
55
|
-
return parent_node
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
def property_value_set(property_ref, query_opts, node_index, new_value)
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
class FakePVHClass
|
4
|
-
include OX::PropertyValuesHelper
|
5
|
-
end
|
6
|
-
|
7
|
-
def helper
|
8
|
-
@fake_includer
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "OX::PropertyValuesHelper" do
|
12
|
-
|
13
|
-
before(:all) do
|
14
|
-
@fake_includer = FakePVHClass.new
|
15
|
-
end
|
16
|
-
|
17
|
-
describe ".property_values" do
|
18
|
-
|
19
|
-
it "should call .lookup and then build an array of values from the returned nodeset (using default_node, etc as nessesary)" do
|
20
|
-
lookup_opts = "insert args here"
|
21
|
-
mock_node = mock("node")
|
22
|
-
mock_node.expects(:text).returns("sample value").times(3)
|
23
|
-
mock_nodeset = [mock_node, mock_node, mock_node]
|
24
|
-
helper.expects(:lookup).with(lookup_opts).returns(mock_nodeset)
|
25
|
-
|
26
|
-
helper.property_values(lookup_opts).should == ["sample value","sample value","sample value"]
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
describe ".property_values_append" do
|
32
|
-
|
33
|
-
# see ../unit/ox_integration_spec.rb
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
describe ".property_value_set" do
|
38
|
-
|
39
|
-
it "should " do
|
40
|
-
pending "in progress..."
|
41
|
-
@fixturemods.property_value_set('//oxns:name[@type="personal"]',1,"foo")
|
42
|
-
@fixturemods.property_value_set([ [:person,:role], "donor"], 5, "special donor")
|
43
|
-
end
|
44
|
-
|
45
|
-
it "could support alternative notation" do
|
46
|
-
pending "this would be for the sake of consistency with the method signature of property_values_append"
|
47
|
-
# @fixturemods.property_value_set(
|
48
|
-
# :node_select =>'//oxns:name[@type="personal"]',
|
49
|
-
# :node_index => 1,
|
50
|
-
# :values => "foo"
|
51
|
-
# ).to_xml.should == expected_result
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
@@ -1,124 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "OpinionatedXml" do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
#ModsHelpers.name_("Beethoven, Ludwig van", :date=>"1770-1827", :role=>"creator")
|
7
|
-
class FakeOxIntegrationMods < Nokogiri::XML::Document
|
8
|
-
|
9
|
-
include OX
|
10
|
-
|
11
|
-
# Could add support for multiple root declarations.
|
12
|
-
# For now, assume that any modsCollections have already been broken up and fed in as individual mods documents
|
13
|
-
# root :mods_collection, :path=>"modsCollection",
|
14
|
-
# :attributes=>[],
|
15
|
-
# :subelements => :mods
|
16
|
-
|
17
|
-
root_property :mods, "mods", "http://www.loc.gov/mods/v3", :attributes=>["id", "version"], :schema=>"http://www.loc.gov/standards/mods/v3/mods-3-2.xsd"
|
18
|
-
|
19
|
-
|
20
|
-
property :name_, :path=>"name",
|
21
|
-
:attributes=>[:xlink, :lang, "xml:lang", :script, :transliteration, {:type=>["personal", "enumerated", "corporate"]} ],
|
22
|
-
:subelements=>["namePart", "displayForm", "affiliation", :role, "description"],
|
23
|
-
:default_content_path => "namePart",
|
24
|
-
:convenience_methods => {
|
25
|
-
:date => {:path=>"namePart", :attributes=>{:type=>"date"}},
|
26
|
-
:family_name => {:path=>"namePart", :attributes=>{:type=>"family"}},
|
27
|
-
:given_name => {:path=>"namePart", :attributes=>{:type=>"given"}},
|
28
|
-
:terms_of_address => {:path=>"namePart", :attributes=>{:type=>"termsOfAddress"}}
|
29
|
-
}
|
30
|
-
|
31
|
-
property :person, :variant_of=>:name_, :attributes=>{:type=>"personal"}
|
32
|
-
|
33
|
-
property :role, :path=>"role",
|
34
|
-
:parents=>[:name_],
|
35
|
-
:attributes=>[ { "type"=>["text", "code"] } , "authority"],
|
36
|
-
:default_content_path => "roleTerm"
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
before(:each) do
|
44
|
-
@fixturemods = FakeOxIntegrationMods.parse( fixture( File.join("test_dummy_mods.xml") ) )
|
45
|
-
end
|
46
|
-
|
47
|
-
after(:all) do
|
48
|
-
Object.send(:remove_const, :FakeOxIntegrationMods)
|
49
|
-
end
|
50
|
-
|
51
|
-
describe ".property_values_append" do
|
52
|
-
|
53
|
-
it "looks up the parent using :parent_select, uses :parent_index to choose the parent node from the result set, uses :template to build the node(s) to be inserted, inserts the :values(s) into the node(s) and adds the node(s) to the parent" do
|
54
|
-
@fixturemods.property_values_append(
|
55
|
-
:parent_select => [:person, {:given_name=>"Tim", :family_name=>"Berners-Lee"}] ,
|
56
|
-
:parent_index => :first,
|
57
|
-
:template => [:person, :affiliation],
|
58
|
-
:values => ["my new value", "another new value"]
|
59
|
-
)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should accept parent_select and template [property_reference, lookup_opts] as argument arrays for generators/lookups" do
|
63
|
-
# this appends two affiliation nodes into the first person node whose name is Tim Berners-Lee
|
64
|
-
expected_result = '<ns3:name type="personal">
|
65
|
-
<ns3:namePart type="family">Berners-Lee</ns3:namePart>
|
66
|
-
<ns3:namePart type="given">Tim</ns3:namePart>
|
67
|
-
<ns3:affiliation>my new value</ns3:affiliation><ns3:affiliation>another new value</ns3:affiliation></ns3:name>'
|
68
|
-
#expected_result = Nokogiri::XML::Node.new( expected_result, @fixturemods )
|
69
|
-
|
70
|
-
@fixturemods.property_values_append(
|
71
|
-
:parent_select => [:person, {:given_name=>"Tim", :family_name=>"Berners-Lee"}] ,
|
72
|
-
:parent_index => :first,
|
73
|
-
:template => [:person, :affiliation],
|
74
|
-
:values => ["my new value", "another new value"]
|
75
|
-
).to_xml.should == expected_result
|
76
|
-
|
77
|
-
@fixturemods.lookup(:person, {:given_name=>"Tim", :family_name=>"Berners-Lee"}).first.to_xml.should == expected_result
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should accept symbols as arguments for generators/lookups" do
|
81
|
-
# this appends a role of "my role" into the third "person" node in the document
|
82
|
-
expected_result = "<ns3:name type=\"personal\">\n <ns3:namePart type=\"family\">Klimt</ns3:namePart>\n <ns3:namePart type=\"given\">Gustav</ns3:namePart>\n <ns3:role type=\"text\"><ns3:roleTerm>my role</ns3:roleTerm></ns3:role></ns3:name>"
|
83
|
-
|
84
|
-
@fixturemods.property_values_append(
|
85
|
-
:parent_select => :person ,
|
86
|
-
:parent_index => 3,
|
87
|
-
:template => :role,
|
88
|
-
:values => "my role"
|
89
|
-
).to_xml.should == expected_result
|
90
|
-
|
91
|
-
@fixturemods.lookup(:person)[3].to_xml.should == expected_result
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should accept parent_select as an (xpath) string and template as a (template) string" do
|
95
|
-
# this uses the provided template to add a node into the first node resulting from the xpath '//oxns:name[@type="personal"]'
|
96
|
-
expected_result = "<ns3:name type=\"personal\">\n <ns3:namePart type=\"family\">Berners-Lee</ns3:namePart>\n <ns3:namePart type=\"given\">Tim</ns3:namePart>\n <ns3:role type=\"code\" authority=\"marcrelator\"><ns3:roleTerm>creator</ns3:roleTerm></ns3:role></ns3:name>"
|
97
|
-
@fixturemods.property_values_append(
|
98
|
-
:parent_select =>'//oxns:name[@type="personal"]',
|
99
|
-
:parent_index => 0,
|
100
|
-
:template => 'xml.role( :type=>\'code\', :authority=>\'marcrelator\' ) { xml.roleTerm( \'#{builder_new_value}\' ) }',
|
101
|
-
:values => "creator"
|
102
|
-
).to_xml.should == expected_result
|
103
|
-
|
104
|
-
@fixturemods.lookup(:person).first.to_xml.should == expected_result
|
105
|
-
end
|
106
|
-
|
107
|
-
it "should support more complex mixing & matching" do
|
108
|
-
expected_result = "<ns3:name type=\"personal\">\n <ns3:namePart type=\"family\">Jobs</ns3:namePart>\n <ns3:namePart type=\"given\">Steve</ns3:namePart>\n <ns3:role type=\"code\" authority=\"marcrelator\"><ns3:roleTerm>foo</ns3:roleTerm></ns3:role></ns3:name>"
|
109
|
-
|
110
|
-
@fixturemods.property_values_append(
|
111
|
-
:parent_select =>'//oxns:name[@type="personal"]',
|
112
|
-
:parent_index => 1,
|
113
|
-
:template => [ :person, :role, {:attributes=>{"type"=>"code", "authority"=>"marcrelator"}} ],
|
114
|
-
:values => "foo"
|
115
|
-
).to_xml.should == expected_result
|
116
|
-
|
117
|
-
@fixturemods.lookup(:person)[1].to_xml.should == expected_result
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should raise exception if no node corresponds to the provided :parent_select and :parent_index"
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
end
|