om 3.0.0.beta1 → 3.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 311c97a223b0160f5766d16da7ec636a85073c39
4
- data.tar.gz: 907162fe5292c1316732f68be31d987a3892e402
3
+ metadata.gz: 2c2d373dd7d3fef0f2cf3d23de1aea05cc4de921
4
+ data.tar.gz: 66b678101c0255b7b9c94e81987f30329eca32d8
5
5
  SHA512:
6
- metadata.gz: 249f1349616500b6fbf03cb8d2168fe87e19ea2084f29a77ea5a15996f349ed4b71a84c17a0b9b636f70f522d89f8be4d5f2dda3b3d41d442d5e4eea1830586e
7
- data.tar.gz: 10386c58e84a42859682dc6e70b9fe11c548befbaec48185194d7c4897803d6ab8e5876ac4d5c6f57b62b943a34e5419502fc99575b8d4ed1b1fbc8fd82b5ebe
6
+ metadata.gz: 8b074e1d3cd7ff1a25b7257679519a1d9e0913057ab8b0c4182cd90067513c8d01753b3e27e1324d058b92cff6462f4b072d6c481aca067c344368de5cb41290
7
+ data.tar.gz: 701ea9a57f9e17410dfe9c50af40ed425df1f35f676d37e292ad625c6b076034f14267859478cfb34569eb162d7ef27b6136b3e6356152a0d187b89ac1e30a65
data/lib/om/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Om
2
- VERSION = "3.0.0.beta1"
2
+ VERSION = "3.0.0.beta2"
3
3
  end
@@ -69,7 +69,6 @@ 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)
73
72
  new_values = term.sanitize_new_values(args.first)
74
73
  existing_nodes = @document.find_by_xpath(xpath)
75
74
  if existing_nodes.length > new_values.length
@@ -78,10 +77,9 @@ module OM
78
77
  @document.term_value_delete select: xpath, child_index: index
79
78
  end
80
79
  end
81
- new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
82
- z = new_values[y]
80
+ new_values.each_with_index do |z, y|
83
81
  ## If we pass something that already has an index on it, we should be able to add it.
84
- if existing_nodes[y.to_i].nil? || y.to_i == -1
82
+ if existing_nodes[y.to_i].nil?
85
83
  parent_pointer = parent ? parent.to_pointer : nil
86
84
  @document.term_values_append(:parent_select=> parent_pointer,:parent_index=>0,:template=>to_pointer,:values=>z)
87
85
  else
data/lib/om/xml/term.rb CHANGED
@@ -78,15 +78,12 @@ class OM::XML::Term
78
78
  # Sanitize new_values to always be a hash with indexes
79
79
  case new_values
80
80
  when Hash
81
- new_values.each {|k, v| v = serialize(v) }
81
+ sanitize_new_values(new_values.values)
82
82
  when Array
83
- nv = new_values.dup
84
- new_values = {}
85
- nv.each {|v| new_values[nv.index(v).to_s] = serialize(v)}
83
+ new_values.map {|v| serialize(v)}
86
84
  else
87
- new_values = {"0"=>serialize(new_values)}
85
+ [serialize(new_values)]
88
86
  end
89
- new_values
90
87
  end
91
88
 
92
89
  # @param val [String,Date,Integer]
@@ -29,7 +29,7 @@ module OM::XML::TermValueOperators
29
29
  #
30
30
  # @param [Hash] params
31
31
  # @example
32
- # {[{":person"=>"0"}, "role", "text"]=>["role1", "role2", "role3"], [{:person=>1}, :family_name]=>"Andronicus", [{"person"=>"1"},:given_name]=>["Titus"],[{:person=>1},:role,:text]=>["otherrole1","otherrole2"] }
32
+ # {[{":person"=>"0"}, "role", "text"]=>{'1'=>"role1", '2' => "role2", '3'=>"role3"}, [{:person=>1}, :family_name]=>"Andronicus", [{"person"=>"1"},:given_name]=>["Titus"],[{:person=>1},:role,:text]=>["otherrole1","otherrole2"] }
33
33
  def update_values(params={})
34
34
  # remove any terms from params that this datastream doesn't recognize
35
35
 
@@ -41,39 +41,58 @@ module OM::XML::TermValueOperators
41
41
  end
