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",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



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

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:



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

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

#prerender_params(params, opts) ⇒ Object



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

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



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

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



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

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

#render_base_identifier(**args) ⇒ Object



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

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

  render_identifier(@prerendered_params)
end

#render_corrigendums(corrigendums, _opts, _params) ⇒ Object



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

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

#render_identifier(params) ⇒ Object



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

def render_identifier(params)
  render_base(params)
end

#render_language(language, opts, _params) ⇒ Object



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

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

#render_part(part, opts, _params) ⇒ Object



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

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

#render_publisher(publisher, _opts, params) ⇒ Object



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

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



44
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
# File 'lib/pubid/core/renderer/base.rb', line 44

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



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

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