mods_display 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +16 -0
  3. data/.rubocop_todo.yml +60 -0
  4. data/.travis.yml +2 -2
  5. data/Rakefile +10 -2
  6. data/lib/mods_display.rb +42 -42
  7. data/lib/mods_display/configuration.rb +69 -67
  8. data/lib/mods_display/configuration/access_condition.rb +17 -13
  9. data/lib/mods_display/configuration/base.rb +27 -24
  10. data/lib/mods_display/configuration/format.rb +8 -4
  11. data/lib/mods_display/configuration/genre.rb +8 -4
  12. data/lib/mods_display/configuration/imprint.rb +12 -7
  13. data/lib/mods_display/configuration/name.rb +8 -4
  14. data/lib/mods_display/configuration/note.rb +8 -4
  15. data/lib/mods_display/configuration/related_item.rb +8 -4
  16. data/lib/mods_display/configuration/subject.rb +11 -9
  17. data/lib/mods_display/configuration/title.rb +8 -4
  18. data/lib/mods_display/controller_extension.rb +24 -24
  19. data/lib/mods_display/country_codes.rb +385 -384
  20. data/lib/mods_display/fields/abstract.rb +7 -6
  21. data/lib/mods_display/fields/access_condition.rb +55 -55
  22. data/lib/mods_display/fields/audience.rb +7 -6
  23. data/lib/mods_display/fields/cartographics.rb +15 -14
  24. data/lib/mods_display/fields/collection.rb +32 -17
  25. data/lib/mods_display/fields/contact.rb +15 -13
  26. data/lib/mods_display/fields/contents.rb +7 -6
  27. data/lib/mods_display/fields/description.rb +21 -21
  28. data/lib/mods_display/fields/field.rb +164 -109
  29. data/lib/mods_display/fields/format.rb +36 -33
  30. data/lib/mods_display/fields/genre.rb +12 -11
  31. data/lib/mods_display/fields/identifier.rb +34 -34
  32. data/lib/mods_display/fields/imprint.rb +258 -214
  33. data/lib/mods_display/fields/language.rb +18 -16
  34. data/lib/mods_display/fields/location.rb +27 -26
  35. data/lib/mods_display/fields/name.rb +113 -118
  36. data/lib/mods_display/fields/note.rb +33 -34
  37. data/lib/mods_display/fields/related_item.rb +64 -66
  38. data/lib/mods_display/fields/resource_type.rb +13 -11
  39. data/lib/mods_display/fields/sub_title.rb +6 -4
  40. data/lib/mods_display/fields/subject.rb +92 -90
  41. data/lib/mods_display/fields/title.rb +54 -49
  42. data/lib/mods_display/fields/values.rb +7 -8
  43. data/lib/mods_display/html.rb +117 -96
  44. data/lib/mods_display/model_extension.rb +17 -16
  45. data/lib/mods_display/relator_codes.rb +269 -267
  46. data/lib/mods_display/version.rb +1 -1
  47. data/mods_display.gemspec +13 -12
  48. data/spec/configuration/access_condition_spec.rb +5 -5
  49. data/spec/configuration/base_spec.rb +24 -24
  50. data/spec/fields/abstract_spec.rb +24 -14
  51. data/spec/fields/access_condition_spec.rb +80 -55
  52. data/spec/fields/audience_spec.rb +15 -12
  53. data/spec/fields/cartographics_spec.rb +17 -18
  54. data/spec/fields/collection_spec.rb +65 -19
  55. data/spec/fields/contact_spec.rb +11 -9
  56. data/spec/fields/contents_spec.rb +15 -12
  57. data/spec/fields/description_spec.rb +40 -22
  58. data/spec/fields/format_spec.rb +28 -18
  59. data/spec/fields/genre_spec.rb +18 -16
  60. data/spec/fields/identifier_spec.rb +46 -34
  61. data/spec/fields/imprint_spec.rb +151 -125
  62. data/spec/fields/language_spec.rb +36 -20
  63. data/spec/fields/location_spec.rb +43 -27
  64. data/spec/fields/name_spec.rb +92 -92
  65. data/spec/fields/note_spec.rb +53 -40
  66. data/spec/fields/related_item_spec.rb +42 -40
  67. data/spec/fields/resource_type_spec.rb +20 -14
  68. data/spec/fields/sub_title_spec.rb +11 -9
  69. data/spec/fields/subject_spec.rb +61 -56
  70. data/spec/fields/title_spec.rb +53 -40
  71. data/spec/fixtures/access_condition_fixtures.rb +50 -0
  72. data/spec/fixtures/cartographics_fixtures.rb +1 -1
  73. data/spec/fixtures/imprint_fixtures.rb +49 -1
  74. data/spec/fixtures/name_fixtures.rb +195 -2
  75. data/spec/fixtures/related_item_fixtures.rb +107 -0
  76. data/spec/fixtures/subjects_fixtures.rb +15 -2
  77. data/spec/fixtures/title_fixtures.rb +101 -0
  78. data/spec/integration/configuration_spec.rb +23 -20
  79. data/spec/integration/html_spec.rb +40 -29
  80. data/spec/integration/installation_spec.rb +14 -14
  81. data/spec/spec_helper.rb +5 -8
  82. metadata +25 -3
