mods_display 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,10 +19,12 @@ class ModsDisplay::Imprint < ModsDisplay::Field
19
19
  publisher = val.publisher.map do |p|
20
20
  p.text unless p.text.strip.empty?
21
21
  end.compact.join(" : ").strip unless val.publisher.text.strip.empty?
22
- parts = val.children.select do |child|
23
- ["dateIssued", "dateOther"].include?(child.name) and !child.attributes.has_key?("encoding")
24
- end.map do |child|
25
- child.text.strip unless child.text.strip.empty?
22
+ parts = ["dateIssued", "dateOther"].map do |date_field_name|
23
+ if val.respond_to?(date_field_name.to_sym)
24
+ parse_dates(val.send(date_field_name.to_sym))
25
+ end
26
+ end.flatten.map do |date|
27
+ date.strip unless date.strip.empty?
26
28
  end.compact.join(", ")
27
29
  edition = nil if edition.strip.empty?
28
30
  parts = nil if parts.strip.empty?
@@ -33,6 +35,9 @@ class ModsDisplay::Imprint < ModsDisplay::Field
33
35
  unless [editionPlace, parts].compact.join(", ").strip.empty?
34
36
  return_values << ModsDisplay::Values.new(:label => displayLabel(val) || "Imprint", :values => [[editionPlace, parts].compact.join(", ")])
35
37
  end
38
+ if dates(val).length > 0
39
+ return_values.concat(dates(val))
40
+ end
36
41
  if other_pub_info(val).length > 0
37
42
  other_pub_info(val).each do |pub_info|
38
43
  return_values << ModsDisplay::Values.new(:label => displayLabel(val) || pub_info_labels[pub_info.name.to_sym], :values => [pub_info.text.strip])
@@ -42,7 +47,111 @@ class ModsDisplay::Imprint < ModsDisplay::Field
42
47
  end
43
48
  return_values
44
49
  end
45
-
50
+ def dates(element)
51
+ date_field_keys.map do |date_field|
52
+ if element.respond_to?(date_field)
53
+ elements = element.send(date_field)
54
+ unless elements.empty?
55
+ ModsDisplay::Values.new(:label => displayLabel(element) || pub_info_labels[elements.first.name.to_sym], :values => parse_dates(elements))
56
+ end
57
+ end
58
+ end.compact
59
+ end
60
+ def parse_dates(date_field)
61
+ apply_date_qualifier_decoration dedup_dates join_date_ranges date_field
62
+ end
63
+ def join_date_ranges(date_fields)
64
+ if dates_are_range?(date_fields)
65
+ start_date = date_fields.find{|d| d.attributes["point"] && d.attributes["point"].value == "start"}
66
+ end_date = date_fields.find{|d| d.attributes["point"] && d.attributes["point"].value == "end"}
67
+ date_fields.map do |date|
68
+ date = date.clone # clone the date object so we don't append the same one
69
+ if normalize_date(date.text) == start_date.text
70
+ date.content = [start_date.text, end_date.text].join("-")
71
+ date
72
+ elsif normalize_date(date.text) != end_date.text
73
+ date
74
+ end
75
+ end.compact
76
+ elsif dates_are_open_range?(date_fields)
77
+ start_date = date_fields.find{|d| d.attributes["point"] && d.attributes["point"].value == "start"}
78
+ date_fields.map do |date|
79
+ date = date.clone # clone the date object so we don't append the same one
80
+ if date.text == start_date.text
81
+ date.content = "#{start_date.text}-"
82
+ end
83
+ date
84
+ end
85
+ else
86
+ date_fields
87
+ end
88
+ end
89
+ def apply_date_qualifier_decoration(date_fields)
90
+ return_fields = date_fields.map do |date|
91
+ date = date.clone
92
+ if date_is_approximate?(date)
93
+ date.content = "c#{date.text}"
94
+ elsif date_is_questionable?(date)
95
+ date.content = "[#{date.text}?]"
96
+ elsif date_is_inferred?(date)
97
+ date.content = "[#{date.text}]"
98
+ end
99
+ date
100
+ end
101
+ return_fields.map{|d| d.text }
102
+ end
103
+ def date_is_approximate?(date_field)
104
+ date_field.attributes["qualifier"] and
105
+ date_field.attributes["qualifier"].respond_to?(:value) and
106
+ date_field.attributes["qualifier"].value == "approximate"
107
+ end
108
+ def date_is_questionable?(date_field)
109
+ date_field.attributes["qualifier"] and
110
+ date_field.attributes["qualifier"].respond_to?(:value) and
111
+ date_field.attributes["qualifier"].value == "questionable"
112
+ end
113
+ def date_is_inferred?(date_field)
114
+ date_field.attributes["qualifier"] and
115
+ date_field.attributes["qualifier"].respond_to?(:value) and
116
+ date_field.attributes["qualifier"].value == "inferred"
117
+ end
118
+ def dates_are_open_range?(date_fields)
119
+ date_fields.any? do |field|
120
+ field.attributes["point"] and
121
+ field.attributes["point"].respond_to?(:value) and
122
+ field.attributes["point"].value == "start"
123
+ end and !date_fields.any? do |field|
124
+ field.attributes["point"] and
125
+ field.attributes["point"].respond_to?(:value) and
126
+ field.attributes["point"].value == "end"
127
+ end
128
+ end
129
+ def dates_are_range?(date_fields)
130
+ attributes = date_fields.map do |date|
131
+ if date.attributes["point"].respond_to?(:value)
132
+ date.attributes["point"].value
133
+ end
134
+ end
135
+ attributes.include?("start") and
136
+ attributes.include?("end")
137
+ end
138
+ def dedup_dates(date_fields)
139
+ date_text = date_fields.map{|d| normalize_date(d.text) }
140
+ if date_text != date_text.uniq
141
+ if date_fields.find{ |d| d.attributes["qualifier"].respond_to?(:value) }
142
+ [date_fields.find{ |d| d.attributes["qualifier"].respond_to?(:value) }]
143
+ elsif date_fields.find{ |d| !d.attributes["encoding"] }
144
+ [date_fields.find{ |d| !d.attributes["encoding"] }]
145
+ else
146
+ [date_fields.first]
147
+ end
148
+ else
149
+ date_fields
150
+ end
151
+ end
152
+ def normalize_date(date)
153
+ date.strip.gsub(/^\s*c\s*|\[|\]|\?/, "")
154
+ end
46
155
  def other_pub_info(element)
