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

count[R]

@!attribute [r] interval

@return [Fixnum]
ends_on[R]

@!attribute [r] starts_on

@return [Date]
frequency[R]
interval[R]

@!attribute [r] frequency

@return [String]
starts_on[R]

@!attribute [r] count

@return [Fixnum, nil]

Public Class Methods

new(attributes = {}) click to toggle source

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

default_attributes() click to toggle source
# 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

==(other) click to toggle source

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
attributes() click to toggle source

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
substitute(substitutions = {}) click to toggle source

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

attributes=(attrs) click to toggle source
# 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