om 2.2.1 → 3.0.0.beta1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 047198eb8ea0cf1f0b32818fb21ea09be9f4edd7
4
- data.tar.gz: 771c06c6304173f63d98db27a490eaf9adc38205
3
+ metadata.gz: 311c97a223b0160f5766d16da7ec636a85073c39
4
+ data.tar.gz: 907162fe5292c1316732f68be31d987a3892e402
5
5
  SHA512:
6
- metadata.gz: d7c61df6a42241e45281c0831dbf822282c2630a019617d13c3e8aca005e9b017a6b16adfb10541df0553091e272b428a90d73eb9a29a7c3f0816a863415aca5
7
- data.tar.gz: 2d5a6d8d73f56fd06ac885a55468c38c62f642d3d5090ce7c1c7e81af32dc0f93183647a63bc5f919a771223df8347374846b8798b413fbdda66b761b7fd1c82
6
+ metadata.gz: 249f1349616500b6fbf03cb8d2168fe87e19ea2084f29a77ea5a15996f349ed4b71a84c17a0b9b636f70f522d89f8be4d5f2dda3b3d41d442d5e4eea1830586e
7
+ data.tar.gz: 10386c58e84a42859682dc6e70b9fe11c548befbaec48185194d7c4897803d6ab8e5876ac4d5c6f57b62b943a34e5419502fc99575b8d4ed1b1fbc8fd82b5ebe
data/History.textile CHANGED
@@ -1,9 +1,3 @@
1
- h3. 2.2.0 (20 June 2013)
2
- Deprecate passing Hash values into DynamicNode#val= or
3
- Document#update_attributes. This behavior will be removed in 3.0.0
4
- Rails 4 support
5
- Pass nil in order to remove a node (instead of blank string)
6
-
7
1
  h3. 2.1.2 (3 May 2013)
8
2
  Fix missing comma after exception
9
3
 
data/lib/om/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Om
2
- VERSION = "2.2.1"
2
+ VERSION = "3.0.0.beta1"
3
3
  end
@@ -69,11 +69,19 @@ module OM
69
69
 
70
70
  def val=(args)
71
71
  @document.ng_xml_will_change!
72
+ raise ArgumentError, "The new_values passed to #{to_pointer} must be an array or a string. You provided #{args.first.inspect}" unless [Array, String, NilClass].include?(args.first.class)
72
73
  new_values = term.sanitize_new_values(args.first)
74
+ existing_nodes = @document.find_by_xpath(xpath)
75
+ if existing_nodes.length > new_values.length
76
+ starting_index = new_values.length + 1
77
+ starting_index.upto(existing_nodes.size).each do |index|
78
+ @document.term_value_delete select: xpath, child_index: index
79
+ end
80
+ end
73
81
  new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
74
82
  z = new_values[y]
75
83
  ## If we pass something that already has an index on it, we should be able to add it.
76
- if @document.find_by_xpath(xpath)[y.to_i].nil? || y.to_i == -1
84
+ if existing_nodes[y.to_i].nil? || y.to_i == -1
77
85
  parent_pointer = parent ? parent.to_pointer : nil
78
86
  @document.term_values_append(:parent_select=> parent_pointer,:parent_index=>0,:template=>to_pointer,:values=>z)
79
87
  else
@@ -29,10 +29,7 @@ module OM::XML::TermValueOperators
29
29
  #
30
30
  # @param [Hash] params
31
31
  # @example
32
- # {[{":person"=>"0"}, "role", "text"]=>["role1", "role2", "role3"],
33
- # [{:person=>1}, :family_name]=>"Andronicus",
34
- # [{"person"=>"1"},:given_name]=>["Titus"],
35
- # [{:person=>1},:role,:text]=>["otherrole1","otherrole2"] }
32
+ # {[{":person"=>"0"}, "role", "text"]=>["role1", "role2", "role3"], [{:person=>1}, :family_name]=>"Andronicus", [{"person"=>"1"},:given_name]=>["Titus"],[{:person=>1},:role,:text]=>["otherrole1","otherrole2"] }
36
33
  def update_values(params={})
