From 2928d0b78c0afce2593fc50bf55315f5278e573e Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Mon, 27 Jul 2026 11:42:54 -0400 Subject: [PATCH 1/3] Restrict classes to the gem namespace Parser and TimeSpan sat directly under SOF, so this gem owned three constants in a namespace other SOF libraries and applications share. Both are documented as internal implementations of Cycle behavior, so neither has a claim on it. They now nest under Cycle, where version.rb already lives. Nothing is left behind at the old names: an alias would hold the namespace this change exists to release. Changed: `SOF::Parser` and `SOF::TimeSpan` are now `SOF::Cycle::Parser` and `SOF::Cycle::TimeSpan` Version: minor --- lib/sof-cycle.rb | 2 +- lib/sof/cycle.rb | 2 +- lib/sof/cycle/parser.rb | 112 ++++++++++++++ lib/sof/cycle/time_span.rb | 238 ++++++++++++++++++++++++++++++ lib/sof/cycles/within.rb | 2 +- lib/sof/parser.rb | 110 -------------- lib/sof/time_span.rb | 236 ----------------------------- spec/sof/cycle/parser_spec.rb | 246 +++++++++++++++++++++++++++++++ spec/sof/cycle/time_span_spec.rb | 109 ++++++++++++++ spec/sof/parser_spec.rb | 244 ------------------------------ spec/sof/time_span_spec.rb | 107 -------------- 11 files changed, 708 insertions(+), 700 deletions(-) create mode 100644 lib/sof/cycle/parser.rb create mode 100644 lib/sof/cycle/time_span.rb delete mode 100644 lib/sof/parser.rb delete mode 100644 lib/sof/time_span.rb create mode 100644 spec/sof/cycle/parser_spec.rb create mode 100644 spec/sof/cycle/time_span_spec.rb delete mode 100644 spec/sof/parser_spec.rb delete mode 100644 spec/sof/time_span_spec.rb diff --git a/lib/sof-cycle.rb b/lib/sof-cycle.rb index 34aa4c8..714a1a5 100644 --- a/lib/sof-cycle.rb +++ b/lib/sof-cycle.rb @@ -14,4 +14,4 @@ Dir[File.join(__dir__, "sof", "cycles", "*.rb")].each { |file| require file } -require_relative "sof/time_span" +require_relative "sof/cycle/time_span" diff --git a/lib/sof/cycle.rb b/lib/sof/cycle.rb index 287e7e8..140effb 100644 --- a/lib/sof/cycle.rb +++ b/lib/sof/cycle.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "forwardable" -require_relative "parser" +require_relative "cycle/parser" module SOF class Cycle diff --git a/lib/sof/cycle/parser.rb b/lib/sof/cycle/parser.rb new file mode 100644 index 0000000..9dd6538 --- /dev/null +++ b/lib/sof/cycle/parser.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require_relative "../cycle" +require "active_support/core_ext/hash/keys" +require "active_support/core_ext/object/blank" +require "active_support/core_ext/object/inclusion" +require "active_support/core_ext/hash/reverse_merge" +require "active_support/isolated_execution_state" + +module SOF + class Cycle + # This class is not intended to be referenced directly. + # This is an internal implementation of Cycle behavior. + class Parser + extend Forwardable + + PARTS_REGEX = / + ^(?V(?\d*))? # optional volume + (?(?LE|L|C|W|E|I) # kind + (?\d+) # period count + (?D|W|M|Q|Y)?)? # period_key + (?F(?\d{4}-\d{2}-\d{2}))?$ # optional from + /ix + + def self.dormant_capable_kinds + Cycle.cycle_handlers.select(&:dormant_capable?).map(&:notation_id).compact + end + + def self.for(notation_or_parser) + return notation_or_parser if notation_or_parser.is_a? self + + new(notation_or_parser) + end + + 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 + + def initialize(notation) + @notation = notation&.upcase + @match = @notation&.match(PARTS_REGEX) + end + + attr_reader :match, :notation + + delegate [:dormant_capable_kinds] => "self.class" + delegate [:period, :humanized_period] => :time_span + + # Return a TimeSpan object for the period and period_count + def time_span + @time_span ||= TimeSpan.for(period_count, period_key) + end + + def valid? = match.present? + + def inspect = notation + alias_method :to_s, :inspect + + def activated_notation(date) + return notation unless dormant_capable? + + self.class.load(to_h.merge(from_date: date.to_date)).notation + end + + def ==(other) = other.to_h == to_h + + def to_h + { + volume:, + kind:, + period_count:, + period_key:, + from_date: + } + end + + def parses?(notation_id) = kind == notation_id + + def active? = !dormant? + + def dormant? = dormant_capable? && from_date.nil? + + def dormant_capable? = kind.in?(dormant_capable_kinds) + + def period_count = match[:period_count] + + def period_key = match[:period_key] + + def vol = match[:vol] || "V1" + + def volume = (match[:volume] || 1).to_i + + def from_data + return {} unless from + + {from: from} + end + + def from_date = match[:from_date] + + def from = match[:from] + + def kind = match[:kind] + end + end +end diff --git a/lib/sof/cycle/time_span.rb b/lib/sof/cycle/time_span.rb new file mode 100644 index 0000000..86b93dd --- /dev/null +++ b/lib/sof/cycle/time_span.rb @@ -0,0 +1,238 @@ +# frozen_string_literal: true + +module SOF + class Cycle + # This class is not intended to be referenced directly. + # This is an internal implementation of Cycle behavior. + class TimeSpan + extend Forwardable + + # TimeSpan objects map Cycle notations to behaviors for their periods + # + # For example: + # 'M' => TimeSpan::DatePeriod::Month + # 'Y' => TimeSpan::DatePeriod::Year + # Read each DatePeriod subclass for more information. + # + class InvalidPeriod < StandardError; end + + class << self + # Return a time_span for the given count and period + def for(count, period) + case count.to_i + when 0 + TimeSpanNothing + when 1 + TimeSpanOne + else + self + end.new(count, period) + end + + # Return a notation string from a hash + def notation(hash) + return unless hash.key?(:period) && hash[:period].present? + + [ + hash.fetch(:period_count) { 1 }, + notation_id_from_name(hash[:period]) + ].compact.join + end + + # Return the notation character for the given period name + def notation_id_from_name(name) + type = DatePeriod.types.find do |klass| + klass.period.to_s == name.to_s + end + + raise InvalidPeriod, "'#{name}' is not a valid period" unless type + + type.code + end + end + + def notation + [period_count, code].join + end + + # Class used to calculate the windows of time so that + # a TimeSpan object will know the correct end of year, + # quarter, etc. + class DatePeriod + extend Forwardable + + class << self + def for(count, period_notation) + @cached_periods ||= {} + @cached_periods[period_notation] ||= {} + @cached_periods[period_notation][count] ||= (for_notation(period_notation) || self).new(count) + @cached_periods[period_notation][count] + end + + def for_notation(notation) + DatePeriod.types.find do |klass| + klass.code == notation.to_s.upcase + end + end + + def types = @types ||= Set.new + + def inherited(klass) + DatePeriod.types << klass + end + + @period = nil + @code = nil + @interval = nil + attr_reader :period, :code, :interval + end + + delegate [:period, :code, :interval] => "self.class" + + def initialize(count) + @count = count + end + attr_reader :count + + def end_date(date) + @end_date ||= {} + @end_date[date] ||= date + duration + end + + def begin_date(date) + @begin_date ||= {} + @begin_date[date] ||= date - duration + end + + def duration = count.send(period) + + def end_of_period(_) = nil + + def humanized_period + return period if count == 1 + + "#{period}s" + end + + class Year < self + @period = :year + @code = "Y" + @interval = "years" + + def end_of_period(date) + date.end_of_year + end + + def beginning_of_period(date) + date.beginning_of_year + end + end + + class Quarter < self + @period = :quarter + @code = "Q" + @interval = "quarters" + + def duration + (count * 3).months + end + + def end_of_period(date) + date.end_of_quarter + end + + def beginning_of_period(date) + date.beginning_of_quarter + end + end + + class Month < self + @period = :month + @code = "M" + @interval = "months" + + def end_of_period(date) + date.end_of_month + end + + def beginning_of_period(date) + date.beginning_of_month + end + end + + class Week < self + @period = :week + @code = "W" + @interval = "weeks" + + def end_of_period(date) + date.end_of_week + end + + def beginning_of_period(date) + date.beginning_of_week + end + end + + class Day < self + @period = :day + @code = "D" + @interval = "days" + + def end_of_period(date) + date + end + + def beginning_of_period(date) + date + end + end + end + private_constant :DatePeriod + + def initialize(count, period_id) + @count = Integer(count, exception: false) + @window = DatePeriod.for(period_count, period_id) + end + attr_reader :window + + delegate [:end_date, :begin_date, :code] => :window + + def end_date_of_period(date) + window.end_of_period(date) + end + + def begin_date_of_period(date) + window.beginning_of_period(date) + end + + # Integer value for the period count or nil + def period_count + @count + end + + delegate [:period, :duration, :interval, :humanized_period] => :window + + # Return a date according to the rules of the time_span + def final_date(date) + return unless period + + window.end_date(date.to_date) + end + + def to_h + { + period:, + period_count: + } + end + + class TimeSpanNothing < self + end + + class TimeSpanOne < self + def interval = humanized_period + end + end + end +end diff --git a/lib/sof/cycles/within.rb b/lib/sof/cycles/within.rb index 05ab165..325c996 100644 --- a/lib/sof/cycles/within.rb +++ b/lib/sof/cycles/within.rb @@ -24,7 +24,7 @@ def to_s = "#{volume}x within #{date_range}" def extend_period(count) Cycle.for( - Parser.load( + Cycle::Parser.load( parser.to_h.merge(period_count: period_count + count) ).to_s ) diff --git a/lib/sof/parser.rb b/lib/sof/parser.rb deleted file mode 100644 index fca667c..0000000 --- a/lib/sof/parser.rb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -require_relative "cycle" -require "active_support/core_ext/hash/keys" -require "active_support/core_ext/object/blank" -require "active_support/core_ext/object/inclusion" -require "active_support/core_ext/hash/reverse_merge" -require "active_support/isolated_execution_state" - -module SOF - # This class is not intended to be referenced directly. - # This is an internal implementation of Cycle behavior. - class Parser - extend Forwardable - - PARTS_REGEX = / - ^(?V(?\d*))? # optional volume - (?(?LE|L|C|W|E|I) # kind - (?\d+) # period count - (?D|W|M|Q|Y)?)? # period_key - (?F(?\d{4}-\d{2}-\d{2}))?$ # optional from - /ix - - def self.dormant_capable_kinds - Cycle.cycle_handlers.select(&:dormant_capable?).map(&:notation_id).compact - end - - def self.for(notation_or_parser) - return notation_or_parser if notation_or_parser.is_a? self - - new(notation_or_parser) - end - - 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 - - def initialize(notation) - @notation = notation&.upcase - @match = @notation&.match(PARTS_REGEX) - end - - attr_reader :match, :notation - - delegate [:dormant_capable_kinds] => "self.class" - delegate [:period, :humanized_period] => :time_span - - # Return a TimeSpan object for the period and period_count - def time_span - @time_span ||= TimeSpan.for(period_count, period_key) - end - - def valid? = match.present? - - def inspect = notation - alias_method :to_s, :inspect - - def activated_notation(date) - return notation unless dormant_capable? - - self.class.load(to_h.merge(from_date: date.to_date)).notation - end - - def ==(other) = other.to_h == to_h - - def to_h - { - volume:, - kind:, - period_count:, - period_key:, - from_date: - } - end - - def parses?(notation_id) = kind == notation_id - - def active? = !dormant? - - def dormant? = dormant_capable? && from_date.nil? - - def dormant_capable? = kind.in?(dormant_capable_kinds) - - def period_count = match[:period_count] - - def period_key = match[:period_key] - - def vol = match[:vol] || "V1" - - def volume = (match[:volume] || 1).to_i - - def from_data - return {} unless from - - {from: from} - end - - def from_date = match[:from_date] - - def from = match[:from] - - def kind = match[:kind] - end -end diff --git a/lib/sof/time_span.rb b/lib/sof/time_span.rb deleted file mode 100644 index 698150a..0000000 --- a/lib/sof/time_span.rb +++ /dev/null @@ -1,236 +0,0 @@ -# frozen_string_literal: true - -module SOF - # This class is not intended to be referenced directly. - # This is an internal implementation of Cycle behavior. - class TimeSpan - extend Forwardable - - # TimeSpan objects map Cycle notations to behaviors for their periods - # - # For example: - # 'M' => TimeSpan::DatePeriod::Month - # 'Y' => TimeSpan::DatePeriod::Year - # Read each DatePeriod subclass for more information. - # - class InvalidPeriod < StandardError; end - - class << self - # Return a time_span for the given count and period - def for(count, period) - case count.to_i - when 0 - TimeSpanNothing - when 1 - TimeSpanOne - else - self - end.new(count, period) - end - - # Return a notation string from a hash - def notation(hash) - return unless hash.key?(:period) && hash[:period].present? - - [ - hash.fetch(:period_count) { 1 }, - notation_id_from_name(hash[:period]) - ].compact.join - end - - # Return the notation character for the given period name - def notation_id_from_name(name) - type = DatePeriod.types.find do |klass| - klass.period.to_s == name.to_s - end - - raise InvalidPeriod, "'#{name}' is not a valid period" unless type - - type.code - end - end - - def notation - [period_count, code].join - end - - # Class used to calculate the windows of time so that - # a TimeSpan object will know the correct end of year, - # quarter, etc. - class DatePeriod - extend Forwardable - - class << self - def for(count, period_notation) - @cached_periods ||= {} - @cached_periods[period_notation] ||= {} - @cached_periods[period_notation][count] ||= (for_notation(period_notation) || self).new(count) - @cached_periods[period_notation][count] - end - - def for_notation(notation) - DatePeriod.types.find do |klass| - klass.code == notation.to_s.upcase - end - end - - def types = @types ||= Set.new - - def inherited(klass) - DatePeriod.types << klass - end - - @period = nil - @code = nil - @interval = nil - attr_reader :period, :code, :interval - end - - delegate [:period, :code, :interval] => "self.class" - - def initialize(count) - @count = count - end - attr_reader :count - - def end_date(date) - @end_date ||= {} - @end_date[date] ||= date + duration - end - - def begin_date(date) - @begin_date ||= {} - @begin_date[date] ||= date - duration - end - - def duration = count.send(period) - - def end_of_period(_) = nil - - def humanized_period - return period if count == 1 - - "#{period}s" - end - - class Year < self - @period = :year - @code = "Y" - @interval = "years" - - def end_of_period(date) - date.end_of_year - end - - def beginning_of_period(date) - date.beginning_of_year - end - end - - class Quarter < self - @period = :quarter - @code = "Q" - @interval = "quarters" - - def duration - (count * 3).months - end - - def end_of_period(date) - date.end_of_quarter - end - - def beginning_of_period(date) - date.beginning_of_quarter - end - end - - class Month < self - @period = :month - @code = "M" - @interval = "months" - - def end_of_period(date) - date.end_of_month - end - - def beginning_of_period(date) - date.beginning_of_month - end - end - - class Week < self - @period = :week - @code = "W" - @interval = "weeks" - - def end_of_period(date) - date.end_of_week - end - - def beginning_of_period(date) - date.beginning_of_week - end - end - - class Day < self - @period = :day - @code = "D" - @interval = "days" - - def end_of_period(date) - date - end - - def beginning_of_period(date) - date - end - end - end - private_constant :DatePeriod - - def initialize(count, period_id) - @count = Integer(count, exception: false) - @window = DatePeriod.for(period_count, period_id) - end - attr_reader :window - - delegate [:end_date, :begin_date, :code] => :window - - def end_date_of_period(date) - window.end_of_period(date) - end - - def begin_date_of_period(date) - window.beginning_of_period(date) - end - - # Integer value for the period count or nil - def period_count - @count - end - - delegate [:period, :duration, :interval, :humanized_period] => :window - - # Return a date according to the rules of the time_span - def final_date(date) - return unless period - - window.end_date(date.to_date) - end - - def to_h - { - period:, - period_count: - } - end - - class TimeSpanNothing < self - end - - class TimeSpanOne < self - def interval = humanized_period - end - end -end diff --git a/spec/sof/cycle/parser_spec.rb b/spec/sof/cycle/parser_spec.rb new file mode 100644 index 0000000..15002e0 --- /dev/null +++ b/spec/sof/cycle/parser_spec.rb @@ -0,0 +1,246 @@ +# frozen_string_literal: true + +require "spec_helper" + +module SOF + class Cycle + RSpec.describe Parser, type: :value do + describe ".load(hash)" do + it "returns a Parser instance" do + hash = { + volume: 1, + kind: "L", + period_count: "180", + period_key: "D" + } + expect(described_class.load(hash)).to eq described_class.new("V1L180D") + end + end + + describe ".for(notation_or_parser)" do + context "when given a string" do + it "returns a Parser instance" do + expect(described_class.for("V1L180D")).to be_a(described_class) + end + end + + context "when given a Parser object" do + it "returns the object" do + parser = described_class.new("V1L180D") + expect(described_class.for(parser)).to eq parser + end + end + end + + describe "#initialize" do + it "is case-insensitive" do + aggregate_failures do + expect(described_class.new("V1l180D").inspect).to eq "V1L180D" + expect(described_class.new("V1L180d").to_s).to eq "V1L180D" + end + end + end + + describe "#inspect & #to_s" do + it "returns the string representation" do + aggregate_failures do + expect(described_class.new("V1L180D").inspect).to eq "V1L180D" + expect(described_class.new("V1L180D").to_s).to eq "V1L180D" + end + end + end + + describe "#from_data" do + context "when notation has no from date" do + it "returns an empty hash" do + parser = described_class.new("V1L180D") + expect(parser.from_data).to eq({}) + end + end + + context "when notation has a from date" do + it "returns a hash with the from date" do + parser = described_class.new("V1W1MF2024-01-01") + expect(parser.from_data).to eq({from: "F2024-01-01"}) + end + end + end + + describe "#valid?" do + it "returns true if the notation is recognized" do + aggregate_failures do + expect(described_class.new("V1L180D")).to be_valid + expect(described_class.new("V1E18M")).to be_valid + expect(described_class.new("XXX")).not_to be_valid + end + end + end + + describe "#to_h" do + it "returns a hash representation of the notation" do + aggregate_failures do + expect(described_class.new("V1L180D").to_h).to eq({ + volume: 1, + kind: "L", + period_count: "180", + period_key: "D", + from_date: nil + }) + expect(described_class.new("V1W180D").to_h).to eq({ + volume: 1, + kind: "W", + period_count: "180", + period_key: "D", + from_date: nil + }) + expect(described_class.new("V1W180DF2024-05-06").to_h).to eq({ + volume: 1, + kind: "W", + period_count: "180", + period_key: "D", + from_date: "2024-05-06" + }) + end + end + end + + describe "#activated_notation(date)" do + it "returns the activated notation" do + date = "2024-05-06" + aggregate_failures do + expect(described_class.new("V1L180D").activated_notation(date)).to eq( + "V1L180D" + ) + expect(described_class.new("V1W180D").activated_notation(date)).to eq( + "V1W180DF2024-05-06" + ) + expect(described_class.new("V1W180DF2024-09-09").activated_notation(date)).to eq( + "V1W180DF2024-05-06" + ) + expect(described_class.new("V1E18M").activated_notation(date)).to eq( + "V1E18MF2024-05-06" + ) + expect(described_class.new("V1E18MF2024-09-09").activated_notation(date)).to eq( + "V1E18MF2024-05-06" + ) + end + end + end + + describe "#dormant|_capable? & #active?" do + it "returns true if the notation has a dormant variant" do + aggregate_failures do + expect(described_class.new("V1L180D")).not_to be_dormant_capable + expect(described_class.new("V1L180D")).not_to be_dormant + expect(described_class.new("V1L180D")).to be_active + + expect(described_class.new("V1E18M")).to be_dormant_capable + expect(described_class.new("V1E18M")).to be_dormant + expect(described_class.new("V1E18M")).not_to be_active + + expect(described_class.new("V1E18MF2024-09-09")).to be_dormant_capable + expect(described_class.new("V1E18MF2024-09-09")).not_to be_dormant + expect(described_class.new("V1E18MF2024-09-09")).to be_active + + expect(described_class.new("V1W180D")).to be_dormant_capable + expect(described_class.new("V1W180D")).to be_dormant + expect(described_class.new("V1W180D")).not_to be_active + + expect(described_class.new("V1W180DF2024-09-09")).to be_dormant_capable + expect(described_class.new("V1W180DF2024-09-09")).not_to be_dormant + expect(described_class.new("V1W180DF2024-09-09")).to be_active + end + end + end + + describe "#from|_date" do + it "returns match[:from]|match[:from_date]" do + aggregate_failures do + expect(described_class.new("V1L180D").from).to be_nil + expect(described_class.new("V1L180D").from_date).to be_nil + + expect(described_class.new("V1W10D").from).to be_nil + expect(described_class.new("V1W10D").from_date).to be_nil + expect(described_class.new("V1W10DF2024-04-09").from).to eq "F2024-04-09" + expect(described_class.new("V1W10DF2024-04-09").from_date).to eq "2024-04-09" + + expect(described_class.new("V1E18M").from).to be_nil + expect(described_class.new("V1E18M").from_date).to be_nil + expect(described_class.new("V1E18MF2024-04-09").from).to eq "F2024-04-09" + expect(described_class.new("V1E18MF2024-04-09").from_date).to eq "2024-04-09" + end + end + end + + describe "#period_count" do + it "returns match[:period_count]" do + aggregate_failures do + expect(described_class.new("V1L180D").period_count).to eq "180" + expect(described_class.new("V3C1Y").period_count).to eq "1" + expect(described_class.new("V4").period_count).to be_nil + end + end + end + + describe "#period_key" do + it "returns match[:period_key]" do + aggregate_failures do + expect(described_class.new("V1L180D").period_key).to eq "D" + expect(described_class.new("V3C1Y").period_key).to eq "Y" + expect(described_class.new("V4").period_key).to be_nil + end + end + end + + describe "#volume" do + it "returns match[:volume]" do + aggregate_failures do + expect(described_class.new("L180D").volume).to eq 1 + expect(described_class.new("V1L180D").volume).to eq 1 + expect(described_class.new("V3C1Y").volume).to eq 3 + expect(described_class.new("V4").volume).to eq 4 + end + end + end + + describe "#kind" do + it "returns match[:kind]" do + aggregate_failures do + expect(described_class.new("V1E18M").kind).to eq "E" + expect(described_class.new("V1L180D").kind).to eq "L" + expect(described_class.new("V1C1Y").kind).to eq "C" + expect(described_class.new("V1").kind).to be_nil + end + end + end + + describe "#parses?" do + it "returns true if the object parses the notation_id" do + aggregate_failures do + expect(described_class.new("V1E18M").parses?("E")).to be true + expect(described_class.new("V1E18M").parses?("L")).to be false + expect(described_class.new("V1L180D").parses?("L")).to be true + expect(described_class.new("V1L180D").parses?("C")).to be false + expect(described_class.new("V1C1Y").parses?("C")).to be true + expect(described_class.new("V1").parses?(nil)).to be true + end + end + end + + describe "LE kind" do + it "parses V1LE24M as a valid LE notation" do + parser = described_class.new("V1LE24M") + expect(parser).to be_valid + expect(parser.kind).to eq("LE") + expect(parser.period_count).to eq("24") + expect(parser.period_key).to eq("M") + end + + it "does not confuse V1L24M (Lookback) with LE" do + parser = described_class.new("V1L24M") + expect(parser.kind).to eq("L") + end + end + end + end +end diff --git a/spec/sof/cycle/time_span_spec.rb b/spec/sof/cycle/time_span_spec.rb new file mode 100644 index 0000000..6a2d672 --- /dev/null +++ b/spec/sof/cycle/time_span_spec.rb @@ -0,0 +1,109 @@ +# frozen_string_literal: true + +require "spec_helper" + +module SOF + class Cycle + RSpec.describe TimeSpan, type: :value do + describe ".notation" do + it "accepts a hash and returns a string notation" do + aggregate_failures do + expect(described_class.notation(period: :day, period_count: 3)).to eq("3D") + expect(described_class.notation(period: :month, period_count: 2)).to eq("2M") + expect(described_class.notation(period: :quarter, period_count: 5)).to eq("5Q") + expect(described_class.notation(period: :year, period_count: 2)).to eq("2Y") + end + end + end + + describe ".for" do + it "returns a TimeSpan object" do + expect(described_class.for(1, "M")).to be_a(described_class) + end + + it "accepts string counts" do + expect(described_class.for("5", "M").period_count).to eq(5) + end + + it "accepts lowercase periods" do + expect(described_class.for(3, "m").period).to eq(:month) + end + end + + describe "#end_date_of_period" do + it "is nil if there is no period" do + expect(described_class.for("", "").end_date_of_period(Time.current)).to be_nil + end + + it "for a year period returns a date at the end of the next calendar year" do + span = described_class.for("1", "Y") + expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-12-31") + end + + it "for a month period returns a date 30 days later" do + span = described_class.for("1", "m") + expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-31") + end + + it "for a week period returns a date a at the end of the week" do + span = described_class.for("1", "w") + expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-16") + end + + it "for a day period returns a the same date" do + span = described_class.for("1", "D") + expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-15") + end + + it "for a quarter period returns a date at the end of the next quarter" do + span = described_class.for("1", "Q") + expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-03-31") + end + end + + describe "#begin_date_of_period" do + it "for a quarter period returns the beginning of the quarter" do + span = described_class.for("1", "Q") + expect(span.begin_date_of_period("2022-02-15".to_date)).to eq Date.parse("2022-01-01") + end + + it "for a week period returns the beginning of the week" do + span = described_class.for("1", "W") + # 2022-01-15 is a Saturday, beginning of week is Monday 2022-01-10 + expect(span.begin_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-10") + end + + it "for a day period returns the same date" do + span = described_class.for("1", "D") + expect(span.begin_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-15") + end + end + + describe "#duration" do + it "for quarters returns the duration in months" do + span = described_class.for("2", "Q") + expect(span.duration).to eq 6.months + end + end + + describe "#final_date" do + it "returns nil if there is no period" do + span = described_class.for("", "") + expect(span.final_date(Date.parse("2022-01-15"))).to be_nil + end + + it "returns the end date for a valid period" do + span = described_class.for("3", "M") + expect(span.final_date(Date.parse("2022-01-15"))).to eq Date.parse("2022-04-15") + end + end + + describe "#to_h" do + it "returns a hash with period and period_count" do + span = described_class.for("3", "M") + expect(span.to_h).to eq({period: :month, period_count: 3}) + end + end + end + end +end diff --git a/spec/sof/parser_spec.rb b/spec/sof/parser_spec.rb deleted file mode 100644 index 7bacca9..0000000 --- a/spec/sof/parser_spec.rb +++ /dev/null @@ -1,244 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -module SOF - RSpec.describe Parser, type: :value do - describe ".load(hash)" do - it "returns a Parser instance" do - hash = { - volume: 1, - kind: "L", - period_count: "180", - period_key: "D" - } - expect(described_class.load(hash)).to eq described_class.new("V1L180D") - end - end - - describe ".for(notation_or_parser)" do - context "when given a string" do - it "returns a Parser instance" do - expect(described_class.for("V1L180D")).to be_a(described_class) - end - end - - context "when given a Parser object" do - it "returns the object" do - parser = described_class.new("V1L180D") - expect(described_class.for(parser)).to eq parser - end - end - end - - describe "#initialize" do - it "is case-insensitive" do - aggregate_failures do - expect(described_class.new("V1l180D").inspect).to eq "V1L180D" - expect(described_class.new("V1L180d").to_s).to eq "V1L180D" - end - end - end - - describe "#inspect & #to_s" do - it "returns the string representation" do - aggregate_failures do - expect(described_class.new("V1L180D").inspect).to eq "V1L180D" - expect(described_class.new("V1L180D").to_s).to eq "V1L180D" - end - end - end - - describe "#from_data" do - context "when notation has no from date" do - it "returns an empty hash" do - parser = described_class.new("V1L180D") - expect(parser.from_data).to eq({}) - end - end - - context "when notation has a from date" do - it "returns a hash with the from date" do - parser = described_class.new("V1W1MF2024-01-01") - expect(parser.from_data).to eq({from: "F2024-01-01"}) - end - end - end - - describe "#valid?" do - it "returns true if the notation is recognized" do - aggregate_failures do - expect(described_class.new("V1L180D")).to be_valid - expect(described_class.new("V1E18M")).to be_valid - expect(described_class.new("XXX")).not_to be_valid - end - end - end - - describe "#to_h" do - it "returns a hash representation of the notation" do - aggregate_failures do - expect(described_class.new("V1L180D").to_h).to eq({ - volume: 1, - kind: "L", - period_count: "180", - period_key: "D", - from_date: nil - }) - expect(described_class.new("V1W180D").to_h).to eq({ - volume: 1, - kind: "W", - period_count: "180", - period_key: "D", - from_date: nil - }) - expect(described_class.new("V1W180DF2024-05-06").to_h).to eq({ - volume: 1, - kind: "W", - period_count: "180", - period_key: "D", - from_date: "2024-05-06" - }) - end - end - end - - describe "#activated_notation(date)" do - it "returns the activated notation" do - date = "2024-05-06" - aggregate_failures do - expect(described_class.new("V1L180D").activated_notation(date)).to eq( - "V1L180D" - ) - expect(described_class.new("V1W180D").activated_notation(date)).to eq( - "V1W180DF2024-05-06" - ) - expect(described_class.new("V1W180DF2024-09-09").activated_notation(date)).to eq( - "V1W180DF2024-05-06" - ) - expect(described_class.new("V1E18M").activated_notation(date)).to eq( - "V1E18MF2024-05-06" - ) - expect(described_class.new("V1E18MF2024-09-09").activated_notation(date)).to eq( - "V1E18MF2024-05-06" - ) - end - end - end - - describe "#dormant|_capable? & #active?" do - it "returns true if the notation has a dormant variant" do - aggregate_failures do - expect(described_class.new("V1L180D")).not_to be_dormant_capable - expect(described_class.new("V1L180D")).not_to be_dormant - expect(described_class.new("V1L180D")).to be_active - - expect(described_class.new("V1E18M")).to be_dormant_capable - expect(described_class.new("V1E18M")).to be_dormant - expect(described_class.new("V1E18M")).not_to be_active - - expect(described_class.new("V1E18MF2024-09-09")).to be_dormant_capable - expect(described_class.new("V1E18MF2024-09-09")).not_to be_dormant - expect(described_class.new("V1E18MF2024-09-09")).to be_active - - expect(described_class.new("V1W180D")).to be_dormant_capable - expect(described_class.new("V1W180D")).to be_dormant - expect(described_class.new("V1W180D")).not_to be_active - - expect(described_class.new("V1W180DF2024-09-09")).to be_dormant_capable - expect(described_class.new("V1W180DF2024-09-09")).not_to be_dormant - expect(described_class.new("V1W180DF2024-09-09")).to be_active - end - end - end - - describe "#from|_date" do - it "returns match[:from]|match[:from_date]" do - aggregate_failures do - expect(described_class.new("V1L180D").from).to be_nil - expect(described_class.new("V1L180D").from_date).to be_nil - - expect(described_class.new("V1W10D").from).to be_nil - expect(described_class.new("V1W10D").from_date).to be_nil - expect(described_class.new("V1W10DF2024-04-09").from).to eq "F2024-04-09" - expect(described_class.new("V1W10DF2024-04-09").from_date).to eq "2024-04-09" - - expect(described_class.new("V1E18M").from).to be_nil - expect(described_class.new("V1E18M").from_date).to be_nil - expect(described_class.new("V1E18MF2024-04-09").from).to eq "F2024-04-09" - expect(described_class.new("V1E18MF2024-04-09").from_date).to eq "2024-04-09" - end - end - end - - describe "#period_count" do - it "returns match[:period_count]" do - aggregate_failures do - expect(described_class.new("V1L180D").period_count).to eq "180" - expect(described_class.new("V3C1Y").period_count).to eq "1" - expect(described_class.new("V4").period_count).to be_nil - end - end - end - - describe "#period_key" do - it "returns match[:period_key]" do - aggregate_failures do - expect(described_class.new("V1L180D").period_key).to eq "D" - expect(described_class.new("V3C1Y").period_key).to eq "Y" - expect(described_class.new("V4").period_key).to be_nil - end - end - end - - describe "#volume" do - it "returns match[:volume]" do - aggregate_failures do - expect(described_class.new("L180D").volume).to eq 1 - expect(described_class.new("V1L180D").volume).to eq 1 - expect(described_class.new("V3C1Y").volume).to eq 3 - expect(described_class.new("V4").volume).to eq 4 - end - end - end - - describe "#kind" do - it "returns match[:kind]" do - aggregate_failures do - expect(described_class.new("V1E18M").kind).to eq "E" - expect(described_class.new("V1L180D").kind).to eq "L" - expect(described_class.new("V1C1Y").kind).to eq "C" - expect(described_class.new("V1").kind).to be_nil - end - end - end - - describe "#parses?" do - it "returns true if the object parses the notation_id" do - aggregate_failures do - expect(described_class.new("V1E18M").parses?("E")).to be true - expect(described_class.new("V1E18M").parses?("L")).to be false - expect(described_class.new("V1L180D").parses?("L")).to be true - expect(described_class.new("V1L180D").parses?("C")).to be false - expect(described_class.new("V1C1Y").parses?("C")).to be true - expect(described_class.new("V1").parses?(nil)).to be true - end - end - end - - describe "LE kind" do - it "parses V1LE24M as a valid LE notation" do - parser = described_class.new("V1LE24M") - expect(parser).to be_valid - expect(parser.kind).to eq("LE") - expect(parser.period_count).to eq("24") - expect(parser.period_key).to eq("M") - end - - it "does not confuse V1L24M (Lookback) with LE" do - parser = described_class.new("V1L24M") - expect(parser.kind).to eq("L") - end - end - end -end diff --git a/spec/sof/time_span_spec.rb b/spec/sof/time_span_spec.rb deleted file mode 100644 index 6008f96..0000000 --- a/spec/sof/time_span_spec.rb +++ /dev/null @@ -1,107 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -module SOF - RSpec.describe SOF::TimeSpan, type: :value do - describe ".notation" do - it "accepts a hash and returns a string notation" do - aggregate_failures do - expect(described_class.notation(period: :day, period_count: 3)).to eq("3D") - expect(described_class.notation(period: :month, period_count: 2)).to eq("2M") - expect(described_class.notation(period: :quarter, period_count: 5)).to eq("5Q") - expect(described_class.notation(period: :year, period_count: 2)).to eq("2Y") - end - end - end - - describe ".for" do - it "returns a TimeSpan object" do - expect(described_class.for(1, "M")).to be_a(described_class) - end - - it "accepts string counts" do - expect(described_class.for("5", "M").period_count).to eq(5) - end - - it "accepts lowercase periods" do - expect(described_class.for(3, "m").period).to eq(:month) - end - end - - describe "#end_date_of_period" do - it "is nil if there is no period" do - expect(described_class.for("", "").end_date_of_period(Time.current)).to be_nil - end - - it "for a year period returns a date at the end of the next calendar year" do - span = described_class.for("1", "Y") - expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-12-31") - end - - it "for a month period returns a date 30 days later" do - span = described_class.for("1", "m") - expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-31") - end - - it "for a week period returns a date a at the end of the week" do - span = described_class.for("1", "w") - expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-16") - end - - it "for a day period returns a the same date" do - span = described_class.for("1", "D") - expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-15") - end - - it "for a quarter period returns a date at the end of the next quarter" do - span = described_class.for("1", "Q") - expect(span.end_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-03-31") - end - end - - describe "#begin_date_of_period" do - it "for a quarter period returns the beginning of the quarter" do - span = described_class.for("1", "Q") - expect(span.begin_date_of_period("2022-02-15".to_date)).to eq Date.parse("2022-01-01") - end - - it "for a week period returns the beginning of the week" do - span = described_class.for("1", "W") - # 2022-01-15 is a Saturday, beginning of week is Monday 2022-01-10 - expect(span.begin_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-10") - end - - it "for a day period returns the same date" do - span = described_class.for("1", "D") - expect(span.begin_date_of_period("2022-01-15".to_date)).to eq Date.parse("2022-01-15") - end - end - - describe "#duration" do - it "for quarters returns the duration in months" do - span = described_class.for("2", "Q") - expect(span.duration).to eq 6.months - end - end - - describe "#final_date" do - it "returns nil if there is no period" do - span = described_class.for("", "") - expect(span.final_date(Date.parse("2022-01-15"))).to be_nil - end - - it "returns the end date for a valid period" do - span = described_class.for("3", "M") - expect(span.final_date(Date.parse("2022-01-15"))).to eq Date.parse("2022-04-15") - end - end - - describe "#to_h" do - it "returns a hash with period and period_count" do - span = described_class.for("3", "M") - expect(span.to_h).to eq({period: :month, period_count: 3}) - end - end - end -end From 63ff1f3ca1a944864464aabb7f0db9f7fd14809b Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Mon, 27 Jul 2026 11:43:16 -0400 Subject: [PATCH 2/3] Freeze the dormant-capable kinds The returned array is derived state with no behavior of its own, so nothing should be mutating it. It stays a method rather than becoming a constant, and the reason is now recorded next to it: handlers register through Cycle.inherited as each file in sof/cycles loads, which happens after this file. A constant would capture the empty set, and every dormant-capable kind would go unrecognized. --- lib/sof/cycle/parser.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/sof/cycle/parser.rb b/lib/sof/cycle/parser.rb index 9dd6538..aaa1b80 100644 --- a/lib/sof/cycle/parser.rb +++ b/lib/sof/cycle/parser.rb @@ -22,8 +22,12 @@ class Parser (?F(?\d{4}-\d{2}-\d{2}))?$ # optional from /ix + # Deliberately a method, not a constant: handlers register themselves + # through Cycle.inherited as each file in sof/cycles loads, which happens + # after this file. A constant would capture the empty set and leave every + # dormant-capable kind unrecognized. def self.dormant_capable_kinds - Cycle.cycle_handlers.select(&:dormant_capable?).map(&:notation_id).compact + Cycle.cycle_handlers.select(&:dormant_capable?).map(&:notation_id).compact.freeze end def self.for(notation_or_parser) From 70ade10ee06cd3116110347418da69d421271339 Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Mon, 27 Jul 2026 13:45:05 -0400 Subject: [PATCH 3/3] Define instance variables eagerly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parser and DatePeriod each grew instance variables on first use, so their instances took several object shapes instead of one. DatePeriod was the worse of the two: end_date and begin_date each added an ivar when called, so the shape depended on which ran first. Both now declare every ivar in the constructor, which is what Ruby's own performance warning recommends. Neither class was close to the eight variation threshold that triggers it, so this is not a fix for an observed warning — but the memoization guards were doing shape work for no reason, and dropping them makes both readers shorter. Verified with -W:performance: each class now takes exactly one shape. --- lib/sof/cycle/parser.rb | 1 + lib/sof/cycle/time_span.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sof/cycle/parser.rb b/lib/sof/cycle/parser.rb index aaa1b80..957e4d5 100644 --- a/lib/sof/cycle/parser.rb +++ b/lib/sof/cycle/parser.rb @@ -49,6 +49,7 @@ def self.load(hash) def initialize(notation) @notation = notation&.upcase @match = @notation&.match(PARTS_REGEX) + @time_span = nil end attr_reader :match, :notation diff --git a/lib/sof/cycle/time_span.rb b/lib/sof/cycle/time_span.rb index 86b93dd..bcb2798 100644 --- a/lib/sof/cycle/time_span.rb +++ b/lib/sof/cycle/time_span.rb @@ -91,16 +91,16 @@ def inherited(klass) def initialize(count) @count = count + @end_date = {} + @begin_date = {} end attr_reader :count def end_date(date) - @end_date ||= {} @end_date[date] ||= date + duration end def begin_date(date) - @begin_date ||= {} @begin_date[date] ||= date - duration end