Module: Pubid::Rendering::Common
- Defined in:
- lib/pubid/rendering/common.rb
Overview
Common rendering methods for identifier string representation
This module extracts duplicated rendering logic across flavors to reduce code duplication and ensure consistency.
Usage
Include this module in flavor-specific identifier base classes:
class Base < ::Pubid::Identifier
include Pubid::Rendering::Common
def to_s(lang: :en, lang_single: false, with_edition: false)
render_identifier(lang: lang, lang_single: lang_single, with_edition: with_edition)
end
end
Instance Method Summary collapse
-
#copublishers_list(publisher) ⇒ Array<String>
Get copublishers as array.
-
#publisher_body(publisher) ⇒ String
Get publisher body/name.
-
#publisher_has_copublisher?(publisher) ⇒ Boolean
Check if publisher has copublishers.
-
#render_date(date, include_month: false, date_separator: "-") ⇒ String
Render date with optional year/month/day.
-
#render_edition(edition, with_prefix: true) ⇒ String
Render edition component.
-
#render_full_identifier(publisher:, stage: nil, type: nil, number: nil, part: nil, subpart: nil, stage_iteration: nil, date: nil, edition: nil, languages: nil, lang: :en, lang_single: false, with_edition: false, default_type_abbr: "IS") ⇒ String
Render full identifier with all components.
-
#render_languages(languages, lang_single: false, lang_separator: "/") ⇒ String
Render language codes.
-
#render_numbering(number, part = nil, subpart = nil, part_separator: "-") ⇒ String
Render number with optional parts.
-
#render_publisher(publisher, copublisher_separator: "/") ⇒ String
Render publisher with optional copublishers.
-
#render_stage_iteration(stage_iteration) ⇒ String
Render stage iteration (e.g., “.1” for stage iteration).
-
#render_stage_type(stage, type, has_copublisher: false, default_type_abbr: "IS", stage_separator: "/") ⇒ String
Render stage and type abbreviations with proper separators.
Instance Method Details
#copublishers_list(publisher) ⇒ Array<String>
Get copublishers as array
163 164 165 166 167 |
# File 'lib/pubid/rendering/common.rb', line 163 def copublishers_list(publisher) return [] unless publisher.is_a?(Components::Publisher) && publisher.copublisher&.any? publisher.copublisher.map { |cp| publisher_body(cp) } end |
#publisher_body(publisher) ⇒ String
Get publisher body/name
155 156 157 |
# File 'lib/pubid/rendering/common.rb', line 155 def publisher_body(publisher) publisher.is_a?(Components::Publisher) ? publisher.body : publisher.to_s end |
#publisher_has_copublisher?(publisher) ⇒ Boolean
Check if publisher has copublishers
147 148 149 |
# File 'lib/pubid/rendering/common.rb', line 147 def publisher_has_copublisher?(publisher) publisher.is_a?(Components::Publisher) && publisher.has_copublisher? end |
#render_date(date, include_month: false, date_separator: "-") ⇒ String
Render date with optional year/month/day
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pubid/rendering/common.rb', line 102 def render_date(date, include_month: false, date_separator: "-") return "" unless date&.year result = ":#{date.year}" if date.month && include_month result += "#{date_separator}#{format('%02d', date.month)}" result += "#{date_separator}#{format('%02d', date.day)}" if date.day end result end |
#render_edition(edition, with_prefix: true) ⇒ String
Render edition component
133 134 135 136 137 138 139 140 141 |
# File 'lib/pubid/rendering/common.rb', line 133 def render_edition(edition, with_prefix: true) return "" unless edition if with_prefix edition.to_s else " #{edition}" end end |
#render_full_identifier(publisher:, stage: nil, type: nil, number: nil, part: nil, subpart: nil, stage_iteration: nil, date: nil, edition: nil, languages: nil, lang: :en, lang_single: false, with_edition: false, default_type_abbr: "IS") ⇒ String
Render full identifier with all components
This is a convenience method that combines all rendering methods in the standard order for most identifier types.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/pubid/rendering/common.rb', line 189 def render_full_identifier(publisher:, stage: nil, type: nil, number: nil, part: nil, subpart: nil, stage_iteration: nil, date: nil, edition: nil, languages: nil, lang: :en, lang_single: false, with_edition: false, default_type_abbr: "IS") result = render_publisher(publisher) result += render_stage_type(stage, type, has_copublisher: publisher_has_copublisher?( publisher, ), default_type_abbr: default_type_abbr) result += render_numbering(number, part, subpart) result += render_stage_iteration(stage_iteration) if stage_iteration result += render_date(date) result += render_edition(edition) if with_edition && edition result += render_languages(languages, lang_single: lang_single) result end |
#render_languages(languages, lang_single: false, lang_separator: "/") ⇒ String
Render language codes
119 120 121 122 123 124 125 126 |
# File 'lib/pubid/rendering/common.rb', line 119 def render_languages(languages, lang_single: false, lang_separator: "/") return "" unless languages&.any? formatted = languages.map do |l| l.to_s(lang_single: lang_single) end.join(lang_separator) "(#{formatted})" end |
#render_numbering(number, part = nil, subpart = nil, part_separator: "-") ⇒ String
Render number with optional parts
78 79 80 81 82 83 84 85 86 |
# File 'lib/pubid/rendering/common.rb', line 78 def render_numbering(number, part = nil, subpart = nil, part_separator: "-") return "" unless number&.value result = " #{number.value}" result += "#{part_separator}#{part.value}" if part&.value result += "#{part_separator}#{subpart.value}" if subpart&.value result end |
#render_publisher(publisher, copublisher_separator: "/") ⇒ String
Render publisher with optional copublishers
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pubid/rendering/common.rb', line 28 def render_publisher(publisher, copublisher_separator: "/") return "" unless publisher result = publisher.to_s cops = publisher.is_a?(Components::Publisher) ? publisher.copublisher : nil if cops&.any? result += cops.map do |cp| "#{copublisher_separator}#{cp}" end.join end result end |
#render_stage_iteration(stage_iteration) ⇒ String
Render stage iteration (e.g., “.1” for stage iteration)
92 93 94 |
# File 'lib/pubid/rendering/common.rb', line 92 def render_stage_iteration(stage_iteration) stage_iteration&.value ? ".#{stage_iteration.value}" : "" end |
#render_stage_type(stage, type, has_copublisher: false, default_type_abbr: "IS", stage_separator: "/") ⇒ String
Render stage and type abbreviations with proper separators
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pubid/rendering/common.rb', line 49 def render_stage_type(stage, type, has_copublisher: false, default_type_abbr: "IS", stage_separator: "/") result = "" # Add stage abbreviation if present if stage&.abbr sep = has_copublisher ? " " : stage_separator result += "#{sep}#{stage.abbr}" end # Add type abbreviation if present and not default if type&.abbr && type.abbr != default_type_abbr # Separator: space after stage or copublisher, slash otherwise has_prefix = stage&.abbr || has_copublisher sep = has_prefix ? " " : stage_separator result += "#{sep}#{type.abbr}" end result end |