37
34
  # remove any terms from params that this datastream doesn't recognize
38
35
 
@@ -47,35 +44,45 @@ module OM::XML::TermValueOperators
47
44
  result = params.dup
48
45
 
49
46
  params.each_pair do |term_pointer,new_values|
47
+ raise ArgumentError, "The new_values passed to update_values must be an array or string. You provided #{new_values}." unless [Array, String, NilClass, Symbol].include?(new_values.class)
50
48
  pointer = OM.destringify(term_pointer)
51
49
  template_pointer = OM.pointers_to_flat_array(pointer,false)
52
50
  hn = OM::XML::Terminology.term_hierarchical_name(*pointer)
53
51
 
54
52
  term = self.class.terminology.retrieve_term( *OM.pointers_to_flat_array(pointer,false) )
55
53
 
54
+ xpath = self.class.terminology.xpath_with_indexes(*pointer)
55
+ current_values = term_values(*pointer)
56
+
57
+ if new_values.is_a?(Array) && current_values.length > new_values.length
58
+ starting_index = new_values.length + 1
59
+ starting_index.upto(current_values.size).each do |index|
60
+ term_value_delete select: xpath, child_index: index
61
+ end
62
+ end
63
+
56
64
  # Sanitize new_values to always be a hash with indexes
57
65
  new_values = term.sanitize_new_values(new_values)
58
-
66
+
67
+
59
68
  # Populate the response hash appropriately, using hierarchical names for terms as keys rather than the given pointers.
60
69
  result.delete(term_pointer)
61
70
  result[hn] = new_values.dup
62
71
 
63
72
  # Skip any submitted values if the new value matches the current values
64
- current_values = term_values(*pointer)
65
73
  new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
66
74
  z = new_values[y]
67
- if current_values[y.to_i]==z and y.to_i > -1
75
+ if !z.nil? && current_values[y.to_i]==z and y.to_i > -1
68
76
  new_values.delete(y)
69
77
  end
70
78
  end
71
-
79
+
72
80
  # Fill out the pointer completely if the final term is a NamedTermProxy
73
81
  if term.kind_of? OM::XML::NamedTermProxy
74
82
  pointer.pop
75
83
  pointer = pointer.concat(term.proxy_pointer)
76
84
  end
77
85
 
78
- xpath = self.class.terminology.xpath_with_indexes(*pointer)
79
86
  parent_pointer = pointer.dup
80
87
  parent_pointer.pop
81
88
  parent_xpath = self.class.terminology.xpath_with_indexes(*parent_pointer)
@@ -206,17 +206,24 @@ module OM::XML::TermXpathGenerator
206
206
  if query_constraints.kind_of?(Hash)
207
207
  contains_functions = []
208
208
  query_constraints.each_pair do |k,v|
209
- if k.instance_of?(Symbol)
210
- constraint_path = final_term.children[k].xpath_relative
209
+
210
+ if v.is_a?(Integer) || v == "0" || v.to_i != 0
211
+ # lookup sub element
212
+ xpath = '(' + xpath + '/' + final_term.children[query_constraints.keys.first].xpath_relative + ")[#{v.to_i + 1}]"
211
213
  else
212
- constraint_path = k
214
+ if k.instance_of?(Symbol)
215
+ constraint_path = final_term.children[k].xpath_relative
216
+ else
217
+ constraint_path = k
218
+ end
219
+ # match for text
220
+ contains_functions << "#{constraint_path}[text()=\"#{v}\"]"
221
+ xpath = add_predicate(xpath, delimited_list(contains_functions, " and ") )
213
222
  end
214
- contains_functions << "contains(#{constraint_path}, \"#{v}\")"
215
223
  end
216
224
 