42
42
  end
43
43
 
44
- result = params.dup
45
-
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)
44
+ params.inject({}) do |result, (term_pointer,new_values)|
48
45
  pointer = OM.destringify(term_pointer)
49
- template_pointer = OM.pointers_to_flat_array(pointer,false)
50
46
  hn = OM::XML::Terminology.term_hierarchical_name(*pointer)
47
+ result[hn] = assign_nested_attributes_for_collection_association(pointer, new_values)
48
+ result
49
+ end
50
+ end
51
+
52
+
53
+ # @see ActiveRecord::NestedAttributes#assign_nested_attributes_for_collection_association
54
+ # @example
55
+ #
56
+ # assign_nested_attributes_for_collection_association(:people, {
57
+ # '1' => { name: 'Peter' },
58
+ # '2' => { name: 'John', role: 'Creator' },
59
+ # '3' => { name: 'Bess' }
60
+ # })
61
+ #
62
+ # Will update the name of the Person with ID 1, build a new associated
63
+ # person with the name 'John', and mark the associated Person with ID 2
64
+ # for destruction.
65
+ #
66
+ # Also accepts an Array of attribute hashes:
67
+ #
68
+ # assign_nested_attributes_for_collection_association(:people, [
69
+ # { name: 'Peter' },
70
+ # { name: 'John', role: 'Creator' },
71
+ # { name: 'Bess' }
72
+ # ])
73
+ def assign_nested_attributes_for_collection_association(pointer, new_values)
51
74
 
52
75
  term = self.class.terminology.retrieve_term( *OM.pointers_to_flat_array(pointer,false) )
53
-
54
76
  xpath = self.class.terminology.xpath_with_indexes(*pointer)
55
77
  current_values = term_values(*pointer)
56
78
 
57
- if new_values.is_a?(Array) && current_values.length > new_values.length
79
+ new_values = term.sanitize_new_values(new_values)
80
+
81
+
82
+ if current_values.length > new_values.length
58
83
  starting_index = new_values.length + 1
59
84
  starting_index.upto(current_values.size).each do |index|
60
85
  term_value_delete select: xpath, child_index: index
61
86
  end
62
87
  end
63
88
 
64
- # Sanitize new_values to always be a hash with indexes
65
- new_values = term.sanitize_new_values(new_values)
66
-
67
-
68
89
  # Populate the response hash appropriately, using hierarchical names for terms as keys rather than the given pointers.
69
- result.delete(term_pointer)
70
- result[hn] = new_values.dup
90
+ result = new_values.dup
71
91
 
72
92
  # Skip any submitted values if the new value matches the current values
73
- new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
74
- z = new_values[y]
75
- if !z.nil? && current_values[y.to_i]==z and y.to_i > -1
76
- new_values.delete(y)
93
+ new_values.each_with_index do |val, index|
94
+ if !val.nil? && current_values[index] == val
95
+ new_values.delete_at(index)
77
96
  end
78
97
  end
79
98
 
@@ -86,22 +105,22 @@ module OM::XML::TermValueOperators
86
105
  parent_pointer = pointer.dup
87
106
  parent_pointer.pop
88
107
  parent_xpath = self.class.terminology.xpath_with_indexes(*parent_pointer)
108
+
109
+ template_pointer = OM.pointers_to_flat_array(pointer,false)
89
110
 
90
111
  # If the value doesn't exist yet, append it. Otherwise, update the existing value.
91
- new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
92
- z = new_values[y]
93
- if find_by_terms(*pointer)[y.to_i].nil? || y.to_i == -1
94
- result[hn].delete(y)
112
+ new_values.each_with_index do |z, y|
113
+ if find_by_terms(*pointer)[y.to_i].nil?
114
+ result.delete(y)
95
115
  term_values_append(:parent_select=>parent_pointer,:parent_index=>0,:template=>template_pointer,:values=>z)
96
- # term_values_append(:parent_select=>parent_xpath,:parent_index=>0,:template=>template_pointer,:values=>z)
97
116
  new_array_index = find_by_terms(*pointer).length - 1
98
- result[hn][new_array_index.to_s] = z
117
+ result[new_array_index] = z
99
118
  else
100
119
  term_value_update(xpath, y.to_i, z)
101
120
  end
102
121
  end