47
156
  element.children.select do |child|
48
157
  pub_info_parts.include?(child.name.to_sym)
@@ -59,7 +168,11 @@ class ModsDisplay::Imprint < ModsDisplay::Field
59
168
  private
60
169
 
61
170
  def pub_info_parts
62
- pub_info_labels.keys
171
+ [:issuance, :frequency]
172
+ end
173
+
174
+ def date_field_keys
175
+ [:dateCreated, :dateCaptured, :dateValid, :dateModified, :copyrightDate]
63
176
  end
64
177
 
65
178
  def pub_info_labels
@@ -1,3 +1,3 @@
1
1
  module ModsDisplay
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -12,12 +12,21 @@ describe ModsDisplay::Imprint do
12
12
  @imprint = Stanford::Mods::Record.new.from_str(imprint_mods, false).origin_info
13
13
  @no_edition = Stanford::Mods::Record.new.from_str(no_edition_mods, false).origin_info
14
14
  @edition_and_date = Stanford::Mods::Record.new.from_str(origin_info_mods, false).origin_info
15
- @encoded_date = Stanford::Mods::Record.new.from_str(encoded_date, false).origin_info
16
- @encoded_only = Stanford::Mods::Record.new.from_str(only_encoded_data, false).origin_info
17
15
  @mixed = Stanford::Mods::Record.new.from_str(mixed_mods, false).origin_info
18
16
  @display_form = Stanford::Mods::Record.new.from_str(display_form, false).origin_info
19
17
  @display_form_with_label = Stanford::Mods::Record.new.from_str(display_form, false).origin_info
20
18
  @display_label = Stanford::Mods::Record.new.from_str(display_label, false).origin_info
19
+ @date_range = Stanford::Mods::Record.new.from_str(date_range, false).origin_info
20
+ @open_date_range = Stanford::Mods::Record.new.from_str(open_date_range, false).origin_info
21
+ @dup_qualified_date = Stanford::Mods::Record.new.from_str(dup_qualified_date, false).origin_info
22
+ @dup_unencoded_date = Stanford::Mods::Record.new.from_str(dup_unencoded_date, false).origin_info
23
+ @dup_date = Stanford::Mods::Record.new.from_str(dup_date, false).origin_info
24
+ @approximate_date = Stanford::Mods::Record.new.from_str(approximate_date, false).origin_info
25
+ @questionable_date = Stanford::Mods::Record.new.from_str(questionable_date, false).origin_info
26
+ @inferred_date = Stanford::Mods::Record.new.from_str(inferred_date, false).origin_info
27
+ @three_imprint_dates = Stanford::Mods::Record.new.from_str(three_imprint_dates, false).origin_info
28
+ @qualified_imprint_date = Stanford::Mods::Record.new.from_str(qualified_imprint_date, false).origin_info
29
+ @imprint_date_range = Stanford::Mods::Record.new.from_str(imprint_date_range, false).origin_info
21
30
  end