217
- xpath = add_predicate(xpath, delimited_list(contains_functions, " and ") )
218
225
  end
219
-
226
+ #
220
227
  return xpath
221
228
  end
222
229
 
@@ -222,6 +222,8 @@ class OM::XML::Terminology
222
222
  # Use the current terminology to generate an xpath with (optional) node indexes for each of the term pointers.
223
223
  # Ex. terminology.xpath_with_indexes({:conference=>0}, {:role=>1}, :text )
224
224
  # will yield an xpath like this: '//oxns:name[@type="conference"][1]/oxns:role[2]/oxns:roleTerm[@type="text"]'
225
+ # Ex. terminology.xpath_with_indexes({:conference=>0}, {:role=>1}, {:text => 0} )
226
+ # will yield an xpath like this: '//oxns:name[@type="conference"][1]/oxns:role[2]/oxns:roleTerm[@type="text"][1]'
225
227
  def xpath_with_indexes(*pointers)
226
228
  OM::XML::TermXpathGenerator.generate_xpath_with_indexes(self, *pointers)
227
229
  end
@@ -126,7 +126,7 @@ describe "OM::XML::DynamicNode" do
126
126
  end
127
127
 
128
128
  it "Should be addressable to a specific node" do
129
- @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>{"0"=>"434"} })
129
+ @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>"434"})
130
130
 
131
131
  @article.subject.topic(1).to_pointer == [:subject, {:topic => 1}]
132
132
  @article.journal(0).issue.length.should == 2
@@ -141,7 +141,7 @@ describe "OM::XML::DynamicNode" do
141
141
 
142
142
  describe ".nodeset" do
143
143
  it "should return a Nokogiri NodeSet" do
144
- @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>{"0"=>"434"} })
144
+ @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>"434" })
145
145
  nodeset = @article.journal(0).issue(1).pages.start.nodeset
146
146
  nodeset.should be_kind_of Nokogiri::XML::NodeSet
147
147
  nodeset.length.should == @article.journal(0).issue(1).pages.start.length
@@ -156,6 +156,11 @@ describe "OM::XML::DynamicNode" do
156
156
  @article.should be_changed
157
157
  end
158
158
 
159
+ it "should remove extra nodes if fewer are given than currently exist" do
160
+ @article.journal.title_info = %W(one two three four five)
161
+ @article.journal.title_info = %W(six seven)
162
+ @article.journal.title_info.should == ["six", "seven"]
163
+ end
159
164
  end
160
165
  end
161
166
  end
@@ -18,55 +18,53 @@ describe "OM::XML::TermValueOperators" do
18
18
  end
19
19
 
20
20
  it "should respond with a hash of updated values and their indexes" do
21
- test_args = {[{"person"=>"0"},"description"]=>{"-1"=>"mork", "1"=>"york"}}
21
+ test_args = {[{"person"=>"0"},"description"]=>["mork", "york"]}
22
22
  result = @article.update_values(test_args)
23
23
  result.should == {"person_0_description"=>{"0"=>"mork","1"=>"york"}}
24
24
  end
25
25
 
26
- it "should update the xml in the specified datatsream and save those changes to Fedora" do
26
+ it "should update the xml in the specified datastream and know that changes have been made" do
27
27
  @article.term_values({:person=>0}, :first_name).should == ["GIVEN NAMES"]
28
- test_args = {[{:person=>0}, :first_name]=>{"0"=>"Replacement FirstName"}}
28
+ test_args = {[{:person=>0}, :first_name]=>"Replacement FirstName"}
29
29
  @article.update_values(test_args)
30
30
  @article.term_values({:person=>0}, :first_name).should == ["Replacement FirstName"]
31
31
  @article.should be_changed
32
32
  end
33
33
 
34
34
  it "should update the xml according to the find_by_terms_and_values in the given hash" do
35
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
36
- result = @article.update_values(terms_update_hash)
35
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
36
+ result = @article.update_values(terms_attributes)
37
37
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
38
38
 
