Class: Pubid::Core::HarmonizedStageCode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pubid/core/harmonized_stage_code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage_or_code, substage = "00", config:) ⇒ HarmonizedStageCode

Returns a new instance of HarmonizedStageCode.

Parameters:

  • stage_or_code (String, Array<String>)

    stage number or whole harmonized code with substage or list or stages for fuzzy stages eg. “10”, 10, “20.20”, [“10.20”, “20.20”] or stage name eg. “proposal”, “approval” or typed stages eg. :dtr, :fdis

  • substage (Integer, String) (defaults to: "00")

    eg. “00”, 0 or substage name eg. “registration”, “start_of_main_action”



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pubid/core/harmonized_stage_code.rb', line 13

def initialize(stage_or_code, substage = "00", config:)
  @config = config
  @stages = if stage_or_code.is_a?(Array)
              stage_or_code
            elsif stage_or_code.is_a?(String) && (
              config.stages["codes_description"].key?(stage_or_code) ||
                harmonized_typed_stages.include?(stage_or_code))
              [stage_or_code]
            # when stage is typed stage
            elsif stage_or_code.is_a?(Symbol) && config.typed_stages.key?(stage_or_code)
              config.typed_stages[stage_or_code][:harmonized_stages]
            # when stage is stage name
            elsif config.stages["stage_codes"]&.key?(stage_or_code.to_s)
              ["#{config.stages["stage_codes"][stage_or_code.to_s]}.#{config.stages["substage_codes"][substage.to_s]}"]
            else
              # stage is number
              ["#{stage_or_code}.#{substage}"]
            end
  validate_stages
end

Instance Attribute Details

#configObject

attr_accessor :stage, :substage



5
6
7
# File 'lib/pubid/core/harmonized_stage_code.rb', line 5

def config
  @config
end

#harmonized_typed_stagesObject

attr_accessor :stage, :substage



5
6
7
# File 'lib/pubid/core/harmonized_stage_code.rb', line 5

def harmonized_typed_stages
  @harmonized_typed_stages
end

#stagesObject

attr_accessor :stage, :substage



5
6
7
# File 'lib/pubid/core/harmonized_stage_code.rb', line 5

def stages
  @stages
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/pubid/core/harmonized_stage_code.rb', line 62

def ==(other)
  (stages & other.stages) == other.stages
end

#descriptionObject



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

def description
  config.stages["codes_description"][to_s]
end

#fuzzy?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/pubid/core/harmonized_stage_code.rb', line 66

def fuzzy?
  @stages.length > 1
end

#stageObject



70
71
72
73
74
75
76
# File 'lib/pubid/core/harmonized_stage_code.rb', line 70

def stage
  raise Errors::HarmonizedStageRenderingError, "cannot render stage for fuzzy stages" if fuzzy?

  return nil if @stages.empty?

  @stages.first.split(".").first
end

#substageObject



78
79
80
81
82
# File 'lib/pubid/core/harmonized_stage_code.rb', line 78

def substage
  raise Errors::HarmonizedStageRenderingError, "cannot render substage for fuzzy stages" if fuzzy?

  @stages.first.split(".").last
end

#to_sObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pubid/core/harmonized_stage_code.rb', line 50

def to_s
  if fuzzy?
    return "draft" if @stages.all? { |s| config.stages["draft_codes"].include?(s) || config.stages["canceled_codes"].include?(s) }

    return "published" if @stages.all? { |s| config.stages["published_codes"].include?(s) }

    raise Errors::HarmonizedStageRenderingError, "cannot render fuzzy stages"
  else
    @stages.first
  end
end

#validate_stagesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pubid/core/harmonized_stage_code.rb', line 38

def validate_stages
  @stages.each do |stage|
    # raise an error when stage is wrong
    next if config.stages["codes_description"].key?(stage)

    # check typed stages if no stages in config
    next if harmonized_typed_stages.include?(stage)

    raise Errors::HarmonizedStageCodeInvalidError
  end
end