@@ -1,21 +1,23 @@
1
- class ModsDisplay::Language < ModsDisplay::Field
2
- def fields
3
- return_fields = @values.map do |value|
4
- if value.respond_to?(:languageTerm)
1
+ module ModsDisplay
2
+ class Language < Field
3
+ def fields
4
+ return_fields = @values.map do |value|
5
+ next unless value.respond_to?(:languageTerm)
5
6
  value.languageTerm.map do |term|
6
- if term.attributes["type"].respond_to?(:value) and term.attributes["type"].value == "code"
7
- ModsDisplay::Values.new(:label => displayLabel(value) || displayLabel(term) || I18n.t('mods_display.language'), :values => [displayForm(value) || language_codes[term.text]].flatten)
8
- end
7
+ next unless term.attributes['type'].respond_to?(:value) && term.attributes['type'].value == 'code'
8
+ ModsDisplay::Values.new(
9
+ label: displayLabel(value) || displayLabel(term) || I18n.t('mods_display.language'),
10
+ values: [displayForm(value) || language_codes[term.text]].flatten
11
+ )
9
12
  end.flatten.compact
10
- end
11
- end.flatten.compact
12
- collapse_fields(return_fields)
13
- end
13
+ end.flatten.compact
14
+ collapse_fields(return_fields)
15
+ end
14
16
 
15
- private
17
+ private
16
18
 
17
- def language_codes
18
- SEARCHWORKS_LANGUAGES
19
+ def language_codes
20
+ SEARCHWORKS_LANGUAGES
21
+ end
19
22
  end
20
-
21
- end
23
+ end
@@ -1,41 +1,42 @@
1
- class ModsDisplay::Location < ModsDisplay::Field
2
-
3
- def fields
4
- return_fields = []
5
- @values.each do |location|
6
- location.children.each do |child|
7
- if location_field_keys.include? child.name.to_sym
1
+ module ModsDisplay
2
+ class Location < Field
3
+ def fields
4
+ return_fields = []
5
+ @values.each do |location|
6
+ location.children.each do |child|
7
+ next unless location_field_keys.include?(child.name.to_sym)
8
8
  if child.name.to_sym == :url
9
9
  loc_label = displayLabel(location) || I18n.t('mods_display.location')
10
- value = "<a href='#{child.text}'>#{(displayLabel(child) || child.text).gsub(/:$/,'')}</a>"
10
+ value = "<a href='#{child.text}'>#{(displayLabel(child) || child.text).gsub(/:$/, '')}</a>"
11
11
  else
12
12
  loc_label = location_label(child) || displayLabel(location) || I18n.t('mods_display.location')