39
39
  # Trying again with a more complex update hash
40
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}, [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
41
- result = @article.update_values(terms_update_hash)
40
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"], [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
41
+ result = @article.update_values(terms_attributes)
42
42
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}, "person_1_last_name"=>{"0"=>"Andronicus"},"person_1_first_name"=>{"0"=>"Titus"}, "person_1_role"=>{"0"=>"otherrole1","1"=>"otherrole2"}}
43
43
  @article.should be_changed
44
44
  end
45
45
 
46
46
  it "should work when you re-run the command" do
47
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
48
- result = @article.update_values(terms_update_hash)
47
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
48
+ result = @article.update_values(terms_attributes)
49
49
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
50
50
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
51
51
 
52
- # result = @article.update_values(terms_update_hash)
53
- # result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
54
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
52
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
55
53
  @article = OM::Samples::ModsArticle.from_xml( fixture( File.join("mods_articles","hydrangea_article1.xml") ) )
56
- result = @article.update_values(terms_update_hash)
54
+ result = @article.update_values(terms_attributes)
57
55
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
58
56
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
59
- result = @article.update_values(terms_update_hash)
57
+ result = @article.update_values(terms_attributes)
60
58
 
61
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
59
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
62
60
  @article = OM::Samples::ModsArticle.from_xml( fixture( File.join("mods_articles","hydrangea_article1.xml") ) )
63
- result = @article.update_values(terms_update_hash)
61
+ result = @article.update_values(terms_attributes)
64
62
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
65
63
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
66
64
 
67
65
  # Trying again with a more complex update hash
68
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}, [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
69
- result = @article.update_values(terms_update_hash)
66
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"], [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
67
+ result = @article.update_values(terms_attributes)
70
68
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}, "person_1_last_name"=>{"0"=>"Andronicus"},"person_1_first_name"=>{"0"=>"Titus"}, "person_1_role"=>{"0"=>"otherrole1","1"=>"otherrole2"}}
71
69
  @article.should be_changed
72
70
  end
@@ -17,6 +17,11 @@ describe "OM::XML::TermValueOperators" do
17
17
  expected_values.each {|v| result.should include(v)}
18
18
  end
19
19
 
20
+ it "should look at the index" do
21
+ result = @sample.term_values(:role, {:text => 3})
22
+ result.should == ['visionary']
23
+ end
24
+
20
25
  it "should ignore whitespace elements for a term pointing to a text() node for an element that contains children" do
21
26
  @article.term_values(:name, :name_content).should == ["Describes a person"]
22
27
  end
@@ -26,8 +31,8 @@ describe "OM::XML::TermValueOperators" do
26
31
 
27
32
  describe ".update_values" do
28
33
  it "should update the xml according to the find_by_terms_and_values in the given hash" do
29
- terms_update_hash = {[{":person"=>"0"}, "affiliation"]=>{"0"=>"affiliation1", "1"=>"affiliation2".freeze, "2"=>"affiliation3"}, [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
30
- result = @article.update_values(terms_update_hash)
34
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2".freeze, "affiliation3"], [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
35
+ result = @article.update_values(terms_attributes)
31
36
  result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}, "person_1_last_name"=>{"0"=>"Andronicus"},"person_1_first_name"=>{"0"=>"Titus"}, "person_1_role"=>{"0"=>"otherrole1","1"=>"otherrole2"}}
32
37
  person_0_affiliation = @article.find_by_terms({:person=>0}, :affiliation)
33
38
  person_0_affiliation[0].text.should == "affiliation1"
@@ -47,8 +52,9 @@ describe "OM::XML::TermValueOperators" do
47
52
  end
48
53
 
49
54
  it "should allow setting a blank string " do
50
- @article.update_values({[:title_info, :main_title]=>['']})
51
- @article.term_values(:title_info, :main_title).should == ["", "Artikkelin otsikko Hydrangea artiklan 1", "TITLE OF HOST JOURNAL"] end
55
+ @article.update_values([:abstract]=>[''])
56
+ @article.term_values(:abstract).should == [""]
57
+ end
52
58
 
