Class: SOF::Parser
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
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#notation ⇒ Object
readonly
Returns the value of attribute notation.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #activated_notation(date) ⇒ Object
- #active? ⇒ Boolean
- #dormant? ⇒ Boolean
- #dormant_capable? ⇒ Boolean
- #from ⇒ Object
- #from_data ⇒ Object
- #from_date ⇒ Object
-
#initialize(notation) ⇒ Parser
constructor
A new instance of Parser.
- #inspect ⇒ Object (also: #to_s)
- #kind ⇒ Object
- #parses?(notation_id) ⇒ Boolean
- #period_count ⇒ Object
- #period_key ⇒ Object
-
#time_span ⇒ Object
Return a TimeSpan object for the period and period_count.
- #to_h ⇒ Object
- #valid? ⇒ Boolean
- #vol ⇒ Object
- #volume ⇒ Object
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
#match ⇒ Object (readonly)
Returns the value of attribute match.
49 50 51 |
# File 'lib/sof/parser.rb', line 49 def match @match end |
#notation ⇒ Object (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_kinds ⇒ Object
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
84 |
# File 'lib/sof/parser.rb', line 84 def active? = !dormant? |
#dormant? ⇒ Boolean
86 |
# File 'lib/sof/parser.rb', line 86 def dormant? = dormant_capable? && from_date.nil? |
#dormant_capable? ⇒ Boolean
88 |
# File 'lib/sof/parser.rb', line 88 def dormant_capable? = kind.in?(dormant_capable_kinds) |
#from ⇒ Object
106 |
# File 'lib/sof/parser.rb', line 106 def from = match[:from] |
#from_data ⇒ Object
98 99 100 101 102 |
# File 'lib/sof/parser.rb', line 98 def from_data return {} unless from {from: from} end |
#from_date ⇒ Object
104 |
# File 'lib/sof/parser.rb', line 104 def from_date = match[:from_date] |
#inspect ⇒ Object Also known as: to_s
61 |
# File 'lib/sof/parser.rb', line 61 def inspect = notation |
#kind ⇒ Object
108 |
# File 'lib/sof/parser.rb', line 108 def kind = match[:kind] |
#parses?(notation_id) ⇒ Boolean
82 |
# File 'lib/sof/parser.rb', line 82 def parses?(notation_id) = kind == notation_id |
#period_count ⇒ Object
90 |
# File 'lib/sof/parser.rb', line 90 def period_count = match[:period_count] |
#period_key ⇒ Object
92 |
# File 'lib/sof/parser.rb', line 92 def period_key = match[:period_key] |
#time_span ⇒ Object
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_h ⇒ Object
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
59 |
# File 'lib/sof/parser.rb', line 59 def valid? = match.present? |
#vol ⇒ Object
94 |
# File 'lib/sof/parser.rb', line 94 def vol = match[:vol] || "V1" |
#volume ⇒ Object
96 |
# File 'lib/sof/parser.rb', line 96 def volume = (match[:volume] || 1).to_i |