opinionated-xml 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/History.textile +1 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/opinionated-xml/ox.rb +430 -0
- data/lib/opinionated-xml/ox_property_values_helper.rb +62 -0
- data/lib/opinionated-xml.rb +9 -0
- data/opinionated-xml.gemspec +77 -0
- data/spec/fixtures/CBF_MODS/ARS0025_016.xml +94 -0
- data/spec/fixtures/mods-3-2.xsd +1 -24
- data/spec/fixtures/test_dummy_mods.xml +19 -0
- data/spec/helpers/ox_property_values_helper_spec.rb +55 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/unit/opinionated-xml_spec.rb +300 -0
- data/spec/unit/ox_integration_spec.rb +124 -0
- metadata +147 -0
@@ -0,0 +1,124 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: opinionated-xml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Matt Zumwalt
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-16 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: nokogiri
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: facets
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: rspec
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 1
|
53
|
+
- 2
|
54
|
+
- 9
|
55
|
+
version: 1.2.9
|
56
|
+
type: :development
|
57
|
+
version_requirements: *id003
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: mocha
|
60
|
+
prerelease: false
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
- 9
|
68
|
+
- 8
|
69
|
+
version: 0.9.8
|
70
|
+
type: :development
|
71
|
+
version_requirements: *id004
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: ruby-debug
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
type: :development
|
83
|
+
version_requirements: *id005
|
84
|
+
description: A library to help you tame sprawling XML schemas like MODS. Wraps Nokogiri documents in objects with miscellaneous helper methods for doing things like retrieve generated xpath queries or look up properties based on a simplified DSL
|
85
|
+
email: matt.zumwalt@yourmediashelf.com
|
86
|
+
executables: []
|
87
|
+
|
88
|
+
extensions: []
|
89
|
+
|
90
|
+
extra_rdoc_files:
|
91
|
+
- LICENSE
|
92
|
+
- README.rdoc
|
93
|
+
files:
|
94
|
+
- .document
|
95
|
+
- .gitignore
|
96
|
+
- History.textile
|
97
|
+
- LICENSE
|
98
|
+
- README.rdoc
|
99
|
+
- Rakefile
|
100
|
+
- VERSION
|
101
|
+
- lib/opinionated-xml.rb
|
102
|
+
- lib/opinionated-xml/ox.rb
|
103
|
+
- lib/opinionated-xml/ox_property_values_helper.rb
|
104
|
+
- opinionated-xml.gemspec
|
105
|
+
- spec/fixtures/CBF_MODS/ARS0025_016.xml
|
106
|
+
- spec/fixtures/mods-3-2.xsd
|
107
|
+
- spec/fixtures/test_dummy_mods.xml
|
108
|
+
- spec/helpers/ox_property_values_helper_spec.rb
|
109
|
+
- spec/spec.opts
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/unit/opinionated-xml_spec.rb
|
112
|
+
- spec/unit/ox_integration_spec.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: http://github.com/mediashelf/opinionated-xml
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options:
|
119
|
+
- --charset=UTF-8
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
version: "0"
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.3.6
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: A library to help you tame sprawling XML schemas like MODS.
|
143
|
+
test_files:
|
144
|
+
- spec/helpers/ox_property_values_helper_spec.rb
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
- spec/unit/opinionated-xml_spec.rb
|
147
|
+
- spec/unit/ox_integration_spec.rb
|