53
59
  it "should call term_value_update if the corresponding node already exists" do
54
60
  @article.should_receive(:term_value_update).with('//oxns:titleInfo/oxns:title', 0, "My New Title")
@@ -64,28 +70,27 @@ describe "OM::XML::TermValueOperators" do
64
70
  :values => "My New Role"
65
71
  }
66
72
  @article.should_receive(:term_values_append).with(expected_args).twice
67
- @article.update_values( {[{:person=>0}, :role] => {"6"=>"My New Role"}} )
68
- @article.update_values( {[{:person=>0}, :role] => {"-1"=>"My New Role"}} )
73
+ @article.update_values( {[{:person=>0}, {:role => 6}] => "My New Role"} )
74
+ @article.update_values( {[{:person=>0}, {:role => 7}] => "My New Role"} )
69
75
  end
70
76
 
71
77
  it "should support updating attribute values" do
72
78
  pointer = [:title_info, :language]
73
79
  test_val = "language value"
74
- @article.update_values( {pointer=>{"0"=>test_val}} )
80
+ @article.update_values( {pointer=>test_val} )
75
81
  @article.term_values(*pointer).first.should == test_val
76
82
  end
77
83
 
78
84
  it "should not get tripped up on root nodes" do
79
- @article.update_values([:title_info]=>{"0"=>"york", "1"=>"mangle","2"=>"mork"})
85
+ @article.update_values([:title_info]=>["york", "mangle","mork"])
80
86
  @article.term_values(*[:title_info]).should == ["york", "mangle", "mork"]
81
87
  end
82
88
 
83
89
  it "should destringify the field key/find_by_terms_and_value pointer" do
84
- OM::Samples::ModsArticle.terminology.should_receive(:xpath_with_indexes).with( *[{:person=>0}, :role]).exactly(10).times.and_return("//oxns:name[@type=\"personal\"][1]/oxns:role")
85
- OM::Samples::ModsArticle.terminology.stub(:xpath_with_indexes).with( *[{:person=>0}]).and_return("//oxns:name[@type=\"personal\"][1]")
86
- @article.update_values( { [{":person"=>"0"}, "role"]=>"the role" } )
87
- @article.update_values( { [{"person"=>"0"}, "role"]=>"the role" } )
88
- @article.update_values( { [{:person=>0}, :role]=>"the role" } )
90
+ expected_result = {"person_0_role"=>{"0"=>"the role"}}
91
+ @article.update_values( { [{":person"=>"0"}, "role"]=>"the role" }).should == expected_result
92
+ @article.update_values( { [{"person"=>"0"}, "role"]=>"the role" }).should == expected_result
93
+ @article.update_values( { [{:person=>0}, :role]=>"the role" }).should == expected_result
89
94
  end
90
95
 
91
96
  it "should traverse named term proxies transparently" do
@@ -104,7 +109,7 @@ describe "OM::XML::TermValueOperators" do
104
109
  end
105
110
 
106
111
  it "should create deep trees of ancestor nodes" do
107
- result = @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>{"0"=>"434"} })
112
+ result = @article.update_values( {[{:journal=>0}, {:issue=>3}, :pages, :start]=>"434" })
108
113
  @article.find_by_terms({:journal=>0}, :issue).length.should == 2
109
114
  @article.find_by_terms({:journal=>0}, {:issue=>1}, :pages).length.should == 1
110
115
  @article.find_by_terms({:journal=>0}, {:issue=>1}, :pages, :start).length.should == 1
@@ -143,7 +148,7 @@ describe "OM::XML::TermValueOperators" do
143
148
  ### Examples copied over form nokogiri_datastream_spec
144
149
 
145
150
  it "should apply submitted hash to corresponding datastream field values" do