13
13
  value = child.text
14
14
  end
15
- return_fields << ModsDisplay::Values.new(:label => loc_label || displayLabel(location) || I18n.t('mods_display.location'),
16
- :values => [value])
15
+ return_fields << ModsDisplay::Values.new(
16
+ label: loc_label || displayLabel(location) || I18n.t('mods_display.location'),
17
+ values: [value]
18
+ )
17
19
  end
18
20
  end
21
+ collapse_fields(return_fields)
19
22
  end
20
- collapse_fields(return_fields)
21
- end
22
23
 
23
- private
24
+ private
24
25
 
25
- def location_field_keys
26
- [:physicalLocation, :url, :shelfLocation, :holdingSimple, :holdingExternal]
27
- end
26
+ def location_field_keys
27
+ [:physicalLocation, :url, :shelfLocation, :holdingSimple, :holdingExternal]
28
+ end
28
29
 
29
- def location_label(element)
30
- if displayLabel(element)
31
- displayLabel(element)
32
- elsif element.attributes["type"].respond_to?(:value) && location_labels.has_key?(element.attributes["type"].value)
33
- location_labels[element.attributes["type"].value]
30
+ def location_label(element)
31
+ if displayLabel(element)
32
+ displayLabel(element)
33
+ elsif element.attributes['type'].respond_to?(:value) && location_labels.key?(element.attributes['type'].value)
34
+ location_labels[element.attributes['type'].value]
35
+ end
34
36
  end
35
- end
36
37
 
37
- def location_labels
38
- {"repository" => I18n.t('mods_display.repository')}
38
+ def location_labels
39
+ { 'repository' => I18n.t('mods_display.repository') }
40
+ end
39
41
  end
40
-
41
- end
42
+ end
@@ -1,25 +1,26 @@
1
- class ModsDisplay::Name < ModsDisplay::Field
2
- include ModsDisplay::RelatorCodes
3
- def fields
4
- return_fields = @values.map do |value|
5
- roles = process_role(value)
6
- person = nil
7
- if value.displayForm.length > 0
8
- person = ModsDisplay::Name::Person.new(:name => value.displayForm.text, :roles => roles)
9
- elsif !name_parts(value).empty?
10
- person = ModsDisplay::Name::Person.new(:name => name_parts(value), :roles => roles)
11
- end
12
- ModsDisplay::Values.new(:label => displayLabel(value) || name_label(value), :values => [person]) if person
13
- end.compact
14
- collapse_fields(return_fields)
15
- end
1
+ module ModsDisplay
2
+ class Name < Field
3
+ include ModsDisplay::RelatorCodes
4
+ def fields
5
+ return_fields = @values.map do |value|
6
+ roles = process_role(value)
7
+ person = nil
8
+ if value.displayForm.length > 0
9
+ person = ModsDisplay::Name::Person.new(name: value.displayForm.text, roles: roles)
10
+ elsif !name_parts(value).empty?
11
+ person = ModsDisplay::Name::Person.new(name: name_parts(value), roles: roles)
12
+ end
13
+ ModsDisplay::Values.new(label: displayLabel(value) || name_label(value), values: [person]) if person
14
+ end.compact
15
+ collapse_fields(return_fields)
16
+ end
16
17
 
17
- def to_html
18
- return nil if fields.empty? or @config.ignore?
19
- output = ""
20
- fields.each do |field|
21
- output << "<dt#{label_class} #{sanitized_field_title(field.label)}>#{field.label}</dt>"
22
- output << "<dd#{value_class}>"
18
+ def to_html
19
+ return nil if fields.empty? || @config.ignore?
20
+ output = ''
21
+ fields.each do |field|
22
+ output << "<dt#{label_class} #{sanitized_field_title(field.label)}>#{field.label}</dt>"
23
+ output << "<dd#{value_class}>"
23
24
  output << field.values.map do |val|
24
25
  if @config.link