103
- end
104
- return result
122
+
123
+ return result
105
124
  end
106
125
 
107
126
  def term_values_append(opts={})
@@ -20,7 +20,7 @@ describe "OM::XML::TermValueOperators" do
20
20
  it "should respond with a hash of updated values and their indexes" do
21
21
  test_args = {[{"person"=>"0"},"description"]=>["mork", "york"]}
22
22
  result = @article.update_values(test_args)
23
- result.should == {"person_0_description"=>{"0"=>"mork","1"=>"york"}}
23
+ result.should == {"person_0_description"=>["mork", "york"]}
24
24
  end
25
25
 
26
26
  it "should update the xml in the specified datastream and know that changes have been made" do
@@ -34,12 +34,12 @@ describe "OM::XML::TermValueOperators" do
34
34
  it "should update the xml according to the find_by_terms_and_values in the given hash" do
35
35
  terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
36
36
  result = @article.update_values(terms_attributes)
37
- result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
37
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"]}
38
38
 
39
39
  # Trying again with a more complex update hash
40
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
41
  result = @article.update_values(terms_attributes)
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"}}
42
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"], "person_1_last_name"=>["Andronicus"],"person_1_first_name"=>["Titus"], "person_1_role"=>["otherrole1","otherrole2"]}
43
43
  @article.should be_changed
44
44
  end
45
45
 
@@ -47,25 +47,25 @@ describe "OM::XML::TermValueOperators" do
47
47
  terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
48
48
  result = @article.update_values(terms_attributes)
49
49
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
50
- result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
50
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"]}
51
51
 
52
52
  terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
53
53
  @article = OM::Samples::ModsArticle.from_xml( fixture( File.join("mods_articles","hydrangea_article1.xml") ) )
54
54
  result = @article.update_values(terms_attributes)
55
55
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
56
- result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
56
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"]}
57
57
  result = @article.update_values(terms_attributes)
58
58
 
59
59
  terms_attributes = {[{":person"=>"0"}, "affiliation"]=>["affiliation1", "affiliation2", "affiliation3"]}
60
60
  @article = OM::Samples::ModsArticle.from_xml( fixture( File.join("mods_articles","hydrangea_article1.xml") ) )
61
61
  result = @article.update_values(terms_attributes)
62
62
  @article.term_values( {":person"=>"0"}, "affiliation" ).should == ["affiliation1", "affiliation2", "affiliation3"]
63
- result.should == {"person_0_affiliation"=>{"0"=>"affiliation1", "1"=>"affiliation2", "2"=>"affiliation3"}}
63
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"]}
64
64
 
65
65
  # Trying again with a more complex update hash
66
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
67
  result = @article.update_values(terms_attributes)
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"}}
68
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"], "person_1_last_name"=>["Andronicus"],"person_1_first_name"=>["Titus"], "person_1_role"=>["otherrole1", "otherrole2"]}
69
69
  @article.should be_changed
70
70
  end
71
71
  end
@@ -31,9 +31,9 @@ describe "OM::XML::TermValueOperators" do
31
31
 
32
32
  describe ".update_values" do
33
33
  it "should update the xml according to the find_by_terms_and_values in the given hash" do
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"] }
34
+ terms_attributes = {[{":person"=>"0"}, "affiliation"]=>{'1' => "affiliation1", '2'=> "affiliation2", '3' => "affiliation3"}, [{:person=>1}, :last_name]=>"Andronicus", [{"person"=>"1"},:first_name]=>["Titus"],[{:person=>1},:role]=>["otherrole1","otherrole2"] }
35
35
  result = @article.update_values(terms_attributes)
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"}}
36
+ result.should == {"person_0_affiliation"=>["affiliation1", "affiliation2", "affiliation3"], "person_1_last_name"=>["Andronicus"], "person_1_first_name"=>["Titus"], "person_1_role"=>["otherrole1","otherrole2"]}
37
37
  person_0_affiliation = @article.find_by_terms({:person=>0}, :affiliation)
38
38
  person_0_affiliation[0].text.should == "affiliation1"
39
39
  person_0_affiliation[1].text.should == "affiliation2"
@@ -87,7 +87,7 @@ describe "OM::XML::TermValueOperators" do
87
87
  end
88
88
 