146
- result = @article.update_values( {[{":person"=>"0"}, "first_name"]=>{"0"=>"Billy", "1"=>"Bob", "2"=>"Joe"} })
151
+ result = @article.update_values( {[{":person"=>"0"}, "first_name"]=>["Billy", "Bob", "Joe"] })
147
152
  result.should == {"person_0_first_name"=>{"0"=>"Billy", "1"=>"Bob", "2"=>"Joe"}}
148
153
  # xpath = ds.class.xpath_with_indexes(*field_key)
149
154
  # result = ds.term_values(xpath)
@@ -169,73 +174,66 @@ describe "OM::XML::TermValueOperators" do
169
174
  end
170
175
 
171
176
  it "should work for text fields" do
172
- att= {[{"person"=>"0"},"description"]=>{"-1"=>"mork", "1"=>"york"}}
177
+ att= {[{"person"=>"0"},"description"]=>["mork", "york"]}
173
178
  result = @article.update_values(att)
174
179
  result.should == {"person_0_description"=>{"0"=>"mork","1"=>"york"}}
175
180
  @article.term_values({:person=>0},:description).should == ['mork', 'york']
176
- att= {[{"person"=>"0"},"description"]=>{"-1"=>"dork"}}
181
+ att= {[{"person"=>"0"},{"description" => 2}]=>"dork"}
177
182
  result2 = @article.update_values(att)
178
- result2.should == {"person_0_description"=>{"2"=>"dork"}}
183
+ result2.should == {"person_0_description_2"=>{"0"=>"dork"}}
179
184
  @article.term_values({:person=>0},:description).should == ['mork', 'york', 'dork']
180
185
  end
181
186
 
182
- it "should return the new index of any added values" do
183
- @article.term_values({:title_info=>0},:main_title).should == ["ARTICLE TITLE HYDRANGEA ARTICLE 1", "TITLE OF HOST JOURNAL"]
184
- result = @article.update_values [{"title_info"=>"0"},"main_title"]=>{"-1"=>"mork"}
185
- result.should == {"title_info_0_main_title"=>{"2"=>"mork"}}
186
- end
187
-
188
- it "should return accurate response when multiple values have been added in a single run" do
189
- pending "THIS SHOULD BE FIXED"
190
- att= {[:journal, :title_info]=>{"-1"=>"mork", "0"=>"york"}}
191
- @article.update_values(att).should == {"journal_title_info"=>{"0"=>"york", "1"=>"mork"}}
192
- @article.term_values(*att.keys.first).should == ["york", "mork"]
193
- end
194
-
195
187
  it "should append nodes at the specified index if possible" do
196
188
  @article.update_values([:journal, :title_info]=>["all", "for", "the"])
197
- att = {[:journal, :title_info]=>{"3"=>'glory'}}
189
+ att = {[:journal, {:title_info => 3}]=>'glory'}
198
190
  result = @article.update_values(att)
199
- result.should == {"journal_title_info"=>{"3"=>"glory"}}
191
+ result.should == {"journal_title_info_3"=>{"0"=>"glory"}}
200
192
  @article.term_values(:journal, :title_info).should == ["all", "for", "the", "glory"]
201
193
  end
194
+
195
+ it "should remove extra nodes if fewer are given than currently exist" do
196
+ @article.update_values([:journal, :title_info]=>%W(one two three four five))
197
+ result = @article.update_values({[:journal, :title_info]=>["six", "seven"]})
198
+ @article.term_values(:journal, :title_info).should == ["six", "seven"]
199
+ end
202
200
 
203
201
  it "should append values to the end of the array if the specified index is higher than the length of the values array" do
204
- att = {[:journal, :issue, :pages, :end]=>{"3"=>'108'}}
202
+ att = {[:journal, :issue, :pages, {:end => 3}]=>'108'}
205
203
  @article.term_values(:journal, :issue, :pages, :end).should == []
206
204
  result = @article.update_values(att)