22
31
 
23
32
  describe "labels" do
@@ -30,7 +39,7 @@ describe ModsDisplay::Imprint do
30
39
  fields.last.label.should == "Issuance"
31
40
  end
32
41
  it "should get multiple labels when we have mixed content" do
33
- mods_display_imprint(@mixed).fields.map{|val| val.label }.should == ["Imprint", "Issuance", "Date captured"]
42
+ mods_display_imprint(@mixed).fields.map{|val| val.label }.should == ["Imprint", "Date captured", "Issuance"]
34
43
  end
35
44
  it "should use the displayLabel when available" do
36
45
  mods_display_imprint(@display_label).fields.map{|val| val.label }.should == ["TheLabel", "IssuanceLabel", "IssuanceLabel"]
@@ -51,12 +60,6 @@ describe ModsDisplay::Imprint do
51
60
  fields.first.values.should == ["A Valid Date"]
52
61
  fields.last.values.should == ["The Issuance"]
53
62
  end
54
- it "should omit dates with an encoding attribute" do
55
- mods_display_imprint(@encoded_date).fields.map{|val| val.values }.join.should_not include("An Encoded Date")
56
- end
57
- it "should not try to return data w/ only encoded data" do
58
- mods_display_imprint(@encoded_only).fields.should be_empty
59
- end
60
63
  it "should handle mixed mods properly" do
61
64
  values = mods_display_imprint(@mixed).fields
62
65
  values.length.should == 3
@@ -65,6 +68,69 @@ describe ModsDisplay::Imprint do
65
68
  values.map{|val| val.values}.should include(["The Capture Date"])
66
69
  end
67
70
  end
71
+ describe "date processing" do
72
+ describe "ranges" do
73
+ it "should join start and end point ranges with a '-'" do
74
+ fields = mods_display_imprint(@date_range).fields
75
+ fields.length.should == 1
76
+ fields.first.values.should == ["1820-1825"]
77
+ end
78
+ it "should handle open ranges properly" do
79
+ fields = mods_display_imprint(@open_date_range).fields
80
+ fields.length.should == 1
81
+ fields.first.values.should == ["1820-"]
82
+ end
83
+ it "should handle when there are more than 3 of the same date w/i a range" do
84
+ fields = mods_display_imprint(@three_imprint_dates).fields
85
+ fields.length.should == 1
86
+ fields.first.values.should == ["[1820-1825?]"]
87
+ end
88
+ it "should apply the qualifier decoration in the imprints" do
89
+ fields = mods_display_imprint(@qualified_imprint_date).fields
90
+ fields.length.should == 1
91
+ fields.first.values.should == ["[1820?]"]
92
+ end
93
+ it "should handle date ranges in imprints" do
94
+ fields = mods_display_imprint(@imprint_date_range).fields
95
+ fields.length.should == 1
96
+ fields.first.values.should == ["1820-1825"]
97
+ end
98
+ end
99
+ describe "duplication" do
100
+ it "should only return the qualified date when present" do
101
+ fields = mods_display_imprint(@dup_qualified_date).fields
102
+ fields.length.should == 1
103
+ fields.first.values.should == ["[1820?]"]
104
+ end
105
+ it "should use the non-encoded date when prsent" do
106
+ fields = mods_display_imprint(@dup_unencoded_date).fields
107
+ fields.length.should == 1
108
+ fields.first.values.should == ["c1820"]
109
+ end
110
+ it "should only return one when no attributes are present" do
111
+ fields = mods_display_imprint(@dup_date).fields
112
+ fields.length.should == 1
113
+ fields.first.values.should == ["1820"]
114
+ end
115
+ end
116
+ describe "qualifier decoration" do
117
+ it "should prepend a 'c' to approximate dates" do
118
+ fields = mods_display_imprint(@approximate_date).fields
119
+ fields.length.should == 1
120
+ fields.first.values.should == ["c1820"]
121
+ end
122
+ it "should append a '?' to a questionable dates and wrap them in square-brackets" do
123
+ fields = mods_display_imprint(@questionable_date).fields
124
+ fields.length.should == 1
125
+ fields.first.values.should == ["[1820?]"]
126
+ end
127
+ it "should wrap inferred dates in square-brackets" do
128
+ fields = mods_display_imprint(@inferred_date).fields
129
+ fields.length.should == 1
130
+ fields.first.values.should == ["[1820]"]
131
+ end
132
+ end
133
+ end
68
134
  describe "to_html" do
69
135
  it "should return the display form if one is available" do
70
136
  html = mods_display_imprint(@display_form).to_html
@@ -38,25 +38,6 @@ module ImprintFixtures
38
38
  </mods>
39
39
  MODS
