mods 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -38,6 +38,7 @@ TODO: Write usage instructions here
38
38
 
39
39
  == Releases
40
40
 
41
+ * <b>0.0.19</b> term_values and term_value method added to Record object
41
42
  * <b>0.0.18</b> <subject><temporal> cannot have subelements
42
43
  * <b>0.0.17</b> add display_value and display_value_w_date to name node; add personal_names_w_dates to record
43
44
  * <b>0.0.16</b> add role convenience methods (within name node)
data/lib/mods/record.rb CHANGED
@@ -56,6 +56,88 @@ module Mods
56
56
  set_terminology_no_ns(@mods_ng_xml)
57
57
  end
58
58
  end
59
+
60
+ # get the value for the terms, as a String. f there are multiple values, they will be joined with the separator.
61
+ # If there are no values, the result will be nil.
62
+ # @param [Symbol or String or Array<Symbol>] messages the single symbol of the message to send to the Stanford::Mods::Record object
63
+ # (as a Symbol or a String), or an Array of (Symbols or Strings) to be sent as messages.
64
+ # Messages will usually be terms from the nom-xml terminology defined in the mods gem.)
65
+ # @param [String] sep - the separator string to insert between multiple values
66
+ # @return [String] a String representing the value(s) or nil.
67
+ def term_value messages, sep = ' '
68
+ case messages
69
+ when Symbol
70
+ nodes = send(messages)
71
+ when String
72
+ nodes = send(messages.to_sym)
73
+ when Array
74
+ obj = self
75
+ messages.each { |msg|
76
+ if msg.is_a? Symbol
77
+ obj = obj.send(msg)
78
+ elsif msg.is_a? String
79
+ obj = obj.send(msg.to_sym)
80
+ else
81
+ raise ArgumentError, "term_value called with Array containing unrecognized class: #{msg.class}, #{messages.inspect}", caller
82
+ end
83
+ }
84
+ nodes = obj
85
+ else
86
+ raise ArgumentError, "term_value called with unrecognized argument class: #{messages.class}", caller
87
+ end
88
+
89
+ val = ''
90
+ if nodes
91
+ nodes.each { |n|
92
+ val << sep + n.text unless n.text.empty?
93
+ }
94
+ end
95
+ val.sub!(sep, '')
96
+ return nil if val.empty?
97
+ val
98
+ rescue NoMethodError
99
+ raise ArgumentError, "term_value called with unknown argument: #{messages.inspect}", caller
100
+ end
101
+
102
+ # get the values for the terms, as an Array. If there are no values, the result will be nil.
103
+ # @param [Symbol or String or Array<Symbol>] messages the single symbol of the message to send to the Stanford::Mods::Record object
104
+ # (as a Symbol or a String), or an Array of (Symbols or Strings) to be sent as messages.
105
+ # Messages will usually be terms from the nom-xml terminology defined in the mods gem.)
106
+ # @return [Array<String>] an Array with a String value for each result node's non-empty text, or nil if none
107
+ def term_values messages
108
+ case messages
109
+ when Symbol
110
+ nodes = send(messages)
111
+ when String
112
+ nodes = send(messages.to_sym)
113
+ when Array
114
+ obj = self
115
+ messages.each { |msg|
116
+ if msg.is_a? Symbol
117
+ obj = obj.send(msg)
118
+ elsif msg.is_a? String
119
+ obj = obj.send(msg.to_sym)
120
+ else
121
+ raise ArgumentError, "term_values called with Array containing unrecognized class: #{msg.class}, #{messages.inspect}", caller
122
+ end
123
+ }
124
+ nodes = obj
125
+ else
126
+ raise ArgumentError, "term_values called with unrecognized argument class: #{messages.class}", caller
127
+ end
128
+
129
+ vals = []
130
+ if nodes
131
+ nodes.each { |n|
132
+ vals << n.text unless n.text.empty?
133
+ }
134
+ end
135
+ return nil if vals.empty?
136
+ vals
137
+ rescue NoMethodError
138
+ raise ArgumentError, "term_values called with unknown argument: #{messages.inspect}", caller
139
+ end
140
+
59
141
 
60
142
  # @return Array of Strings, each containing the text contents of <mods><titleInfo> <nonSort> + ' ' + <title> elements
61
143
  # but not including any titleInfo elements with type="alternative"
data/lib/mods/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Mods
2
2
  # this is the Ruby Gem version
3
- VERSION = "0.0.18"
3
+ VERSION = "0.0.19"
4
4
  end
data/spec/record_spec.rb CHANGED
@@ -82,6 +82,84 @@ describe "Mods::Record" do
82
82
  end
83
83
  end # context from_nk_node
84
84
 