207
- result.should == {"journal_issue_pages_end"=>{"0"=>"108"}}
205
+ result.should == {"journal_issue_pages_end_3"=>{"-1"=>"108"}}
208
206
  @article.term_values(:journal, :issue, :pages, :end).should == ["108"]
209
207
  end
210
208
 
211
209
  it "should allow deleting of values and should delete values so that to_xml does not return emtpy nodes" do
212
- att= {[:journal, :title_info]=>{"0"=>"york", "1"=>"mangle","2"=>"mork"}}
210
+ att= {[:journal, :title_info]=>["york", "mangle","mork"]}
213
211
  @article.update_values(att)
214
212
  @article.term_values(:journal, :title_info).should == ['york', 'mangle', 'mork']
215
213
 
216
- @article.update_values({[:journal, :title_info]=>{"1"=>nil}})
214
+ @article.update_values({[:journal, {:title_info => 1}]=>nil})
217
215
  @article.term_values(:journal, :title_info).should == ['york', 'mork']
218
216
 
219
- @article.update_values({[:journal, :title_info]=>{"0"=>:delete}})
217
+ @article.update_values({[:journal, {:title_info => 0}]=>:delete})
220
218
  @article.term_values(:journal, :title_info).should == ['mork']
221
219
  end
222
220
 
223
221
  describe "delete_on_update?" do
224
222
 
225
223
  before(:each) do
226
- att= {[:journal, :title_info]=>{"0"=>"york", "1"=>"mangle","2"=>"mork"}}
224
+ att= {[:journal, :title_info]=>["york", "mangle","mork"]}
227
225
  @article.update_values(att)
228
226
  @article.term_values(:journal, :title_info).should == ['york', 'mangle', 'mork']
229
227
  end
230
228
 
231
- it "by default, setting to empty string deletes the node" do
232
- @article.update_values({[:journal, :title_info]=>{"1"=>nil}})
229
+ it "by default, setting to nil deletes the node" do
230
+ @article.update_values({[:journal, {:title_info => 1}]=>nil})
233
231
  @article.term_values(:journal, :title_info).should == ['york', 'mork']
234
232
  end
235
233
 
236
234
  it "if delete_on_update? returns false, setting to nil won't delete node" do
237
235
  @article.stub('delete_on_update?').and_return(false)
238
- @article.update_values({[:journal, :title_info]=>{"1"=>""}})
236
+ @article.update_values({[:journal, {:title_info => 1}]=>""})
239
237
  @article.term_values(:journal, :title_info).should == ['york', '', 'mork']
240
238
  end
241
239
 
@@ -395,7 +393,7 @@ describe "OM::XML::TermValueOperators" do
395
393
  1.should == 2
396
394
  end
397
395
  it "should delete nodes if value is :delete or nil" do
398
- @article.update_values([:title_info]=>{"0"=>"york", "1"=>"mangle","2"=>"mork"})
396
+ @article.update_values([:title_info]=>["york", "mangle","mork"])
399
397
  xpath = @article.class.terminology.xpath_for(:title_info)
400
398
 
401
399
  @article.term_value_update([:title_info], 1, nil)
@@ -405,7 +403,7 @@ describe "OM::XML::TermValueOperators" do
405
403
  @article.term_values(:title_info).should == ['york']
406
404
  end
407
405
  it "should create empty nodes if value is empty string" do
408
- @article.update_values([:title_info]=>{"0"=>"york", "1"=>'',"2"=>"mork"})
406
+ @article.update_values([:title_info]=>["york", '', "mork"])
409
407
  @article.term_values(:title_info).should == ['york', "", "mork"]
410
408
  end
411
409
  end
@@ -89,11 +89,38 @@ describe "OM::XML::TermXpathGeneratorSpec" do
89
89
 
90
90
  describe "generate_xpath_with_indexes" do
91
91
  it "should accept multiple constraints" do
