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 +4 -0
- data/VERSION +1 -1
- data/lib/om/xml/accessors.rb +41 -23
- data/om.gemspec +1 -1
- data/spec/unit/accessors_spec.rb +13 -0
- metadata +3 -3
data/History.textile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/om/xml/accessors.rb
CHANGED
@@ -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(*
|
43
|
+
def accessor_info(*pointers)
|
44
44
|
info = @accessors
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
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(
|
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(*
|
67
|
+
def accessor_xpath(*pointers)
|
71
68
|
|
72
69
|
keys = []
|
73
|
-
# keys = even_values(args)
|
74
|
-
# indices = odd_values(args)
|
75
70
|
xpath = "//"
|
76
|
-
|
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 =
|
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
|
133
|
-
|
127
|
+
def accessor_generic_name(*pointers)
|
128
|
+
pointers_to_flat_array(pointers, false).join("_")
|
134
129
|
end
|
135
|
-
|
136
|
-
|
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
|
-
# *
|
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(*
|
150
|
-
xpath = self.class.accessor_xpath(*
|
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
data/spec/unit/accessors_spec.rb
CHANGED
@@ -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:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Zumwalt
|