From 24039567578068af3a654c9975e34582c26a9c0d Mon Sep 17 00:00:00 2001 From: Guilherme Gazzinelli Date: Thu, 24 Aug 2023 14:50:13 -0300 Subject: [PATCH] Add Only Rule, to ierate just on an array of dates. --- lib/montrose.rb | 1 + lib/montrose/frequency/manual.rb | 0 lib/montrose/options.rb | 6 ++++++ lib/montrose/rule.rb | 1 + lib/montrose/rule/only.rb | 34 ++++++++++++++++++++++++++++++++ lib/montrose/stack.rb | 3 ++- spec/montrose/rule/only_spec.rb | 19 ++++++++++++++++++ 7 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/montrose/frequency/manual.rb create mode 100644 lib/montrose/rule/only.rb create mode 100644 spec/montrose/rule/only_spec.rb diff --git a/lib/montrose.rb b/lib/montrose.rb index e426193..0413592 100644 --- a/lib/montrose.rb +++ b/lib/montrose.rb @@ -32,6 +32,7 @@ module Montrose autoload :Utils, "montrose/utils" autoload :Week, "montrose/week" autoload :YearDay, "montrose/year_day" + autoload :Only, "montrose/rule/only" extend Chainable diff --git a/lib/montrose/frequency/manual.rb b/lib/montrose/frequency/manual.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/montrose/options.rb b/lib/montrose/options.rb index 34a9736..bce1fe2 100644 --- a/lib/montrose/options.rb +++ b/lib/montrose/options.rb @@ -102,6 +102,7 @@ def default_options def_option :on def_option :except def_option :exclude_end + def_option :only def initialize(opts = {}) defaults = { @@ -246,6 +247,10 @@ def except=(date) @except = map_arg(date) { |d| as_date(d) } end + def only=(dates) + @only = map_arg(dates) { |d| as_date(d) } + end + def inspect "#<#{self.class} #{to_h.inspect}>" end @@ -262,6 +267,7 @@ def start_time end end + private def default_starts diff --git a/lib/montrose/rule.rb b/lib/montrose/rule.rb index f386a97..a5167c2 100644 --- a/lib/montrose/rule.rb +++ b/lib/montrose/rule.rb @@ -20,6 +20,7 @@ module Rule autoload :Total, "montrose/rule/total" autoload :Until, "montrose/rule/until" autoload :WeekOfYear, "montrose/rule/week_of_year" + autoload :Only, "montrose/rule/only" def self.included(base) base.extend ClassMethods diff --git a/lib/montrose/rule/only.rb b/lib/montrose/rule/only.rb new file mode 100644 index 0000000..c1af357 --- /dev/null +++ b/lib/montrose/rule/only.rb @@ -0,0 +1,34 @@ +module Montrose + module Rule + class Only + include Montrose::Rule + + def self.apply_options(opts) + opts[:only] + end + + # Initializes rule + # + # @param [Date] dates - array of date objects + # + def initialize(dates) + @dates = dates + @max = dates.length + # @count = 0 + end + + def include?(time) + !!@dates.include?(time.to_date) + end + + # def advance!(time) + # continue?(time) + # end + + def continue?(time) + # @count <= @max && @dates.max >= time.to_date + @dates.max >= time.to_date + end + end + end +end diff --git a/lib/montrose/stack.rb b/lib/montrose/stack.rb index 33190b5..84f018d 100644 --- a/lib/montrose/stack.rb +++ b/lib/montrose/stack.rb @@ -24,7 +24,8 @@ def self.build(opts = {}) Rule::DayOfMonth, Rule::DayOfYear, Rule::WeekOfYear, - Rule::MonthOfYear + Rule::MonthOfYear, + Rule::Only ].map { |r| r.from_options(opts) }.compact end diff --git a/spec/montrose/rule/only_spec.rb b/spec/montrose/rule/only_spec.rb new file mode 100644 index 0000000..c381037 --- /dev/null +++ b/spec/montrose/rule/only_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Montrose::Rule::Only do + let(:dates) { [Date.today, Date.today + 5.days] } + let(:rule) { Montrose::Rule::Only.new(dates) } + + describe "#include?" do + it { assert rule.include?(time_now) } + it { refute rule.include?(time_now + 1.days) } + it { assert rule.include?(time_now + 5.days) } + it { refute rule.include?(time_now + 10.days) } + end + + describe "#continue?" do + it { refute rule.continue? dates[1]+1.day } + end +end