25
26
  txt = link_to_value(val.name)
@@ -29,132 +30,126 @@ class ModsDisplay::Name < ModsDisplay::Field
29
30
  val.to_s
30
31
  end
31
32
  end.join(@config.delimiter)
32
- output << "</dd>"
33
+ output << '</dd>'
34
+ end
35
+ output
33
36
  end
34
- output
35
- end
36
37
 
37
- private
38
+ private
38
39
 
39
- def name_label(element)
40
- if !has_role?(element) || is_primary?(element) || has_author_or_creator_roles?(element)
41
- I18n.t('mods_display.author_creator')
42
- else
43
- I18n.t('mods_display.contributor')
40
+ def name_label(element)
41
+ if !role?(element) || primary?(element) || author_or_creator_roles?(element)
42
+ I18n.t('mods_display.author_creator')
43
+ else
44
+ I18n.t('mods_display.contributor')
45
+ end
44
46
  end
45
- end
46
47
 
47
- def has_role?(element)
48
- element.respond_to?(:role) and !element.role.empty?
49
- end
48
+ def role?(element)
49
+ element.respond_to?(:role) && !element.role.empty?
50
+ end
50
51
 
51
- def is_primary?(element)
52
- element.attributes["usage"].respond_to?(:value) and
53
- element.attributes["usage"].value == "primary"
54
- end
52
+ def primary?(element)
53
+ element.attributes['usage'].respond_to?(:value) &&
54
+ element.attributes['usage'].value == 'primary'
55
+ end
55
56
 
56
- def has_author_or_creator_roles?(element)
57
- begin
58
- ["author", "aut", "creator", "cre", ""].include?(element.role.roleTerm.text.downcase)
57
+ def author_or_creator_roles?(element)
58
+ ['author', 'aut', 'creator', 'cre', ''].include?(element.role.roleTerm.text.downcase)
59
59
  rescue
60
60
  false
61
61
  end
62
- end
63
62
 
64
- def name_parts(element)
65
- output = ""
66
- output << [unqualified_name_parts(element),
67
- qualified_name_parts(element, "family"),
68
- qualified_name_parts(element, "given")].flatten.compact.join(", ")
69
- terms = qualified_name_parts(element, "termsOfAddress")
70
- unless terms.empty?
71
- term_delimiter = ", "
72
- if name_part_begins_with_roman_numeral?(terms.first)
73
- term_delimiter = " "
63
+ def name_parts(element)
64
+ output = ''
65
+ output << [unqualified_name_parts(element),
66
+ qualified_name_parts(element, 'family'),
67
+ qualified_name_parts(element, 'given')].flatten.compact.join(', ')
68
+ terms = qualified_name_parts(element, 'termsOfAddress')
69
+ unless terms.empty?
70
+ term_delimiter = ', '
71
+ term_delimiter = ' ' if name_part_begins_with_roman_numeral?(terms.first)
72
+ output = [output, terms.join(', ')].flatten.compact.join(term_delimiter)
74
73
  end
75
- output = [output, terms.join(", ")].flatten.compact.join(term_delimiter)
74
+ dates = qualified_name_parts(element, 'date')
75
+ unless dates.empty?
76
+ output = [output, qualified_name_parts(element, 'date')].flatten.compact.join(', ')
77
+ end
78
+ output
76
79
  end
77
- dates = qualified_name_parts(element, "date")
78
- unless dates.empty?
79
- output = [output, qualified_name_parts(element, "date")].flatten.compact.join(", ")
80
+
81
+ def unqualified_name_parts(element)
82
+ element.namePart.map do |part|
83
+ part.text unless part.attributes['type']
84
+ end.compact
80
85
  end
81
- output
82
- end
83
86
 
84
- def unqualified_name_parts(element)
85
- element.namePart.map do |part|
86
- part.text unless part.attributes["type"]
87
- end.compact
88
- end
87
+ def qualified_name_parts(element, type)
88
+ element.namePart.map do |part|
89
+ if part.attributes['type'].respond_to?(:value) &&
90
+ part.attributes['type'].value == type
91
+ part.text
92
+ end
93
+ end.compact
94
+ end
89
95
 
