Class: Pubid::Ieee::Identifier::Base

Inherits:
Core::Identifier::Base
  • Object
show all
Defined in:
lib/pubid/ieee/identifier/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher: "IEEE", number: nil, stage: nil, subpart: nil, edition: nil, draft: nil, redline: nil, month: nil, revision: nil, iso_identifier: nil, type: :std, alternative: nil, draft_status: nil, adoption_year: nil, amendment: nil, supersedes: nil, corrigendum: nil, corrigendum_comment: nil, reaffirmed: nil, incorporates: nil, supplement: nil, proposal: nil, iso_amendment: nil, iteration: nil, includes: nil, adoption: nil, year: nil, day: nil, **opts) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/pubid/ieee/identifier/base.rb', line 15

def initialize(publisher: "IEEE", number: nil, stage: nil, subpart: nil, edition: nil,
               draft: nil, redline: nil, month: nil, revision: nil,
               iso_identifier: nil, type: :std, alternative: nil,
               draft_status: nil, adoption_year: nil,
               amendment: nil, supersedes: nil, corrigendum: nil,
               corrigendum_comment: nil, reaffirmed: nil,
               incorporates: nil, supplement: nil, proposal: nil,
               iso_amendment: nil, iteration: nil, includes: nil, adoption: nil,
               year: nil, day: nil, **opts)

  super(**opts.merge(number: number, publisher: publisher))#.merge(amendments: amendments, corrigendums: corrigendums))

  @edition = edition if edition

  @proposal = @number.to_s[0] == "P"
  @revision = revision
  if iso_identifier
    @iso_identifier = Pubid::Iso::Identifier.parse(iso_identifier)
  elsif draft# && type != :p
    @type = Type.new(:draft)
  elsif type
    if type.is_a?(Symbol)
      @type = Type.new(type)
    else
      @type = Type.parse(type)
    end
  else
    @type = Type.new
  end

  @stage = stage
  @subpart = subpart
  @draft = draft
  @redline = redline
  @month = month
  @revision = revision
  @amendment = amendment
  @corrigendum = corrigendum
  @corrigendum_comment = corrigendum_comment
  @alternative = alternative
  @draft_status = draft_status
  @adoption_year = adoption_year
  @supersedes = supersedes
  @reaffirmed = reaffirmed
  @incorporates = incorporates
  @supplement = supplement
  @proposal = proposal
  @iso_amendment = iso_amendment
  @iteration = iteration
  @includes = includes
  @adoption = adoption
  @year = year.is_a?(Array) ? year.first.to_i : year.to_i if year
  @day = day.to_i if day
end

Instance Attribute Details

#adoptionObject

Returns the value of attribute adoption.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def adoption
  @adoption
end

#adoption_yearObject

Returns the value of attribute adoption_year.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def adoption_year
  @adoption_year
end

#alternativeObject

Returns the value of attribute alternative.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def alternative
  @alternative
end

#amendmentObject

Returns the value of attribute amendment.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def amendment
  @amendment
end

#corrigendumObject

Returns the value of attribute corrigendum.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def corrigendum
  @corrigendum
end

#corrigendum_commentObject

Returns the value of attribute corrigendum_comment.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def corrigendum_comment
  @corrigendum_comment
end

#dayObject

Returns the value of attribute day.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def day
  @day
end

#draftObject

Returns the value of attribute draft.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def draft
  @draft
end

#draft_statusObject

Returns the value of attribute draft_status.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def draft_status
  @draft_status
end

#editionObject

Returns the value of attribute edition.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def edition
  @edition
end

#includesObject

Returns the value of attribute includes.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def includes
  @includes
end

#incorporatesObject

Returns the value of attribute incorporates.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def incorporates
  @incorporates
end

#iso_amendmentObject

Returns the value of attribute iso_amendment.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def iso_amendment
  @iso_amendment
end

#iso_identifierObject

Returns the value of attribute iso_identifier.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def iso_identifier
  @iso_identifier
end

#iterationObject

Returns the value of attribute iteration.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def iteration
  @iteration
end

#monthObject

Returns the value of attribute month.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def month
  @month
end

#proposalObject

Returns the value of attribute proposal.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def proposal
  @proposal
end

#reaffirmedObject

Returns the value of attribute reaffirmed.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def reaffirmed
  @reaffirmed
end

#redlineObject

Returns the value of attribute redline.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def redline
  @redline
end

#revisionObject

Returns the value of attribute revision.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def revision
  @revision
end

#subpartObject

Returns the value of attribute subpart.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def subpart
  @subpart
end

#supersedesObject

