Class: Pubid::Core::Renderer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/core/renderer/base.rb

Direct Known Subclasses

Urn

Constant Summary collapse

LANGUAGES =
{
  "ru" => "R",
  "fr" => "F",
  "en" => "E",
  "ar" => "A",
  "es" => "S",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



13
14
15
# File 'lib/pubid/core/renderer/base.rb', line 13

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/pubid/core/renderer/base.rb', line 3

def params
  @params
end

Instance Method Details

#prerender(**opts) ⇒ Renderer::Base

Prerender parameters and store in @prerendered_params

Parameters:

  • opts (Hash)

    options for render method, eg. { with_language_code: :single, with_date: true }

Returns:



20
21
22
23
24
# File 'lib/pubid/core/renderer/base.rb', line 20

def prerender(**opts)
  @prerendered_params = prerender_params(@params, opts)
  @prerendered_params.default = ""
  self
end

#prerender_params(params, opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/pubid/core/renderer/base.rb', line 33

def prerender_params(params, opts)
  params.map do |key, value|
    next unless value
    if respond_to?("render_#{key}")
      [key, send("render_#{key}", value, opts, params)]
    else
      [key, value]
    end
  end.compact.to_h
end

#render(with_date: true, with_language_code: :iso, **args) ⇒ Object

Render identifier

Parameters:

  • with_date (Boolean) (defaults to: true)

    include year in output

  • with_language_code (:iso, :single) (defaults to: :iso)

    render document language as 2-letter ISO 639-1 language code or single code



81
82
83
# File 'lib/pubid/core/renderer/base.rb', line 81

def render(with_date: true, with_language_code: :iso, **args)
  render_base_identifier(**args.merge({ with_date: with_date, with_language_code: with_language_code})) + @prerendered_params[:language].to_s
end

#render_amendments(amendments, _opts, _params) ⇒ Object



102
103
104
# File 'lib/pubid/core/renderer/base.rb', line 102

def render_amendments(amendments, _opts, _params)
  amendments.sort.map(&:render_pubid).join("+")
end

#render_base_identifier(**args) ⇒ Object



26
27
28
29
30
# File 'lib/pubid/core/renderer/base.rb', line 26

def render_base_identifier(**args)
  prerender(**args)

  render_identifier(@prerendered_params)
end

#render_corrigendums(corrigendums, _opts, _params) ⇒ Object



106
107
108
# File 'lib/pubid/core/renderer/base.rb', line 106

def render_corrigendums(corrigendums, _opts, _params)
  corrigendums.sort.map(&:render_pubid).join("+")
end

#render_identifier(params) ⇒ Object



85
86
87
# File 'lib/pubid/core/renderer/base.rb', line 85

def render_identifier(params)
  render_base(params)
end

#render_language(language, opts, _params) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/pubid/core/renderer/base.rb', line 119

def render_language(language, opts, _params)
  if opts[:with_language_code] == :single
    "(#{LANGUAGES[language]})"
  else
    "(#{language})"
  end
end

#render_part(part, opts, _params) ⇒ Object



111
112
113
# File 'lib/pubid/core/renderer/base.rb', line 111

def render_part(part, opts, _params)
  "-#{part}"
end

#render_publisher(publisher, _opts, params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pubid/core/renderer/base.rb', line 89

def render_publisher(publisher, _opts, params)
  return publisher unless params[:copublisher]

  case params[:copublisher]
  when Array
    ([publisher] + params[:copublisher].map(&:to_s).sort.map(&:to_s)).map do |pub|
      pub.gsub('-', '/')
    end.join("/")
  else
    [publisher, params[:copublisher]].join("/")
  end
end

#render_supplement(supplement_params, **args) ⇒ Object

Renders amendment and corrigendum when applied through identifier type



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pubid/core/renderer/base.rb', line 45

def render_supplement(supplement_params, **args)
  base = if supplement_params[:base].type == :amd
           # render inner amendment (cor -> amd -> base)
           render_supplement(supplement_params[:base].to_h, **args)
         else
           self.class.new(supplement_params[:base].to_h).render_base_identifier(
             # always render year for identifiers with supplement
             **args.merge({ with_date: true }),
           )
         end
  supplement = case supplement_params[:typed_stage].type.type
               when :amd
                 render_amendments(
                   [Pubid::Iso::Amendment.new(**supplement_params.slice(:number, :year, :typed_stage, :edition, :iteration))],
                   args,
                   nil,
                 )
               when :cor
                 render_corrigendums(
                   [Pubid::Iso::Corrigendum.new(**supplement_params.slice(:number, :year, :typed_stage, :edition, :iteration))],
                   args,
                   nil,
                 )
                 # copy parameters from Identifier only supported by Corrigendum
               end

  if supplement_params[:base].language
    base + supplement + render_language(supplement_params[:base].language, args, nil)
  else
    base + supplement
  end
end

#render_year(year, opts, _params) ⇒ Object



115
116
117
# File 'lib/pubid/core/renderer/base.rb', line 115

def render_year(year, opts, _params)
  opts[:with_date] && ":#{year}" || ""
end