90
- def qualified_name_parts(element, type)
91
- element.namePart.map do |part|
92
- if part.attributes["type"].respond_to?(:value) and
93
- part.attributes["type"].value == type
94
- part.text
96
+ def name_part_begins_with_roman_numeral?(part)
97
+ first_part = part.split(/\s|,/).first.strip
98
+ first_part.chars.all? do |char|
99
+ %w(I X C L V).include? char
95
100
  end
96
- end.compact
97
- end
98
-
99
- def name_part_begins_with_roman_numeral?(part)
100
- first_part = part.split(/\s|,/).first.strip
101
- first_part.chars.all? do |char|
102
- ["I", "X", "C", "L", "V"].include? char
103
101
  end
104
- end
105
102
 
106
- def process_role(element)
107
- if element.role.length > 0 and element.role.roleTerm.length > 0
103
+ def process_role(element)
104
+ return unless element.role.length > 0 && element.role.roleTerm.length > 0
108
105
  if unencoded_role_term?(element)
109
106
  unencoded_role_term(element)
110
107
  else
111
108
  element.role.roleTerm.map do |term|
112
- if term.attributes["type"].respond_to?(:value) and
113
- term.attributes["type"].value == "code" and
114
- term.attributes["authority"].respond_to?(:value) and
115
- term.attributes["authority"].value == "marcrelator" and
116
- relator_codes.include?(term.text.strip)
117
- relator_codes[term.text.strip]
118
- end
109
+ next unless term.attributes['type'].respond_to?(:value) &&
110
+ term.attributes['type'].value == 'code' &&
111
+ term.attributes['authority'].respond_to?(:value) &&
112
+ term.attributes['authority'].value == 'marcrelator' &&
113
+ relator_codes.include?(term.text.strip)
114
+ relator_codes[term.text.strip]
119
115
  end.compact
120
116
  end
121
117
  end
122
- end
123
118
 
124
- def unencoded_role_term(element)
125
- roles = element.role.map do |role|
126
- role.roleTerm.find do |term|
127
- term.attributes["type"].respond_to?(:value) and
128
- term.attributes["type"].value == "text"
119
+ def unencoded_role_term(element)
120
+ roles = element.role.map do |role|
121
+ role.roleTerm.find do |term|
122
+ term.attributes['type'].respond_to?(:value) &&
123
+ term.attributes['type'].value == 'text'
124
+ end
125
+ end.compact
126
+ roles = element.role.map do |role|
127
+ role.roleTerm.find do |term|
128
+ !term.attributes['type'].respond_to?(:value)
129
+ end
130
+ end.compact if roles.empty?
131
+ roles.map { |t| t.text.strip }
132
+ end
133
+
134
+ def unencoded_role_term?(element)
135
+ element.role.roleTerm.any? do |term|
136
+ !term.attributes['type'].respond_to?(:value) ||
137
+ term.attributes['type'].value == 'text'
129
138
  end
130
- end.compact
131
- roles = element.role.map do |role|
132
- role.roleTerm.find do |term|
133
- !term.attributes["type"].respond_to?(:value)
139
+ end
140
+
141
+ class Person
142
+ attr_accessor :name, :roles
143
+ def initialize(data)
144
+ @name = data[:name]
145
+ @roles = data[:roles] && !data[:roles].empty? ? data[:roles] : nil
134
146
  end
135
- end.compact if roles.empty?
136
- roles.map{|t| t.text.strip }
137
- end
138
147
 