Returns the value of attribute supersedes.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def supersedes
  @supersedes
end

#supplementObject

Returns the value of attribute supplement.



9
10
11
# File 'lib/pubid/ieee/identifier/base.rb', line 9

def supplement
  @supplement
end

Class Method Details

.add_missing_bracket(code) ⇒ Object



79
80
81
# File 'lib/pubid/ieee/identifier/base.rb', line 79

def self.add_missing_bracket(code)
  code.count("(") > code.count(")") ? "#{code})" : code
end

.get_parser_classObject



167
168
169
# File 'lib/pubid/ieee/identifier/base.rb', line 167

def get_parser_class
  Parser
end

.get_renderer_classObject



159
160
161
# File 'lib/pubid/ieee/identifier/base.rb', line 159

def get_renderer_class
  Renderer::Base
end

.get_transformer_classObject



163
164
165
# File 'lib/pubid/ieee/identifier/base.rb', line 163

def get_transformer_class
  Transformer
end

.transform(params) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pubid/ieee/identifier/base.rb', line 128

def self.transform(params)
  # transform inside parameters

  identifier_params = transform_parameters(
    Identifier.convert_parser_parameters(**params),
  )

  if identifier_params.key?(:draft) && identifier_params[:draft].key?(:year)
    identifier_params[:year] = identifier_params[:draft][:year]
    identifier_params[:month] = identifier_params[:draft][:month]
    identifier_params[:day] = identifier_params[:draft][:day]
    identifier_params[:draft].delete(:year)
    identifier_params[:draft].delete(:month)
  end

  Identifier.create(**identifier_params)
end

.transform_parameters(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pubid/ieee/identifier/base.rb', line 91

def self.transform_parameters(params)
  return params if params == ""

  if params[:iso_identifier]
    if params[:iso_identifier].is_a?(Array)
      params[:iso_identifier] = array_to_hash(params[:iso_identifier])
    end

    if params[:iso_identifier][:month]
      params[:month] = params[:iso_identifier][:month]
      params[:year] = params[:iso_identifier][:year]
      params[:iso_identifier].delete(:year)
      params[:iso_identifier].delete(:month)
    end
  end

  params.map do |k, v|
    case k
    when :parameters, :draft, :alternative
      v = Identifier.merge_parameters(v) if k == :draft
      # apply transformer for each separate item when array provided
      if v.is_a?(Array)
        get_transformer_class.new.apply(k => v.map { |vv| transform_parameters(vv) })
      else
        get_transformer_class.new.apply(k => transform_parameters(v))
      end
    when :iso_identifier
      v = Identifier.merge_parameters(v)
      result = get_transformer_class.new.apply(k => v)
      # apply transformation when output was transformed to IEEE
      result.key?(:iso_identifier) ? result : transform_parameters(result)
    else
      get_transformer_class.new.apply(k => v)
    end
  end.inject({}, :merge)
end

.typeObject



70
71
72
# File 'lib/pubid/ieee/identifier/base.rb', line 70

def self.type
  { key: :std, title: "Standard" }
end

.update_old_code(code) ⇒ Object



83
84
85
86
87
88
# File 'lib/pubid/ieee/identifier/base.rb', line 83

def self.update_old_code(code)
  UPDATE_CODES.each do |from, to|
    code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : /^#{Regexp.escape(from)}$/, to)
  end
  code
end

Instance Method Details

#set_values(hash) ⇒ Object

convert parameters comes from parser to



75
76
77
# File 'lib/pubid/ieee/identifier/base.rb', line 75

def set_values(hash)
  hash.each { |key, value| send("#{key}=", value.is_a?(Parslet::Slice) && value.to_s || value) }
end

#to_s(format = :short, with_trademark: false, annotated: false) ⇒ Object

Parameters:

  • format (:short, :full) (defaults to: :short)


147
148
149
150
151
152
# File 'lib/pubid/ieee/identifier/base.rb', line 147

def to_s(format = :short, with_trademark: false, annotated: false)
  opts = { format: format, with_trademark: with_trademark, annotated: annotated }
  (@iso_identifier ? @iso_identifier.to_s(format: :ref_num_short, with_edition: true, annotated: annotated) : "") +
    self.class.get_renderer_class.new(to_h(deep: false)).render(**opts) +
    (with_trademark ? trademark(@number) : "")
end

#trademark(number) ⇒ Object



154
155
156
# File 'lib/pubid/ieee/identifier/base.rb', line 154

def trademark(number)
  %w(802 2030).include?(number.to_s) ? "\u00AE" : "\u2122"
end