Class: Pubid::Core::HarmonizedStageCode
- Inherits:
-
Object
- Object
- Pubid::Core::HarmonizedStageCode
- Includes:
- Comparable
- Defined in:
- lib/pubid/core/harmonized_stage_code.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
attr_accessor :stage, :substage.
-
#harmonized_typed_stages ⇒ Object
attr_accessor :stage, :substage.
-
#stages ⇒ Object
attr_accessor :stage, :substage.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #description ⇒ Object
- #fuzzy? ⇒ Boolean
-
#initialize(stage_or_code, substage = "00", config:) ⇒ HarmonizedStageCode
constructor
A new instance of HarmonizedStageCode.
- #stage ⇒ Object
- #substage ⇒ Object
- #to_s ⇒ Object
- #validate_stages ⇒ Object
Constructor Details
#initialize(stage_or_code, substage = "00", config:) ⇒ HarmonizedStageCode
Returns a new instance of HarmonizedStageCode.
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
#config ⇒ Object
attr_accessor :stage, :substage
5 6 7 |
# File 'lib/pubid/core/harmonized_stage_code.rb', line 5 def config @config end |
#harmonized_typed_stages ⇒ Object
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 |
#stages ⇒ Object
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 |
#description ⇒ Object
84 85 86 |
# File 'lib/pubid/core/harmonized_stage_code.rb', line 84 def description config.stages["codes_description"][to_s] end |
#fuzzy? ⇒ Boolean
66 67 68 |
# File 'lib/pubid/core/harmonized_stage_code.rb', line 66 def fuzzy? @stages.length > 1 end |
#stage ⇒ Object
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 |
#substage ⇒ Object
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_s ⇒ Object
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_stages ⇒ Object
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 |