Class: Pubid::Core::Stage

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

Direct Known Subclasses

TypedStage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, abbr: nil, harmonized_code: nil) ⇒ Stage

Returns a new instance of Stage.

Parameters:

  • abbr (String, Symbol) (defaults to: nil)

    abbreviation eg. :PWI, :WD

  • harmonized_code (String, Float, HarmonizedStageCode) (defaults to: nil)
  • config (Configuration)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pubid/core/stage.rb', line 8

def initialize(config:, abbr: nil, harmonized_code: nil)
  @config = config
  @abbr = abbr&.to_s

  if harmonized_code
    @harmonized_code = if harmonized_code.is_a?(HarmonizedStageCode)
                         harmonized_code
                       else
                         HarmonizedStageCode.new(harmonized_code, config: config)
                       end
    @abbr ||= lookup_abbr(@harmonized_code.stages)
  end

  if abbr
    raise Errors::StageInvalidError, "#{abbr} is not valid stage" unless config.stages["abbreviations"].key?(abbr.to_s)

    @harmonized_code ||= HarmonizedStageCode.new(lookup_code(abbr), config: config)
  end
end

Instance Attribute Details

#abbrObject

Returns the value of attribute abbr.



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

def abbr
  @abbr
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#harmonized_codeObject

Returns the value of attribute harmonized_code.



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

def harmonized_code
  @harmonized_code
end

Class Method Details

.has_stage?(stage, config:) ⇒ Boolean

Returns true if stage exists.

Returns:

  • (Boolean)

    true if stage exists



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

def self.has_stage?(stage, config:)
  if stage.is_a?(Stage)
    config.stages["abbreviations"].key?(stage.abbr.to_sym)
  else
    config.stages["abbreviations"].key?(stage.to_s) || /\A[\d.]+\z/.match?(stage)
  end
end

.parse(stage, config:) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/pubid/core/stage.rb', line 57

def self.parse(stage, config:)
  if /\A[\d.]+\z/.match?(stage)
    new(harmonized_code: stage, config: config)
  else
    raise Errors::StageInvalidError unless stage.is_a?(Symbol) || stage.is_a?(String) || stage.is_a?(Parslet::Slice)

    new(abbr: stage.to_s, config: config)
  end
end

Instance Method Details

#==(other) ⇒ Object

Compares one stage with another



77
78
79
80
81
82
83
84
85
# File 'lib/pubid/core/stage.rb', line 77

def ==(other)
  return false unless other

  unless other.is_a?(self.class)
    return false
    # other = self.class.parse(other, config: config)
  end
  other&.harmonized_code == harmonized_code
end

#empty_abbr?(with_prf: false) ⇒ Boolean

Returns false if there are output for abbreviation should be produced.

Parameters:

  • with_prf (Boolean) (defaults to: false)

Returns:

  • (Boolean)

    false if there are output for abbreviation should be produced



94
95
96
97
98
# File 'lib/pubid/core/stage.rb', line 94

def empty_abbr?(with_prf: false)
  return true if abbr == "PRF" && !with_prf

  abbr.nil?
end

#lookup_abbr(lookup_code) ⇒ Object

Lookup for abbreviated code by numeric stage code

Parameters:

  • lookup_code (String, Array)

    stage code or array of stage codes, e.g. “00.00”, “20.20”, [“00.00”, “00.20”]



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pubid/core/stage.rb', line 31

def lookup_abbr(lookup_code)
  lookup_code = lookup_code.first if lookup_code.is_a?(Array) && lookup_code.count == 1

  config.stages["abbreviations"].each do |abbr, codes|
    case codes
    when Array
      if lookup_code.is_a?(Array)
        return abbr if codes == lookup_code
      else
        return abbr if codes.include?(lookup_code)
      end
      # codes.each do |code|
      #   return abbr if code == lookup_code
      # end
    when lookup_code
      return abbr
    end
  end

  nil
end

#lookup_code(lookup_abbr) ⇒ Object



53
54
55
# File 'lib/pubid/core/stage.rb', line 53

def lookup_code(lookup_abbr)
  config.stages["abbreviations"][lookup_abbr.to_s]
end

#nameObject

Return stage name, eg. “Draft International Standard” for “DIS”



88
89
90
# File 'lib/pubid/core/stage.rb', line 88

def name
  config.stages["names"][abbr.to_s]
end

#to_hObject



104
105
106
# File 'lib/pubid/core/stage.rb', line 104

def to_h
  abbr
end

#to_s(opts = {}) ⇒ Object



100
101
102
# File 'lib/pubid/core/stage.rb', line 100

def to_s(opts = {})
  empty_abbr?(**opts) ? "" : abbr
end