Class: Pubid::Bsi::Identifier::Base

Inherits:
Core::Identifier::Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pubid/bsi/identifier/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher: "BS", month: nil, edition: nil, supplement: nil, number: nil, adopted: nil, expert_commentary: false, tracked_changes: false, translation: nil, pdf: false, **opts) ⇒ Base

Returns a new instance of Base.

Parameters:

  • month (Integer) (defaults to: nil)

    document’s month

  • edition (String) (defaults to: nil)

    document’s edition version, e.g. “3.0”, “1.0”



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pubid/bsi/identifier/base.rb', line 12

def initialize(publisher: "BS", month: nil, edition: nil,
               supplement: nil, number: nil, adopted: nil,
               expert_commentary: false, tracked_changes: false,
               translation: nil, pdf: false, **opts)

  super(**opts.merge(publisher: publisher, number: number))
  @month = month if month
  @edition = edition if edition
  @supplement = supplement
  @adopted = adopted
  @tracked_changes = tracked_changes
  @translation = translation
  @pdf = pdf
end

Instance Attribute Details

#adoptedObject

Returns the value of attribute adopted.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def adopted
  @adopted
end

#monthObject

Returns the value of attribute month.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def month
  @month
end

#pdfObject

Returns the value of attribute pdf.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def pdf
  @pdf
end

#supplementObject

Returns the value of attribute supplement.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def supplement
  @supplement
end

#tracked_changesObject

Returns the value of attribute tracked_changes.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def tracked_changes
  @tracked_changes
end

#translationObject

Returns the value of attribute translation.



6
7
8
# File 'lib/pubid/bsi/identifier/base.rb', line 6

def translation
  @translation
end

Class Method Details

.get_parser_classObject



72
73
74
# File 'lib/pubid/bsi/identifier/base.rb', line 72

def get_parser_class
  Parser
end

.get_renderer_classObject



76
77
78
# File 'lib/pubid/bsi/identifier/base.rb', line 76

def get_renderer_class
  Renderer::Base
end

.get_transformer_classObject



80
81
82
# File 'lib/pubid/bsi/identifier/base.rb', line 80

def get_transformer_class
  Transformer
end

.transform(params) ⇒ Object

Use Identifier#create to resolve identifier’s type class



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pubid/bsi/identifier/base.rb', line 35

def transform(params)
  identifier_params = transform_hash(params)

  if identifier_params[:national_annex]
    return transform_national_annex(
      identifier_params[:national_annex],
      identifier_params.dup.tap { |h| h.delete(:national_annex) })
  end

  return transform_expert_commentary(identifier_params) if identifier_params[:expert_commentary]

  return transform_collection(identifier_params) if identifier_params[:second_number]

  Identifier.create(**identifier_params)
end

.transform_collection(params) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/pubid/bsi/identifier/base.rb', line 51

def transform_collection(params)
  first_identifier_params =
    params.reject { |k, _| %i[second_number year supplement].include?(k) }
  second_identifier_params = first_identifier_params.dup
  second_identifier_params[:number] = params[:second_number]
  Collection.new(identifiers: [Identifier.create(**first_identifier_params),
                               Identifier.create(**second_identifier_params)],
                 **params.slice(:supplement, :year))
end

.transform_expert_commentary(base_params) ⇒ Object



61
62
63
64
# File 'lib/pubid/bsi/identifier/base.rb', line 61

def transform_expert_commentary(base_params)
  Identifier.create(type: :ec,
                    base: Identifier.create(**base_params))
end

.transform_hash(params) ⇒ Object



28
29
30
31
32
# File 'lib/pubid/bsi/identifier/base.rb', line 28

def transform_hash(params)
  params.map do |k, v|
    get_transformer_class.new.apply(k => v.is_a?(Hash) ? transform_hash(v) : v)
  end.inject({}, :merge)
end

.transform_national_annex(params, base_params) ⇒ Object



66
67
68
69
70
# File 'lib/pubid/bsi/identifier/base.rb', line 66

def transform_national_annex(params, base_params)
  Identifier.create(type: params[:type],
                    supplement: params[:supplement],
                    base: Identifier.create(**base_params))
end