40
40
  end
41
- def encoded_date
42
- <<-MODS
43
- <mods>
44
- <originInfo>
45
- <place>A Place</place>
46
- <dateIssued encoding="an-encoding">An Encoded Date</dateIssued>
47
- </originInfo>
48
- </mods>
49
- MODS
50
- end
51
- def only_encoded_data
52
- <<-MODS
53
- <mods>
54
- <originInfo>
55
- <dateIssued encoding="an-encoding">An Encoded Date</dateIssued>
56
- </originInfo>
57
- </mods>
58
- MODS
59
- end
60
41
  def mixed_mods
61
42
  <<-MODS
62
43
  <mods>
@@ -101,4 +82,111 @@ module ImprintFixtures
101
82
  </mods>
102
83
  MODS
103
84
  end
85
+ def date_range
86
+ <<-MODS
87
+ <mods>
88
+ <originInfo>
89
+ <dateCreated point="end">1825</dateCreated>
90
+ <dateCreated point="start">1820</dateCreated>
91
+ </originInfo>
92
+ </mods>
93
+ MODS
94
+ end
95
+ def open_date_range
96
+ <<-MODS
97
+ <mods>
98
+ <originInfo>
99
+ <dateCreated point="start">1820</dateCreated>
100
+ </originInfo>
101
+ </mods>
102
+ MODS
103
+ end
104
+ def dup_qualified_date
105
+ <<-MODS
106
+ <mods>
107
+ <originInfo>
108
+ <dateCreated qualifier="questionable">1820</dateCreated>
109
+ <dateCreated>1820</dateCreated>
110
+ </originInfo>
111
+ </mods>
112
+ MODS
113
+ end
114
+ def dup_unencoded_date
115
+ <<-MODS
116
+ <mods>
117
+ <originInfo>
118
+ <dateCreated encoding="marc">1820</dateCreated>
119
+ <dateCreated>c1820</dateCreated>
120
+ </originInfo>
121
+ </mods>
122
+ MODS
123
+ end
124
+ def dup_date
125
+ <<-MODS
126
+ <mods>
127
+ <originInfo>
128
+ <dateCreated>1820</dateCreated>
129
+ <dateCreated>1820</dateCreated>
130
+ </originInfo>
131
+ </mods>
132
+ MODS
133
+ end
134
+ def approximate_date
135
+ <<-MODS
136
+ <mods>
137
+ <originInfo>
138
+ <dateValid qualifier="approximate">1820</dateValid>
139
+ </originInfo>
140
+ </mods>
141
+ MODS
142
+ end
143
+ def questionable_date
144
+ <<-MODS
145
+ <mods>
146
+ <originInfo>
147
+ <dateValid qualifier="questionable">1820</dateValid>
148
+ </originInfo>
149
+ </mods>
150
+ MODS
151
+ end
152
+ def inferred_date
153
+ <<-MODS
154
+ <mods>
155
+ <originInfo>
156
+ <dateValid qualifier="inferred">1820</dateValid>
157
+ </originInfo>
158
+ </mods>
159
+ MODS
160
+ end
161
+ def three_imprint_dates
162
+ <<-MODS
163
+ <mods>
164
+ <originInfo>
165
+ <dateIssued>[1820-1825]</dateIssued>
166
+ <dateIssued point="start" qualifier="questionable">1820</dateIssued>
167
+ <dateIssued point="end" qualifier="questionable">1825</dateIssued>
168
+ </originInfo>
169
+ </mods>
170
+ MODS
171
+ end
172
+ def qualified_imprint_date
173
+ <<-MODS
174
+ <mods>
175
+ <originInfo>
176
+ <dateIssued qualifier="questionable">1820</dateIssued>
177
+ </originInfo>
178
+ </mods>
179
+ MODS
180
+ end
181
+ def imprint_date_range
182
+ <<-MODS
183
+ <mods>
184
+ <originInfo>
185
+ <dateIssued>[1820]</dateIssued>
186
+ <dateIssued point="start">1820</dateIssued>
187
+ <dateIssued point="end">1825</dateIssued>
188
+ </originInfo>
189
+ </mods>
190
+ MODS
191
+ end
104
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mods_display
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-24 00:00:00.000000000 Z
12
+ date: 2013-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stanford-mods
@@ -151,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  segments:
153
153
  - 0
154
- hash: -1792709584890457737
154
+ hash: -3367910035276828963
155
155
  required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  none: false
157
157
  requirements:
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  version: '0'
161
161
  segments:
162
162
  - 0
163
- hash: -1792709584890457737
163
+ hash: -3367910035276828963
164
164
  requirements: []
165
165
  rubyforge_project:
166
166
  rubygems_version: 1.8.25