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>L|C|W|E) # 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.



41
42
43
44
# File 'lib/sof/parser.rb', line 41

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

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



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

def match
  @match
end

#notationObject (readonly)

Returns the value of attribute notation.



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

def notation
  @notation
end

Class Method Details

.dormant_capable_kindsObject



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

def self.dormant_capable_kinds = %w[E W]

.for(notation_or_parser) ⇒ Object



25
26
27
28
29
# File 'lib/sof/parser.rb', line 25

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



31
32
33
34
35
36
37
38
39
# File 'lib/sof/parser.rb', line 31

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



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

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

#activated_notation(date) ⇒ Object



61
62
63
64
65
# File 'lib/sof/parser.rb', line 61

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)


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

def active? = !dormant?

#dormant?Boolean

Returns:

  • (Boolean)


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

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

#dormant_capable?Boolean

Returns:

  • (Boolean)


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

def dormant_capable? = kind.in?(dormant_capable_kinds)

#fromObject



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

def from = match[:from]

#from_dataObject



95
96
97
98
99
# File 'lib/sof/parser.rb', line 95

def from_data
  return {} unless from

  {from: from}
end

#from_dateObject



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

def from_date = match[:from_date]

#inspectObject Also known as: to_s



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

def inspect = notation

#kindObject



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

def kind = match[:kind]

#parses?(notation_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def parses?(notation_id) = kind == notation_id

#period_countObject



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

def period_count = match[:period_count]

#period_keyObject



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

def period_key = match[:period_key]

#time_spanObject

Return a TimeSpan object for the period and period_count



52
53
54
# File 'lib/sof/parser.rb', line 52

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

#to_hObject



69
70
71
72
73
74
75
76
77
# File 'lib/sof/parser.rb', line 69

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid? = match.present?

#volObject



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

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

#volumeObject



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

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