89
89
  it "should destringify the field key/find_by_terms_and_value pointer" do
90
- expected_result = {"person_0_role"=>{"0"=>"the role"}}
90
+ expected_result = {"person_0_role"=>["the role"]}
91
91
  @article.update_values( { [{":person"=>"0"}, "role"]=>"the role" }).should == expected_result
92
92
  @article.update_values( { [{"person"=>"0"}, "role"]=>"the role" }).should == expected_result
93
93
  @article.update_values( { [{:person=>0}, :role]=>"the role" }).should == expected_result
@@ -149,7 +149,7 @@ describe "OM::XML::TermValueOperators" do
149
149
 
150
150
  it "should apply submitted hash to corresponding datastream field values" do
151
151
  result = @article.update_values( {[{":person"=>"0"}, "first_name"]=>["Billy", "Bob", "Joe"] })
152
- result.should == {"person_0_first_name"=>{"0"=>"Billy", "1"=>"Bob", "2"=>"Joe"}}
152
+ result.should == {"person_0_first_name"=>["Billy", "Bob", "Joe"]}
153
153
  # xpath = ds.class.xpath_with_indexes(*field_key)
154
154
  # result = ds.term_values(xpath)
155
155
  @article.term_values({:person=>0}, :first_name).should == ["Billy","Bob","Joe"]
@@ -158,7 +158,7 @@ describe "OM::XML::TermValueOperators" do
158
158
  it "should support single-value arguments (as opposed to a hash of values with array indexes as keys)" do
159
159
  # In other words, { [:journal, :title_info]=>"dork" } should have the same effect as { [:journal, :title_info]=>{"0"=>"dork"} }
160
160
  result = @article.update_values( { [{":person"=>"0"}, "role"]=>"the role" } )
161
- result.should == {"person_0_role"=>{"0"=>"the role"}}
161
+ result.should == {"person_0_role"=>["the role"]}
162
162
  @article.term_values({:person=>0}, :role).first.should == "the role"
163
163
  @article.term_values('//oxns:name[@type="personal"][1]/oxns:role').first.should == "the role"
164
164
  end
@@ -176,11 +176,11 @@ describe "OM::XML::TermValueOperators" do
176
176
  it "should work for text fields" do
177
177
  att= {[{"person"=>"0"},"description"]=>["mork", "york"]}
178
178
  result = @article.update_values(att)
179
- result.should == {"person_0_description"=>{"0"=>"mork","1"=>"york"}}
179
+ result.should == {"person_0_description"=>["mork", "york"]}
180
180
  @article.term_values({:person=>0},:description).should == ['mork', 'york']
181
181
  att= {[{"person"=>"0"},{"description" => 2}]=>"dork"}
182
182
  result2 = @article.update_values(att)
183
- result2.should == {"person_0_description_2"=>{"0"=>"dork"}}
183
+ result2.should == {"person_0_description_2"=>["dork"]}
184
184
  @article.term_values({:person=>0},:description).should == ['mork', 'york', 'dork']
185
185
  end
186
186
 
@@ -188,7 +188,7 @@ describe "OM::XML::TermValueOperators" do
188
188
  @article.update_values([:journal, :title_info]=>["all", "for", "the"])
189
189
  att = {[:journal, {:title_info => 3}]=>'glory'}
190
190
  result = @article.update_values(att)
191
- result.should == {"journal_title_info_3"=>{"0"=>"glory"}}
191
+ result.should == {"journal_title_info_3"=>["glory"]}
192
192
  @article.term_values(:journal, :title_info).should == ["all", "for", "the", "glory"]
193
193
  end
194
194
 
@@ -202,7 +202,7 @@ describe "OM::XML::TermValueOperators" do
202
202
  att = {[:journal, :issue, :pages, {:end => 3}]=>'108'}
203
203
  @article.term_values(:journal, :issue, :pages, :end).should == []
204
204
  result = @article.update_values(att)
205
- result.should == {"journal_issue_pages_end_3"=>{"-1"=>"108"}}
205
+ result.should == {"journal_issue_pages_end_3"=>["108"]}
206
206
  @article.term_values(:journal, :issue, :pages, :end).should == ["108"]
207
207
  end
208
208
 
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.0.0.beta1
4
+ version: 3.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zumwalt