Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/montrose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file.
6 changes: 6 additions & 0 deletions lib/montrose/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def default_options
def_option :on
def_option :except
def_option :exclude_end
def_option :only

def initialize(opts = {})
defaults = {
Expand Down Expand Up @@ -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
Expand All @@ -262,6 +267,7 @@ def start_time
end
end


private

def default_starts
Expand Down
1 change: 1 addition & 0 deletions lib/montrose/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions lib/montrose/rule/only.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/montrose/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions spec/montrose/rule/only_spec.rb
Original file line number Diff line number Diff line change
@@ -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