Class: SOF::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sof/parser.rb

Overview

This class is not intended to be referenced directly. This is an internal implementation of Cycle behavior.

Constant Summary collapse

PARTS_REGEX =
/
  ^(?<vol>V(?<volume>\d*))? # optional volume
  (?<set>(?<kind>LE|L|C|W|E|I) # kind
  (?<period_count>\d+) # period count
  (?<period_key>D|W|M|Q|Y)?)? # period_key
  (?<from>F(?<from_date>\d{4}-\d{2}-\d{2}))?$ # optional from
/ix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notation) ⇒ Parser

Returns a new instance of Parser.



44
45
46
47
# File 'lib/sof/parser.rb', line 44

def initialize(notation)
  @notation = notation&.upcase
  @match = @notation&.match(PARTS_REGEX)
end

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



49
50
51
# File 'lib/sof/parser.rb', line 49

def match
  @match
end

#notationObject (readonly)

Returns the value of attribute notation.



49
50
51
# File 'lib/sof/parser.rb', line 49

def notation
  @notation
end

Class Method Details

.dormant_capable_kindsObject



24
25
26
# File 'lib/sof/parser.rb', line 24

def self.dormant_capable_kinds
  Cycle.cycle_handlers.select(&:dormant_capable?).map(&:notation_id).compact
end

.for(notation_or_parser) ⇒ Object



28
29
30
31
32
# File 'lib/sof/parser.rb', line 28

def self.for(notation_or_parser)
  return notation_or_parser if notation_or_parser.is_a? self

  new(notation_or_parser)
end

.load(hash) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/sof/parser.rb', line 34

def self.load(hash)
  hash.symbolize_keys!
  hash.reverse_merge!(volume: 1)
  keys = %i[volume kind period_count period_key]
  str = "V#{hash.values_at(*keys).join}"
  return new(str) unless hash[:from_date]

  new([str, "F#{hash[:from_date]}"].join)
end

Instance Method Details

#==(other) ⇒ Object



70
# File 'lib/sof/parser.rb', line 70

def ==(other) = other.to_h == to_h

#activated_notation(date) ⇒ Object



64
65
66
67
68
# File 'lib/sof/parser.rb', line 64

def activated_notation(date)
  return notation unless dormant_capable?

  self.class.load(to_h.merge(from_date: date.to_date)).notation
end

#active?Boolean

Returns:

  • (Boolean)


84
# File 'lib/sof/parser.rb', line 84

def active? = !dormant?

#dormant?Boolean

Returns:

  • (Boolean)


86
# File 'lib/sof/parser.rb', line 86

def dormant? = dormant_capable? && from_date.nil?

#dormant_capable?Boolean

Returns:

  • (Boolean)


88
# File 'lib/sof/parser.rb', line 88

def dormant_capable? = kind.in?(dormant_capable_kinds)

#fromObject



106
# File 'lib/sof/parser.rb', line 106

def from = match[:from]

#from_dataObject



98
99
100
101
102
# File 'lib/sof/parser.rb', line 98

def from_data
  return {} unless from

  {from: from}
end

#from_dateObject



104
# File 'lib/sof/parser.rb', line 104

def from_date = match[:from_date]

#inspectObject Also known as: to_s



61
# File 'lib/sof/parser.rb', line 61

def inspect = notation

#kindObject



108
# File 'lib/sof/parser.rb', line 108

def kind = match[:kind]

#parses?(notation_id) ⇒ Boolean

Returns:

  • (Boolean)


82
# File 'lib/sof/parser.rb', line 82

def parses?(notation_id) = kind == notation_id

#period_countObject



90
# File 'lib/sof/parser.rb', line 90

def period_count = match[:period_count]

#period_keyObject



92
# File 'lib/sof/parser.rb', line 92

def period_key = match[:period_key]

#time_spanObject

Return a TimeSpan object for the period and period_count



55
56
57
# File 'lib/sof/parser.rb', line 55

def time_span
  @time_span ||= TimeSpan.for(period_count, period_key)
end

#to_hObject



72
73
74
75
76
77
78
79
80
# File 'lib/sof/parser.rb', line 72

def to_h
  {
    volume:,
    kind:,
    period_count:,
    period_key:,
    from_date:
  }
end

#valid?Boolean

Returns:

  • (Boolean)


59
# File 'lib/sof/parser.rb', line 59

def valid? = match.present?

#volObject



94
# File 'lib/sof/parser.rb', line 94

def vol = match[:vol] || "V1"

#volumeObject



96
# File 'lib/sof/parser.rb', line 96

def volume = (match[:volume] || 1).to_i