class Recurify::Rule
Represents a Recurrence Rule
. Note that Rule
objects are immutable once they are instantiated (i.e. they are value objects).
Constants
- BASE_FREQUENCIES
- SUGAR_FREQUENCIES
- SUPPORTED_FREQUENCIES
Attributes
@!attribute [r] interval
@return [Fixnum]
@!attribute [r] starts_on
@return [Date]
@!attribute [r] frequency
@return [String]
@!attribute [r] count
@return [Fixnum, nil]
Public Class Methods
Returns a new instance of Rule
.
@param attributes [Hash<Symbol,Object>] attributes for the new Rule
@option attributes [#to_s] :frequency @option attributes [#to_i] :interval @option attributes [#to_i, nil] :count @option attributes [#to_date] :starts_on @option attributes [#to_date, nil] :ends_on
@raise [InvalidRuleFrequency] if the provided #frequency
is not
supported
@raise [InvalidRuleInterval] if the provided #interval
is not supported @raise [InvalidRuleCount] if the provided #count
is not supported
# File lib/recurify/rule.rb, line 52 def initialize(attributes = {}) self.attributes = self.class.default_attributes.merge(attributes) validate! end
Private Class Methods
# File lib/recurify/rule.rb, line 95 def self.default_attributes { frequency: SUPPORTED_FREQUENCIES.first, interval: MIN_INTERVAL, starts_on: Date.today } end
Public Instance Methods
Tests for equality with other
.
Two Rule
objects are considered to be equal, if and only if they both evaluate to the same same set of Date
objects.
@param other [Rule] @return [Boolean]
# File lib/recurify/rule.rb, line 75 def ==(other) normalized_attributes == other.normalized_attributes end
Convert self
to a Hash
, representing the same Rule
. Note, that #attributes
is essentially the inverse of #initialize
.
@return [Hash<Symbol,Object>]
# File lib/recurify/rule.rb, line 83 def attributes @_attributes ||= { frequency: frequency, interval: interval, count: count, starts_on: starts_on, ends_on: ends_on } end
Creates a new instance of Rule
with substituted attributes. Attributes that haven’t been specified are copied from self
.
@see initialize
@param substitutions [Hash<Symbol,Object>] for the the new Rule
@return [Rule]
# File lib/recurify/rule.rb, line 64 def substitute(substitutions = {}) self.class.new(attributes.merge(substitutions)) end
Private Instance Methods
# File lib/recurify/rule.rb, line 103 def attributes=(attrs) @frequency = attrs[:frequency].to_s @interval = attrs[:interval].to_i @count = attrs[:count].nil? ? nil : attrs[:count].to_i @starts_on = attrs[:starts_on].to_date @ends_on = attrs[:ends_on].nil? ? nil : attrs[:ends_on].to_date end