om 3.1.0 → 3.1.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.
- checksums.yaml +4 -4
- data/{COMMON_OM_PATTERNS.textile → COMMON_OM_PATTERNS.md} +136 -126
- data/CONTRIBUTING.md +2 -2
- data/GETTING_FANCY.md +153 -0
- data/GETTING_STARTED.md +329 -0
- data/Gemfile +1 -1
- data/History.md +164 -0
- data/LICENSE +15 -20
- data/QUERYING_DOCUMENTS.md +162 -0
- data/README.md +2 -2
- data/UPDATING_DOCUMENTS.md +6 -0
- data/gemfiles/gemfile.rails3 +1 -1
- data/gemfiles/gemfile.rails4 +1 -1
- data/lib/om/version.rb +1 -1
- data/lib/om/xml/dynamic_node.rb +42 -51
- data/lib/tasks/om.rake +1 -1
- data/om.gemspec +1 -2
- data/spec/integration/differentiated_elements_spec.rb +2 -2
- data/spec/integration/element_value_spec.rb +13 -13
- data/spec/integration/proxies_and_ref_spec.rb +10 -10
- data/spec/integration/querying_documents_spec.rb +20 -27
- data/spec/integration/rights_metadata_integration_example_spec.rb +4 -4
- data/spec/integration/selective_querying_spec.rb +1 -1
- data/spec/integration/serialization_spec.rb +15 -15
- data/spec/integration/set_reentrant_terminology_spec.rb +6 -6
- data/spec/integration/subclass_terminology_spec.rb +8 -8
- data/spec/integration/xpathy_stuff_spec.rb +10 -10
- data/spec/unit/container_spec.rb +27 -27
- data/spec/unit/document_spec.rb +24 -24
- data/spec/unit/dynamic_node_spec.rb +60 -49
- data/spec/unit/named_term_proxy_spec.rb +12 -7
- data/spec/unit/node_generator_spec.rb +4 -4
- data/spec/unit/nokogiri_sanity_spec.rb +17 -18
- data/spec/unit/om_spec.rb +2 -2
- data/spec/unit/template_registry_spec.rb +51 -51
- data/spec/unit/term_builder_spec.rb +45 -44
- data/spec/unit/term_spec.rb +55 -55
- data/spec/unit/term_value_operators_spec.rb +205 -205
- data/spec/unit/term_xpath_generator_spec.rb +33 -36
- data/spec/unit/terminology_builder_spec.rb +50 -47
- data/spec/unit/terminology_spec.rb +92 -92
- data/spec/unit/validation_spec.rb +12 -12
- data/spec/unit/xml_serialization_spec.rb +20 -20
- data/spec/unit/xml_spec.rb +3 -3
- data/spec/unit/xml_terminology_based_solrizer_spec.rb +18 -18
- metadata +11 -38
- data/GETTING_FANCY.textile +0 -145
- data/GETTING_STARTED.textile +0 -254
- data/History.textile +0 -186
- data/QUERYING_DOCUMENTS.textile +0 -139
- data/UPDATING_DOCUMENTS.textile +0 -3
@@ -19,28 +19,28 @@ describe "OM::XML::Validation" do
|
|
19
19
|
|
20
20
|
describe '#schema_url' do
|
21
21
|
it "should allow you to set the schema url" do
|
22
|
-
ValidationTest.schema_url.
|
22
|
+
expect(ValidationTest.schema_url).to eq "http://www.loc.gov/standards/mods/v3/mods-3-2.xsd"
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe "#schema" do
|
27
27
|
it "should return an instance of Nokogiri::XML::Schema loaded from the schema url -- fails if no internet connection" do
|
28
28
|
skip "no internet connection"
|
29
|
-
ValidationTest.schema.
|
29
|
+
expect(ValidationTest.schema).to be_kind_of Nokogiri::XML::Schema
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
describe "#validate" do
|
34
34
|
it "should validate the provided document against the schema provided in class definition -- fails if no internet connection" do
|
35
35
|
skip "no internet connection"
|
36
|
-
ValidationTest.schema.
|
36
|
+
expect(ValidationTest.schema).to receive(:validate).with(@sample).and_return([])
|
37
37
|
ValidationTest.validate(@sample)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
describe ".validate" do
|
42
42
|
it "should rely on class validate method" do
|
43
|
-
ValidationTest.
|
43
|
+
expect(ValidationTest).to receive(:validate).with(@sample)
|
44
44
|
@sample.validate
|
45
45
|
end
|
46
46
|
end
|
@@ -55,26 +55,26 @@ describe "OM::XML::Validation" do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should lazy load the schema file from the @schema_url" do
|
58
|
-
ValidationTest.instance_variable_get(:@schema_file).
|
59
|
-
ValidationTest.
|
58
|
+
expect(ValidationTest.instance_variable_get(:@schema_file)).to be_nil
|
59
|
+
expect(ValidationTest).to receive(:file_from_url).with(ValidationTest.schema_url).once.and_return("fake file")
|
60
60
|
ValidationTest.schema_file
|
61
|
-
ValidationTest.instance_variable_get(:@schema_file).
|
62
|
-
ValidationTest.schema_file.
|
61
|
+
expect(ValidationTest.instance_variable_get(:@schema_file)).to eq "fake file"
|
62
|
+
expect(ValidationTest.schema_file).to eq "fake file"
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
describe "#file_from_url" do
|
67
67
|
it "should retrieve a file from the provided url over HTTP" do
|
68
|
-
ValidationTest.
|
68
|
+
expect(ValidationTest).to receive(:open).with("http://google.com")
|
69
69
|
ValidationTest.send(:file_from_url, "http://google.com")
|
70
70
|
end
|
71
71
|
it "should raise an error if the url is invalid" do
|
72
|
-
lambda {ValidationTest.send(:file_from_url, "")}.
|
73
|
-
lambda {ValidationTest.send(:file_from_url, "foo")}.
|
72
|
+
expect(lambda {ValidationTest.send(:file_from_url, "")}).to raise_error(RuntimeError, /Could not retrieve file from /)
|
73
|
+
expect(lambda {ValidationTest.send(:file_from_url, "foo")}).to raise_error(RuntimeError, /Could not retrieve file from foo/)
|
74
74
|
end
|
75
75
|
it "should raise an error if file retrieval fails" do
|
76
76
|
skip "no internet connection"
|
77
|
-
lambda {ValidationTest.send(:file_from_url, "http://fedora-commons.org/nonexistent_file")}.
|
77
|
+
expect(lambda {ValidationTest.send(:file_from_url, "http://fedora-commons.org/nonexistent_file")}).to raise_error(RuntimeError, "Could not retrieve file from http://fedora-commons.org/nonexistent_file. Error: 404 Not Found")
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
@@ -7,13 +7,13 @@ describe "OM::XML::Terminology.to_xml" do
|
|
7
7
|
it "should put terminology details into the xml" do
|
8
8
|
expected_xml = "<namespaces>\n <namespace>\n <name>oxns</name>\n <identifier>http://www.loc.gov/mods/v3</identifier>\n </namespace>\n <namespace>\n <name>xmlns:foo</name>\n <identifier>http://my.custom.namespace</identifier>\n </namespace>\n <namespace>\n <name>xmlns</name>\n <identifier>http://www.loc.gov/mods/v3</identifier>\n </namespace>\n</namespaces>"
|
9
9
|
xml = @terminology.to_xml
|
10
|
-
xml.xpath("/terminology/schema").to_xml.
|
11
|
-
xml.xpath("/terminology/namespaces").to_xml.
|
10
|
+
expect(xml.xpath("/terminology/schema").to_xml).to eq "<schema>http://www.loc.gov/standards/mods/v3/mods-3-2.xsd</schema>"
|
11
|
+
expect(xml.xpath("/terminology/namespaces").to_xml).to be_equivalent_to expected_xml
|
12
12
|
end
|
13
13
|
it "should call .to_xml on all of the terms" do
|
14
14
|
options = {}
|
15
15
|
doc = Nokogiri::XML::Document.new
|
16
|
-
@terminology.terms.values.each {|term| term.
|
16
|
+
@terminology.terms.values.each {|term| expect(term).to receive(:to_xml) }
|
17
17
|
@terminology.to_xml(options,doc)
|
18
18
|
end
|
19
19
|
end
|
@@ -27,33 +27,33 @@ describe "OM::XML::Term.to_xml" do
|
|
27
27
|
end
|
28
28
|
it "should return an xml representation of the Term" do
|
29
29
|
xml = @person_first_name.to_xml
|
30
|
-
xml.xpath("/term").first.attributes["name"].value.
|
31
|
-
xml.xpath("/term/attributes/type").first.text.
|
32
|
-
xml.xpath("/term/path").first.text.
|
33
|
-
xml.xpath("/term/namespace_prefix").first.text.
|
34
|
-
xml.xpath("/term/children/*").
|
35
|
-
xml.xpath("/term/xpath/relative").first.text.
|
36
|
-
xml.xpath("/term/xpath/absolute").first.text.
|
37
|
-
xml.xpath("/term/xpath/constrained").first.text.
|
38
|
-
xml.xpath("/term/index_as").first.text.
|
39
|
-
xml.xpath("/term/required").first.text.
|
40
|
-
xml.xpath("/term/data_type").first.text.
|
30
|
+
expect(xml.xpath("/term").first.attributes["name"].value).to eq "first_name"
|
31
|
+
expect(xml.xpath("/term/attributes/type").first.text).to eq "given"
|
32
|
+
expect(xml.xpath("/term/path").first.text).to eq "namePart"
|
33
|
+
expect(xml.xpath("/term/namespace_prefix").first.text).to eq "oxns"
|
34
|
+
expect(xml.xpath("/term/children/*")).to be_empty
|
35
|
+
expect(xml.xpath("/term/xpath/relative").first.text).to eq "oxns:namePart[@type=\"given\"]"
|
36
|
+
expect(xml.xpath("/term/xpath/absolute").first.text).to eq "//oxns:name[@type=\"personal\"]/oxns:namePart[@type=\"given\"]"
|
37
|
+
expect(xml.xpath("/term/xpath/constrained").first.text).to eq "//oxns:name[@type=\\\"personal\\\"]/oxns:namePart[@type=\\\"given\\\" and contains(., \\\"\#{constraint_value}\\\")]"
|
38
|
+
expect(xml.xpath("/term/index_as").first.text).to eq ""
|
39
|
+
expect(xml.xpath("/term/required").first.text).to eq "false"
|
40
|
+
expect(xml.xpath("/term/data_type").first.text).to eq "string"
|
41
41
|
end
|
42
42
|
it "should capture root term info" do
|
43
43
|
xml = @terminology.root_terms.first.to_xml
|
44
|
-
xml.xpath("/term/is_root_term").text.
|
45
|
-
@person_first_name.to_xml.xpath("/term/is_root_term").
|
44
|
+
expect(xml.xpath("/term/is_root_term").text).to eq("true")
|
45
|
+
expect(@person_first_name.to_xml.xpath("/term/is_root_term")).to be_empty
|
46
46
|
end
|
47
47
|
it "should allow you to pass in a document to add the term to" do
|
48
48
|
doc = Nokogiri::XML::Document.new
|
49
|
-
@person_first_name.to_xml({}, doc).
|
49
|
+
expect(@person_first_name.to_xml({}, doc)).to eq doc
|
50
50
|
end
|
51
51
|
it "should include children" do
|
52
52
|
children = @person.to_xml.xpath("//term[@name=\"person\"]/children/*")
|
53
|
-
children.length.
|
54
|
-
children.each {|child| child.name.
|
53
|
+
expect(children.length).to eq(12)
|
54
|
+
children.each {|child| expect(child.name).to eq "term"}
|
55
55
|
end
|
56
56
|
it "should skip children if :children=>false" do
|
57
|
-
@person.to_xml(:children=>false).xpath("children").
|
57
|
+
expect(@person.to_xml(:children=>false).xpath("children")).to be_empty
|
58
58
|
end
|
59
59
|
end
|
data/spec/unit/xml_spec.rb
CHANGED
@@ -9,13 +9,13 @@ describe "OM::XML::Container" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should automatically include the other modules" do
|
12
|
-
XMLTest.included_modules.
|
13
|
-
XMLTest.included_modules.
|
12
|
+
expect(XMLTest.included_modules).to include(OM::XML::Container)
|
13
|
+
expect(XMLTest.included_modules).to include(OM::XML::Validation)
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "#sanitize_pointer" do
|
17
17
|
it "should convert any nested arrays into hashes" do
|
18
|
-
XMLTest.sanitize_pointer( [[:person,1],:role] ).
|
18
|
+
expect(XMLTest.sanitize_pointer( [[:person,1],:role] )).to eq [{:person=>1},:role]
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -16,13 +16,13 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
16
16
|
describe ".to_solr" do
|
17
17
|
|
18
18
|
it "should provide .to_solr and return a SolrDocument" do
|
19
|
-
@mods_article.
|
20
|
-
@mods_article.to_solr.
|
19
|
+
expect(@mods_article).to respond_to(:to_solr)
|
20
|
+
expect(@mods_article.to_solr).to be_kind_of(Hash)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should optionally allow you to provide the Hash to add fields to and return that document when done" do
|
24
24
|
doc = Hash.new
|
25
|
-
@mods_article.to_solr(doc).
|
25
|
+
expect(@mods_article.to_solr(doc)).to equal(doc)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should iterate through the terminology terms, calling .solrize_term on each and passing in the solr doc" do
|
@@ -30,24 +30,24 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
30
30
|
@mods_article.field_mapper = Solrizer::FieldMapper.new
|
31
31
|
Samples::ModsArticle.terminology.terms.each_pair do |k,v|
|
32
32
|
next if k == :mods # we don't index the root node
|
33
|
-
@mods_article.
|
33
|
+
expect(@mods_article).to receive(:solrize_term).with(v, solr_doc, @mods_article.field_mapper)
|
34
34
|
end
|
35
35
|
@mods_article.to_solr(solr_doc)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should use Solr mappings to generate field names" do
|
39
39
|
solr_doc = @mods_article.to_solr
|
40
|
-
solr_doc["abstract"].
|
40
|
+
expect(solr_doc["abstract"]).to be_nil
|
41
41
|
# NOTE: OM's old default expected stored and indexed; this is a change.
|
42
|
-
solr_doc["abstract_tesim"].
|
43
|
-
solr_doc["title_info_1_language_tesim"].
|
44
|
-
solr_doc["person_1_role_0_text_tesim"].
|
42
|
+
expect(solr_doc["abstract_tesim"]).to eq ["ABSTRACT"]
|
43
|
+
expect(solr_doc["title_info_1_language_tesim"]).to eq ["finnish"]
|
44
|
+
expect(solr_doc["person_1_role_0_text_tesim"]).to eq ["teacher"]
|
45
45
|
# No index_as on the code field.
|
46
|
-
solr_doc["person_1_role_0_code_tesim"].
|
47
|
-
solr_doc["person_last_name_tesim"].sort.
|
48
|
-
solr_doc["topic_tag_tesim"].sort.
|
46
|
+
expect(solr_doc["person_1_role_0_code_tesim"]).to be_nil
|
47
|
+
expect(solr_doc["person_last_name_tesim"].sort).to eq ["FAMILY NAME", "Gautama"]
|
48
|
+
expect(solr_doc["topic_tag_tesim"].sort).to eq ["CONTROLLED TERM", "TOPIC 1", "TOPIC 2"]
|
49
49
|
# These are a holdover from an old verison of OM
|
50
|
-
solr_doc['journal_0_issue_0_publication_date_dtsim'].
|
50
|
+
expect(solr_doc['journal_0_issue_0_publication_date_dtsim']).to eq ["2007-02-01T00:00:00Z"]
|
51
51
|
end
|
52
52
|
|
53
53
|
end
|
@@ -57,7 +57,7 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
57
57
|
it "should add fields to a solr document for all nodes corresponding to the given term and its children" do
|
58
58
|
solr_doc = Hash.new
|
59
59
|
result = @mods_article.solrize_term(Samples::ModsArticle.terminology.retrieve_term(:title_info), solr_doc)
|
60
|
-
result.
|
60
|
+
expect(result).to be solr_doc
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should add multiple fields based on index_as" do
|
@@ -70,7 +70,7 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
70
70
|
expected_names = ["DR.", "FAMILY NAME", "GIVEN NAMES", "PERSON_ID"]
|
71
71
|
%w(_teim _sim).each do |suffix|
|
72
72
|
actual_names = fake_solr_doc["name_0_namePart#{suffix}"].sort
|
73
|
-
actual_names.
|
73
|
+
expect(actual_names).to eq expected_names
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -78,14 +78,14 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
78
78
|
unless RUBY_VERSION.match("1.8.7")
|
79
79
|
solr_doc = Hash.new
|
80
80
|
result = @mods_article.solrize_term(Samples::ModsArticle.terminology.retrieve_term(:pub_date), solr_doc)
|
81
|
-
solr_doc["pub_date_dtsim"].
|
81
|
+
expect(solr_doc["pub_date_dtsim"]).to eq ["2007-02-01T00:00:00Z"]
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should add fields based on type using ref" do
|
86
86
|
solr_doc = Hash.new
|
87
87
|
result = @mods_article.solrize_term(Samples::ModsArticle.terminology.retrieve_term(:issue_date), solr_doc)
|
88
|
-
solr_doc["issue_date_dtsim"].
|
88
|
+
expect(solr_doc["issue_date_dtsim"]).to eq ["2007-02-15T00:00:00Z"]
|
89
89
|
end
|
90
90
|
|
91
91
|
it "shouldn't index terms where index_as is an empty array" do
|
@@ -94,7 +94,7 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
94
94
|
term.children[:namePart].index_as = []
|
95
95
|
|
96
96
|
@mods_article.solrize_term(term, fake_solr_doc)
|
97
|
-
fake_solr_doc["name_0_namePart_teim"].
|
97
|
+
expect(fake_solr_doc["name_0_namePart_teim"]).to be_nil
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should index terms where index_as is searchable" do
|
@@ -104,7 +104,7 @@ describe OM::XML::TerminologyBasedSolrizer do
|
|
104
104
|
|
105
105
|
@mods_article.solrize_term(term, fake_solr_doc)
|
106
106
|
|
107
|
-
fake_solr_doc["name_0_namePart_teim"].sort.
|
107
|
+
expect(fake_solr_doc["name_0_namePart_teim"].sort).to eq ["DR.", "FAMILY NAME", "GIVEN NAMES", "PERSON_ID"]
|
108
108
|
end
|
109
109
|
end
|
110
110
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: om
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Zumwalt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-05-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -109,34 +109,6 @@ dependencies:
|
|
109
109
|
- - ">="
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
-
- !ruby/object:Gem::Dependency
|
113
|
-
name: rdoc
|
114
|
-
requirement: !ruby/object:Gem::Requirement
|
115
|
-
requirements:
|
116
|
-
- - ">="
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: '0'
|
119
|
-
type: :development
|
120
|
-
prerelease: false
|
121
|
-
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
requirements:
|
123
|
-
- - ">="
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: '0'
|
126
|
-
- !ruby/object:Gem::Dependency
|
127
|
-
name: RedCloth
|
128
|
-
requirement: !ruby/object:Gem::Requirement
|
129
|
-
requirements:
|
130
|
-
- - ">="
|
131
|
-
- !ruby/object:Gem::Version
|
132
|
-
version: '0'
|
133
|
-
type: :development
|
134
|
-
prerelease: false
|
135
|
-
version_requirements: !ruby/object:Gem::Requirement
|
136
|
-
requirements:
|
137
|
-
- - ">="
|
138
|
-
- !ruby/object:Gem::Version
|
139
|
-
version: '0'
|
140
112
|
- !ruby/object:Gem::Dependency
|
141
113
|
name: awesome_print
|
142
114
|
requirement: !ruby/object:Gem::Requirement
|
@@ -179,17 +151,17 @@ files:
|
|
179
151
|
- ".gitignore"
|
180
152
|
- ".rspec"
|
181
153
|
- ".travis.yml"
|
182
|
-
- COMMON_OM_PATTERNS.
|
154
|
+
- COMMON_OM_PATTERNS.md
|
183
155
|
- CONTRIBUTING.md
|
184
|
-
- GETTING_FANCY.
|
185
|
-
- GETTING_STARTED.
|
156
|
+
- GETTING_FANCY.md
|
157
|
+
- GETTING_STARTED.md
|
186
158
|
- Gemfile
|
187
|
-
- History.
|
159
|
+
- History.md
|
188
160
|
- LICENSE
|
189
|
-
- QUERYING_DOCUMENTS.
|
161
|
+
- QUERYING_DOCUMENTS.md
|
190
162
|
- README.md
|
191
163
|
- Rakefile
|
192
|
-
- UPDATING_DOCUMENTS.
|
164
|
+
- UPDATING_DOCUMENTS.md
|
193
165
|
- container_spec.rb
|
194
166
|
- devel/notes.txt
|
195
167
|
- gemfiles/gemfile.rails3
|
@@ -254,7 +226,8 @@ files:
|
|
254
226
|
- spec/unit/xml_spec.rb
|
255
227
|
- spec/unit/xml_terminology_based_solrizer_spec.rb
|
256
228
|
homepage: http://github.com/projecthydra/om
|
257
|
-
licenses:
|
229
|
+
licenses:
|
230
|
+
- APACHE2
|
258
231
|
metadata: {}
|
259
232
|
post_install_message:
|
260
233
|
rdoc_options: []
|
@@ -272,7 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
272
245
|
version: '0'
|
273
246
|
requirements: []
|
274
247
|
rubyforge_project:
|
275
|
-
rubygems_version: 2.
|
248
|
+
rubygems_version: 2.5.1
|
276
249
|
signing_key:
|
277
250
|
specification_version: 4
|
278
251
|
summary: 'OM (Opinionated Metadata): A library to help you tame sprawling XML schemas
|
data/GETTING_FANCY.textile
DELETED
@@ -1,145 +0,0 @@
|
|
1
|
-
h1. Getting Fancy
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
h2. Alternative ways to Manipulate Terms, Terminologies and their Builders
|
6
|
-
|
7
|
-
There is more than one way to build a terminology.
|
8
|
-
|
9
|
-
h3. OM::XML::Terminology::Builder":OM/XML/Terminology/Builder.html Block Syntax
|
10
|
-
|
11
|
-
The simplest way to create an OM Terminology is to use the "OM::XML::Terminology::Builder":OM/XML/Terminology/Builder.html" block syntax.
|
12
|
-
|
13
|
-
In the following examples, we will show different ways of building this Terminology:
|
14
|
-
|
15
|
-
<pre>
|
16
|
-
builder = OM::XML::Terminology::Builder.new do |t|
|
17
|
-
t.root(:path=>"grants", :xmlns=>"http://yourmediashelf.com/schemas/hydra-dataset/v0", :schema=>"http://example.org/schemas/grants-v1.xsd")
|
18
|
-
t.grant {
|
19
|
-
t.org(:path=>"organization", :attributes=>{:type=>"funder"}) {
|
20
|
-
t.name(:index_as=>:searchable)
|
21
|
-
}
|
22
|
-
t.number
|
23
|
-
}
|
24
|
-
end
|
25
|
-
another_terminology = builder.build
|
26
|
-
</pre>
|
27
|
-
|
28
|
-
|
29
|
-
h3. Using "OM::XML::Term":OM/XML/Term.html ::Builders
|
30
|
-
|
31
|
-
First, create the Terminology Builder object:
|
32
|
-
|
33
|
-
<pre>
|
34
|
-
terminology_builder = OM::XML::Terminology::Builder.new
|
35
|
-
</pre>
|
36
|
-
|
37
|
-
The .root method handles creating the root term and setting namespaces, schema, etc. on the Terminology:
|
38
|
-
|
39
|
-
<pre>
|
40
|
-
terminology_builder.root(:path=>"grants", :xmlns=>"http://yourmediashelf.com/schemas/hydra-dataset/v0", :schema=>"http://example.org/schemas/grants-v1.xsd")
|
41
|
-
</pre>
|
42
|
-
|
43
|
-
This sets the namespaces for you and created the "grants" root term:
|
44
|
-
|
45
|
-
<pre>
|
46
|
-
terminology_builder.namespaces
|
47
|
-
=> {"oxns"=>"http://yourmediashelf.com/schemas/hydra-dataset/v0", "xmlns"=>"http://yourmediashelf.com/schemas/hydra-dataset/v0"}
|
48
|
-
terminology_builder.term_builders
|
49
|
-
</pre>
|
50
|
-
|
51
|
-
Create Term Builders for each of the Terms:
|
52
|
-
|
53
|
-
<pre>
|
54
|
-
term1_builder = OM::XML::Term::Builder.new("grant", terminology_builder).path("grant")
|
55
|
-
subterm1_builder = OM::XML::Term::Builder.new("org", terminology_builder).attributes(:type=>"funder")
|
56
|
-
subsubterm_builder = OM::XML::Term::Builder.new("name", terminology_builder).index_as(:searchable)
|
57
|
-
subterm2_builder = OM::XML::Term::Builder.new("number", terminology_builder)
|
58
|
-
</pre>
|
59
|
-
|
60
|
-
Assemble the tree of Term builders by adding child builders to their parents; then add the top level terms to the root term in the Terminology builder:
|
61
|
-
|
62
|
-
<pre>
|
63
|
-
subterm1_builder.add_child(subsubterm_builder)
|
64
|
-
term1_builder.add_child(subterm1_builder)
|
65
|
-
term1_builder.add_child(subterm2_builder)
|
66
|
-
terminology_builder.term_builders["grant"] = term1_builder
|
67
|
-
</pre>
|
68
|
-
|
69
|
-
Now build the Terminology, which will also call .build on each of the Term Builders in the tree:
|
70
|
-
|
71
|
-
<pre>
|
72
|
-
built_terminology = terminology_builder.build
|
73
|
-
</pre>
|
74
|
-
|
75
|
-
Test it out:
|
76
|
-
|
77
|
-
<pre>
|
78
|
-
built_terminology.retrieve_term(:grant, :org, :name)
|
79
|
-
built_terminology.xpath_for(:grant, :org, :name)
|
80
|
-
built_terminology.root_terms
|
81
|
-
built_terminology.terms.keys # This will only return the Terms at the root of the terminology hierarchy
|
82
|
-
built_terminology.retrieve_term(:grant).children.keys
|
83
|
-
</pre>
|
84
|
-
|
85
|
-
h3. Creating Terms & Terminologies without any Builders
|
86
|
-
|
87
|
-
If you want to manipulate Terms and Terminologies directly rather than using the Builder classes, you can consume their APIs at any time.
|
88
|
-
|
89
|
-
People don't often do this, but the option is there if you need it.
|
90
|
-
|
91
|
-
Create the Terminology, set its namespaces & (optional) schema:
|
92
|
-
(Note that you have to set the :oxns namespaces to match :xmlns. This is usually done for you by the Terminology::Builder.root method.)
|
93
|
-
|
94
|
-
<pre>
|
95
|
-
handcrafted_terminology = OM::XML::Terminology.new
|
96
|
-
handcrafted_terminology.namespaces[:xmlns] = "http://yourmediashelf.com/schemas/hydra-dataset/v0"
|
97
|
-
handcrafted_terminology.namespaces[:oxns] = "http://yourmediashelf.com/schemas/hydra-dataset/v0"
|
98
|
-
handcrafted_terminology.schema = "http://example.org/schemas/grants-v1.xsd"
|
99
|
-
</pre>
|
100
|
-
|
101
|
-
Create the Terms:
|
102
|
-
|
103
|
-
<pre>
|
104
|
-
# Create term1 (the root) and set it as the root term
|
105
|
-
root_term = OM::XML::Term.new("grants")
|
106
|
-
root_term.is_root_term = true
|
107
|
-
|
108
|
-
# Create term1 (grant) and its subterms
|
109
|
-
term1 = OM::XML::Term.new("grant")
|
110
|
-
|
111
|
-
subterm1 = OM::XML::Term.new("org")
|
112
|
-
subterm1.path = "organization"
|
113
|
-
subterm1.attributes = {:type=>"funder"}
|
114
|
-
|
115
|
-
subsubterm = OM::XML::Term.new("name")
|
116
|
-
subsubterm.index_as = :searchable
|
117
|
-
|
118
|
-
subterm2 = OM::XML::Term.new("number")
|
119
|
-
</pre>
|
120
|
-
|
121
|
-
Assemble the tree of terms by adding child terms to their parents, then add those to the Terminology.
|
122
|
-
|
123
|
-
<pre>
|
124
|
-
subterm1.add_child(subsubterm)
|
125
|
-
term1.add_child(subterm1)
|
126
|
-
term1.add_child(subterm2)
|
127
|
-
handcrafted_terminology.add_term(root_term)
|
128
|
-
handcrafted_terminology.add_term(term1)
|
129
|
-
</pre>
|
130
|
-
|
131
|
-
Generate the xpath queries for each term. This is usually done for you by the Term Builder.build method:
|
132
|
-
|
133
|
-
<pre>
|
134
|
-
[root_term, term1, subterm1, subsubterm, subterm2].each {|t| t.generate_xpath_queries!}
|
135
|
-
</pre>
|
136
|
-
|
137
|
-
Test it out:
|
138
|
-
|
139
|
-
<pre>
|
140
|
-
handcrafted_terminology.retrieve_term(:grant, :org, :name)
|
141
|
-
handcrafted_terminology.xpath_for(:grant, :org, :name)
|
142
|
-
handcrafted_terminology.root_terms
|
143
|
-
handcrafted_terminology.terms.keys # This will only return the Terms at the root of the terminology hierarchy
|
144
|
-
handcrafted_terminology.retrieve_term(:grant).children.keys
|
145
|
-
</pre>
|