92
- generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology, :person, {:first_name=>"Tim", :family_name=>"Berners-Lee"} )
92
+ generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology,
93
+ :person, {:first_name=>"Tim", :family_name=>"Berners-Lee"} )
93
94
  # expect an xpath that looks like this: '//oxns:name[@type="personal" and contains(oxns:namePart[@type="family"], "Berners-Lee") and contains(oxns:namePart[@type="given"], "Tim")]'
94
- # can't use string comparison because the contains functions can arrive in any order
95
- generated_xpath.should match( /\/\/oxns:name\[@type=\"personal\".*and contains\(oxns:namePart\[@type=\"given\"\], \"Tim\"\).*\]/ )
96
- generated_xpath.should match( /\/\/oxns:name\[@type=\"personal\".*and contains\(oxns:namePart\[@type=\"family\"\], \"Berners-Lee\"\).*\]/ )
95
+ generated_xpath.should match( /\/\/oxns:name\[@type=\"personal\".*and oxns:namePart\[@type=\"given\"\]\[text\(\)=\"Tim\"\].*\]/ )
96
+ generated_xpath.should match( /\/\/oxns:name\[@type=\"personal\".*and oxns:namePart\[@type=\"family\"\]\[text\(\)=\"Berners-Lee\"\].*\]/ )
97
+ end
98
+
99
+ it "should find matching nodes" do
100
+ ng = Nokogiri::XML(fixture( File.join("test_dummy_mods.xml")))
101
+ generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology,
102
+ :person, {:first_name=>"Tim", :family_name=>"Berners-Lee"} )
103
+ ng.xpath(generated_xpath, 'oxns' => "http://www.loc.gov/mods/v3").to_xml.should be_equivalent_to <<EOF
104
+ <ns3:name type="personal">
105
+ <ns3:namePart type="family">Berners-Lee</ns3:namePart>
106
+ <ns3:namePart type="given">Tim</ns3:namePart>
107
+ <ns3:role>
108
+ <ns3:roleTerm type="text" authority="marcrelator">creator</ns3:roleTerm>
109
+ <ns3:roleTerm type="code" authority="marcrelator">cre</ns3:roleTerm>
110
+ </ns3:role>
111
+ </ns3:name>
112
+ EOF
113
+ generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology,
114
+ :person, {:first_name=>"Tim", :family_name=>"Berners"} )
115
+ ng.xpath(generated_xpath, 'oxns' => "http://www.loc.gov/mods/v3").should be_empty
116
+ generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology,
117
+ :person, {:first_name=>"Frank", :family_name=>"Berners-Lee"} )
118
+ ng.xpath(generated_xpath, 'oxns' => "http://www.loc.gov/mods/v3").should be_empty
119
+
120
+ generated_xpath = OM::XML::TermXpathGenerator.generate_xpath_with_indexes( @sample_terminology,
121
+ :person, {:first_name=>"Tim", :family_name=>"Howard"} )
122
+ ng.xpath(generated_xpath, 'oxns' => "http://www.loc.gov/mods/v3").should be_empty
123
+
97
124
  end
98
125
  it "should support xpath queries as argument" do
99
126
  OM::XML::TermXpathGenerator.generate_xpath_with_indexes(@sample_terminology, '//oxns:name[@type="personal"][1]/oxns:namePart').should == '//oxns:name[@type="personal"][1]/oxns:namePart'
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: 2.2.1
4
+ version: 3.0.0.beta1
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: 2013-06-21 00:00:00.000000000 Z
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -293,9 +293,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
293
293
  version: 1.9.3
294
294
  required_rubygems_version: !ruby/object:Gem::Requirement
295
295
  requirements:
296
- - - '>='
296
+ - - '>'
297
297
  - !ruby/object:Gem::Version
298
- version: '0'
298
+ version: 1.3.1
299
299
  requirements: []
300
300
  rubyforge_project:
301
301
  rubygems_version: 2.0.3