85
+ context "getting term values" do
86
+ before(:all) do
87
+ m = "<mods #{@def_ns_decl}>
88
+ <abstract>single</abstract>
89
+ <genre></genre>
90
+ <note>mult1</note>
91
+ <note>mult2</note>
92
+ <subject><topic>topic1</topic><topic>topic2</topic></subject>
93
+ <subject><topic>topic3</topic></subject>
94
+ </mods>"
95
+ @mods_rec = Mods::Record.new
96
+ @mods_rec.from_str(m)
97
+ end
98
+
99
+ context "term_value (single value result)" do
100
+ it "should return nil if there are no such values in the MODS" do
101
+ @mods_rec.term_value(:identifier).should == nil
102
+ end
103
+ it "should return nil if there are only empty values in the MODS" do
104
+ @mods_rec.term_value(:genre).should == nil
105
+ end
106
+ it "should return a String for a single value" do
107
+ @mods_rec.term_value(:abstract).should == 'single'
108
+ end
109
+ it "should return a String containing all values, with separator, for multiple values" do
110
+ @mods_rec.term_value(:note).should == 'mult1 mult2'
111
+ end
112
+ it "should work with an Array of messages passed as the argument" do
113
+ @mods_rec.term_value([:subject, 'topic']).should == 'topic1 topic2 topic3'
114
+ end
115
+ it "should work with a String passed as the argument" do
116
+ @mods_rec.term_value('abstract').should == 'single'
117
+ end
118
+ it "should take a separator argument" do
119
+ @mods_rec.term_value(:note, ' -|-').should == 'mult1 -|-mult2'
120
+ end
121
+ it "should raise an error for an unrecognized message symbol" do
122
+ expect { @mods_rec.term_value(:not_there) }.to raise_error(ArgumentError, "term_value called with unknown argument: :not_there")
123
+ end
124
+ it "should raise an error if the argument is an Array containing non-symbols" do
125
+ expect { @mods_rec.term_value([:subject, @mods_rec.subject]) }.to raise_error(ArgumentError, /term_value called with Array containing unrecognized class:.*NodeSet.*/)
126
+ end
127
+ it "should raise an error if the argument isn't a Symbol or an Array" do
128
+ expect { @mods_rec.term_value(@mods_rec.subject) }.to raise_error(ArgumentError, /term_value called with unrecognized argument class:.*NodeSet.*/)
129
+ end
130
+ end
131
+
132
+ context "term_values (multiple values)" do
133
+ it "should return nil if there are no such values in the MODS" do
134
+ @mods_rec.term_values(:identifier).should == nil
135
+ end
136
+ it "should return nil if there are only empty values in the MODS" do
137
+ @mods_rec.term_values(:genre).should == nil
138
+ end
139
+ it "should return an array of size one for a single value" do
140
+ @mods_rec.term_values(:abstract).should == ['single']
141
+ end
142
+ it "should return an array of values for multiple values" do
143
+ @mods_rec.term_values(:note).should == ['mult1', 'mult2']
144
+ end
145
+ it "should work with an Array of messages passed as the argument" do
146
+ @mods_rec.term_values([:subject, 'topic']).should == ['topic1', 'topic2', 'topic3']
147
+ end
148
+ it "should work with a String passed as the argument" do
149
+ @mods_rec.term_values('abstract').should == ['single']
150
+ end
151
+ it "should raise an error for an unrecognized message symbol" do
152
+ expect { @mods_rec.term_values(:not_there) }.to raise_error(ArgumentError, "term_values called with unknown argument: :not_there")
153
+ end
154
+ it "should raise an error if the argument is an Array containing non-symbols" do
155
+ expect { @mods_rec.term_values([:subject, @mods_rec.subject]) }.to raise_error(ArgumentError, /term_values called with Array containing unrecognized class:.*NodeSet.*/)
156
+ end
157
+ it "should raise an error if the argument isn't a Symbol or an Array" do
158
+ expect { @mods_rec.term_values(@mods_rec.subject) }.to raise_error(ArgumentError, /term_values called with unrecognized argument class:.*NodeSet.*/)
159
+ end
160
+ end
161
+ end # getting term values
162
+
85
163
  context "convenience methods for accessing tricky bits of terminology" do
86
164
  before(:all) do
87
165
  @mods_rec = Mods::Record.new
@@ -254,6 +332,6 @@ describe "Mods::Record" do
254
332
  @mods_rec.languages.should include("Dutch; Flemish")
255
333
  end
256
334
  end
257
- end # convenience methods
335
+ end # convenience methods for tricky bits of terminology
258
336
 
259
337
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -230,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
230
230
  version: '0'
231
231
  segments:
232
232
  - 0
233
- hash: 1014279353675064863
233
+ hash: -103128391758447050
234
234
  required_rubygems_version: !ruby/object:Gem::Requirement
235
235
  none: false
236
236
  requirements:
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: 1014279353675064863
242
+ hash: -103128391758447050
243
243
  requirements: []
244
244
  rubyforge_project:
245
245
  rubygems_version: 1.8.24