139
- def unencoded_role_term?(element)
140
- element.role.roleTerm.any? do |term|
141
- !term.attributes["type"].respond_to?(:value) or
142
- term.attributes["type"].value == "text"
143
- end
144
- end
145
-
146
- class Person
147
- attr_accessor :name, :roles
148
- def initialize(data)
149
- @name = data[:name]
150
- @roles = data[:roles] && !data[:roles].empty? ? data[:roles] : nil
151
- end
152
-
153
- def to_s
154
- text = @name.dup
155
- text << " (#{@roles.join(', ')})" if @roles
156
- text
148
+ def to_s
149
+ text = @name.dup
150
+ text << " (#{@roles.join(', ')})" if @roles
151
+ text
152
+ end
157
153
  end
158
154
  end
159
-
160
- end
155
+ end
@@ -1,41 +1,40 @@
1
- class ModsDisplay::Note < ModsDisplay::Field
2
-
3
- def fields
4
- return_fields = note_fields.map do |value|
5
- ModsDisplay::Values.new(:label => displayLabel(value) || note_label(value), :values => [value.text])
1
+ module ModsDisplay
2
+ class Note < Field
3
+ def fields
4
+ return_fields = note_fields.map do |value|
5
+ ModsDisplay::Values.new(label: displayLabel(value) || note_label(value), values: [value.text])
6
+ end
7
+ collapse_fields(return_fields)
6
8
  end
7
- collapse_fields(return_fields)
8
- end
9
-
10
-
11
- private
12
9
 
13
- def note_fields
14
- @values.select do |value|
15
- (!value.attributes["type"].respond_to?(:value) or
16
- (value.attributes["type"].respond_to?(:value) and
17
- value.attributes["type"].value.downcase != "contact"))
10
+ private
11
+
12
+ def note_fields
13
+ @values.select do |value|
14
+ (!value.attributes['type'].respond_to?(:value) ||
15
+ (value.attributes['type'].respond_to?(:value) &&
16
+ value.attributes['type'].value.downcase != 'contact'))
17
+ end
18
18
  end
19
- end
20
19
 
21
- def note_label(element)
22
- if element.attributes["type"].respond_to?(:value)
23
- return note_labels[element.attributes["type"].value] || "#{element.attributes["type"].value.capitalize}:"
20
+ def note_label(element)
21
+ if element.attributes['type'].respond_to?(:value)
22
+ return note_labels[element.attributes['type'].value] || "#{element.attributes['type'].value.capitalize}:"
23
+ end
24
+ I18n.t('mods_display.note')
24
25
  end
25
- I18n.t('mods_display.note')
26
- end
27
26
 
28
- def note_labels
29
- {"statement of responsibility" => I18n.t('mods_display.statement_of_responsibility'),
30
- "date/sequential designation" => I18n.t('mods_display.date_sequential_designation'),
31
- "publications" => I18n.t('mods_display.publications'),
32
- "references" => I18n.t('mods_display.references'),
33
- "bibliography" => I18n.t('mods_display.bibliography'),
34
- "preferred citation" => I18n.t('mods_display.preferred_citation'),
35
- "biographical/historical" => I18n.t('mods_display.biographical_historical'),
36
- "creation/production credits" => I18n.t('mods_display.creation_production_credits'),
37
- "citation/reference" => I18n.t('mods_display.citation_reference')
38
- }
27
+ def note_labels
28
+ { 'statement of responsibility' => I18n.t('mods_display.statement_of_responsibility'),
29
+ 'date/sequential designation' => I18n.t('mods_display.date_sequential_designation'),
30
+ 'publications' => I18n.t('mods_display.publications'),
31
+ 'references' => I18n.t('mods_display.references'),
32
+ 'bibliography' => I18n.t('mods_display.bibliography'),
33
+ 'preferred citation' => I18n.t('mods_display.preferred_citation'),
34
+ 'biographical/historical' => I18n.t('mods_display.biographical_historical'),
35
+ 'creation/production credits' => I18n.t('mods_display.creation_production_credits'),
36
+ 'citation/reference' => I18n.t('mods_display.citation_reference')
37
+ }
38
+ end
39
39
  end
40
-
41
- end
40
+ end