om 0.1.2 → 0.1.3

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/History.textile CHANGED
@@ -1,3 +1,7 @@
1
+ 0.1.3
2
+
3
+ * added accessor_generic_name and accessor_hierarchical_name methods
4
+
1
5
  0.1.2
2
6
 
3
7
  * changed syntax for looking up accessors with (optional) index values -- no using [{:person=>1}, :first_name] instead of [:person, 1, :first_name]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -40,25 +40,22 @@ module OM::XML::Accessors
40
40
 
41
41
  # Returns the configuration info for the selected accessor.
42
42
  # Ingores any integers in the array (ie. nodeset indices intended for use in other accessor convenience methods)
43
- def accessor_info(*args)
43
+ def accessor_info(*pointers)
44
44
  info = @accessors
45
- args.each do |pointer|
46
-
47
- # Ignore any nodeset indexes in the args array
48
- if pointer.kind_of?(Hash)
49
- k = pointer.keys.first
50
- else
51
- k = pointer
52
- end
45
+
46
+ # flatten the pointers array, excluding node indices
47
+ pointers = pointers_to_flat_array(pointers, false)
48
+
49
+ pointers.each do |pointer|
53
50
 
54
- unless args.index(pointer) == 0
51
+ unless pointers.index(pointer) == 0
55
52
  info = info.fetch(:children, nil)
56
53
  if info.nil?
57
54
  return nil
58
55
  end
59
56
  end
60
57
 
61
- info = info.fetch(k, nil)
58
+ info = info.fetch(pointer, nil)
62
59
  if info.nil?
63
60
  return nil
64
61
  end
@@ -67,13 +64,11 @@ module OM::XML::Accessors
67
64
  end
68
65
 
69
66
 
70
- def accessor_xpath(*args)
67
+ def accessor_xpath(*pointers)
71
68
 
72
69
  keys = []
73
- # keys = even_values(args)
74
- # indices = odd_values(args)
75
70
  xpath = "//"
76
- args.each do |pointer|
71
+ pointers.each do |pointer|
77
72
 
78
73
  if pointer.kind_of?(Hash)
79
74
  k = pointer.keys.first
@@ -86,7 +81,7 @@ module OM::XML::Accessors
86
81
  keys << k
87
82
 
88
83
  # key_index = keys.index(k)
89
- pointer_index = args.index(pointer)
84
+ pointer_index = pointers.index(pointer)
90
85
  # accessor_info = accessor_info(*keys[0..key_index])
91
86
  accessor_info = accessor_info(*keys)
92
87
  relative_path = accessor_info[:relative_xpath]
@@ -129,12 +124,35 @@ module OM::XML::Accessors
129
124
  end
130
125
  end
131
126
 
132
- def odd_values(array)
133
- array.values_at(* array.each_index.select {|i| i.odd?})
127
+ def accessor_generic_name(*pointers)
128
+ pointers_to_flat_array(pointers, false).join("_")
134
129
  end
135
- def even_values(array)
136
- array.values_at(* array.each_index.select {|i| i.even?})
130
+
131
+ def accessor_hierarchical_name(*pointers)
132
+ pointers_to_flat_array(pointers, true).join("_")
137
133
  end
134
+
135
+ # @pointers pointers array that you would pass into other Accessor methods
136
+ # @include_indices (default: true) if set to false, parent indices will be excluded from the array
137
+ # Converts an array of accessor pointers into a flat array.
138
+ # ie. [{:conference=>0}, {:role=>1}, :text] becomes [:conference, 0, :role, 1, :text]
139
+ # if include_indices is set to false,
140
+ # [{:conference=>0}, {:role=>1}, :text] becomes [:conference, :role, :text]
141
+ def pointers_to_flat_array(pointers, include_indices=true)
142
+ flat_array = []
143
+ pointers.each do |pointer|
144
+ if pointer.kind_of?(Hash)
145
+ flat_array << pointer.keys.first
146
+ if include_indices
147
+ flat_array << pointer.values.first
148
+ end
149
+ else
150
+ flat_array << pointer
151
+ end
152
+ end
153
+ return flat_array
154
+ end
155
+
138
156
  end
139
157
 
140
158
  # Instance Methods -- These methods will be available on instances of OM classes (ie. the actual xml documents)
@@ -143,11 +161,11 @@ module OM::XML::Accessors
143
161
  klass.extend(ClassMethods)
144
162
  end
145
163
 
146
- # *args Variable length array of values in format [:accessor_name, index, :accessor_name ...]
164
+ # *pointers Variable length array of values in format [:accessor_name, :accessor_name ...] or [{:accessor_name=>index}, :accessor_name ...]
147
165
  # example: [:person, 1, :first_name]
148
166
  # Currently, indexes must be integers.
149
- def retrieve(*args)
150
- xpath = self.class.accessor_xpath(*args)
167
+ def retrieve(*pointers)
168
+ xpath = self.class.accessor_xpath(*pointers)
151
169
  ng_xml.xpath(xpath, "oxns"=>"http://www.loc.gov/mods/v3")
152
170
  end
153
171
 
data/om.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{om}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matt Zumwalt"]
@@ -139,6 +139,19 @@ describe "OM::XML::Accessors" do
139
139
  AccessorTest.accessor_xpath( {:conference=>0}, {:role=>1}, :text ).should == '//oxns:name[@type="conference" and position()=1]/oxns:role[position()=2]/oxns:roleTerm[@type="text"]'
140
140
  end
141
141
  end
142
+
143
+ describe "#accessor_generic_name" do
144
+ it "should generate a generic accessor name based on an array of pointers" do
145
+ AccessorTest.accessor_generic_name( {:conference=>0}, {:role=>1}, :text ).should == "conference_role_text"
146
+ end
147
+ end
148
+
149
+ describe "#accessor_hierarchical_name" do
150
+ it "should generate a specific accessor name based on an array of pointers and indexes" do
151
+ AccessorTest.accessor_hierarchical_name( {:conference=>0}, {:role=>1}, :text ).should == "conference_0_role_1_text"
152
+ end
153
+ end
154
+
142
155
  # describe ".accessor_xpath (instance method)" do
143
156
  # it "should delegate to the class method" do
144
157
  # AccessorTest.expects(:accessor_xpath).with( [:conference, conference_index, :text_role] )
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: om
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Zumwalt