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

Instance Method Details

#copublishers_list(publisher) ⇒ Array<String>

Get copublishers as array

Parameters:

Returns:

  • (Array<String>)

    array of copublisher names



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

Parameters:

Returns:

  • (String)

    publisher body



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

Parameters:

Returns:

  • (Boolean)

    true if 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

Parameters:

  • date (Components::Date)

    date component

  • include_month (Boolean) (defaults to: false)

    include month in output

  • date_separator (String) (defaults to: "-")

    separator for date parts (default: “-”)

Returns:

  • (String)

    formatted date string or empty string



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

Parameters:

  • edition (Components::Edition)

    edition component

  • with_prefix (Boolean) (defaults to: true)

    include prefix (default: true)

Returns:

  • (String)

    formatted edition string or empty string



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.

Parameters:

  • publisher (Components::Publisher)

    publisher component

  • stage (Components::Stage) (defaults to: nil)

    stage component

  • type (Components::Type) (defaults to: nil)

    type component

  • number (Components::Code) (defaults to: nil)

    number component

  • part (Components::Code) (defaults to: nil)

    part component

  • subpart (Components::Code) (defaults to: nil)

    subpart component

  • stage_iteration (Components::Code) (defaults to: nil)

    stage iteration component

  • date (Components::Date) (defaults to: nil)

    date component

  • edition (Components::Edition) (defaults to: nil)

    edition component

  • languages (Array<Components::Language>) (defaults to: nil)

    language components

  • lang (Symbol) (defaults to: :en)

    language for rendering (:en or :fr)

  • lang_single (Boolean) (defaults to: false)

    use single char language format

  • with_edition (Boolean) (defaults to: false)

    include edition in output

  • default_type_abbr (String) (defaults to: "IS")

    default type abbreviation to skip

Returns:

  • (String)

    formatted identifier string



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

Parameters:

  • languages (Array<Components::Language>)

    language components

  • lang_single (Boolean) (defaults to: false)

    use single character format

  • lang_separator (String) (defaults to: "/")

    separator between languages (default: “/”)

Returns:

  • (String)

    formatted language string or empty string



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

Parameters:

  • number (Components::Code)

    primary number component

  • part (Components::Code) (defaults to: nil)

    optional part component

  • subpart (Components::Code) (defaults to: nil)

    optional subpart component

  • part_separator (String) (defaults to: "-")

    separator for parts (default: “-”)

Returns:

  • (String)

    formatted numbering string



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

Parameters:

  • publisher (Components::Publisher)

    publisher component

  • copublisher_separator (String) (defaults to: "/")

    separator for copublishers (default: “/”)

Returns:

  • (String)

    formatted publisher string



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)

Parameters:

Returns:

  • (String)

    formatted stage iteration or empty string



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

Parameters:

  • stage (Components::Stage)

    stage component

  • type (Components::Type)

    type component

  • has_copublisher (Boolean) (defaults to: false)

    whether identifier has copublisher

  • default_type_abbr (String) (defaults to: "IS")

    default type abbreviation to skip (default: “IS”)

  • stage_separator (String) (defaults to: "/")

    separator before stage without copublisher

Returns:

  • (String)

    formatted stage/type string



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