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_params(params, opts) ⇒ Object



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

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

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

render from hash keys



27
28
29
30
31
32
33
34
# File 'lib/pubid/core/renderer/base.rb', line 27

def render(with_date: true, with_language_code: :iso)
  params = prerender_params(@params,
                            { with_date: with_date, with_language_code: with_language_code })
  # render empty string when the key is not exist
  params.default = ""

  render_identifier(params)
end

#render_amendments(amendments, _opts, _params) ⇒ Object



52
53
54
# File 'lib/pubid/core/renderer/base.rb', line 52

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

#render_copublisher(copublisher, _opts, _params) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/pubid/core/renderer/base.rb', line 41

def render_copublisher(copublisher, _opts, _params)
  # ([email protected]_a?(Array) && [@copublisher]) || @copublisher
  if copublisher.is_a?(Array)
    copublisher.map(&:to_s).sort.map do |copublisher|
      "/#{copublisher.gsub('-', '/')}"
    end.join
  else
    "/#{copublisher}"
  end
end

#render_corrigendums(corrigendums, _opts, _params) ⇒ Object



56
57
58
# File 'lib/pubid/core/renderer/base.rb', line 56

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

#render_edition(edition, _opts, _params) ⇒ Object



92
93
94
# File 'lib/pubid/core/renderer/base.rb', line 92

def render_edition(edition, _opts, _params)
  " ED#{edition}"
end

#render_identifier(params) ⇒ Object



36
37
38
39
# File 'lib/pubid/core/renderer/base.rb', line 36

def render_identifier(params)
  "%{publisher}%{copublisher}%{type}%{stage} %{number}%{part}%{iteration}"\
    "%{year}%{edition}%{amendments}%{corrigendums}%{language}" % params
end

#render_iteration(iteration, _opts, _params) ⇒ Object



96
97
98
# File 'lib/pubid/core/renderer/base.rb', line 96

def render_iteration(iteration, _opts, _params)
  ".#{iteration}"
end

#render_language(language, opts, _params) ⇒ Object



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

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

#render_part(part, opts, _params) ⇒ Object



76
77
78
# File 'lib/pubid/core/renderer/base.rb', line 76

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

#render_stage(stage, opts, params) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pubid/core/renderer/base.rb', line 68

def render_stage(stage, opts, params)
  if params[:copublisher]
    " #{stage}"
  else
    "/#{stage}"
  end
end

#render_type(type, opts, params) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pubid/core/renderer/base.rb', line 60

def render_type(type, opts, params)
  if params[:copublisher]
    " #{type}"
  else
    "/#{type}"
  end
end

#render_year(year, opts, _params) ⇒ Object



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

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