Class: Recurify::Rule

Inherits:
Object
  • Object
show all
Includes:
RuleAnalysis, RuleTranslation, RuleValidation
Defined in:
lib/recurify/rule.rb

Overview

Represents a Recurrence Rule. Note that Rule objects are immutable once they are instantiated (i.e. they are value objects).

Constant Summary collapse

BASE_FREQUENCIES =
%w(daily monthly).freeze
SUGAR_FREQUENCIES =
%w(weekly quarterly yearly).freeze
SUPPORTED_FREQUENCIES =
(BASE_FREQUENCIES + SUGAR_FREQUENCIES).freeze

Constants included from RuleTranslation

Recurify::RuleTranslation::DENORMALIZATION_MATRIX, Recurify::RuleTranslation::NORMALIZATION_MATRIX

Constants included from RuleValidation

Recurify::RuleValidation::MIN_COUNT, Recurify::RuleValidation::MIN_INTERVAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RuleTranslation

#denormalize, #denormalized_count, #denormalized_ends_on, #denormalized_frequency, #denormalized_interval, #normalize, #normalized_count, #normalized_ends_on, #normalized_frequency, #normalized_interval

Methods included from RuleAnalysis

#denormalized?, #ends_after?, #ends_before?, #ends_between?, #finite?, #infinite?, #normalized?, #overlaps?, #starts_after?, #starts_before?, #starts_between?

Constructor Details

#initialize(attributes = {}) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • (defaults to: {})

    attributes for the new Rule

Options Hash (attributes):

  • :frequency (#to_s)
  • :interval (#to_i)
  • :count (#to_i, nil)
  • :starts_on (#to_date)
  • :ends_on (#to_date, nil)

Raises:

  • if the provided #frequency is not supported

  • if the provided #interval is not supported

  • if the provided #count is not supported



52
53
54
55
# File 'lib/recurify/rule.rb', line 52

def initialize(attributes = {})
  self.attributes = self.class.default_attributes.merge(attributes)
  validate!
end

Instance Attribute Details

#countFixnum? (readonly)

Returns:



27
28
29
# File 'lib/recurify/rule.rb', line 27

def count
  @count
end

#ends_onDate? (readonly)

Returns:



35
36
37
# File 'lib/recurify/rule.rb', line 35

def ends_on
  @ends_on
end

#frequencyString (readonly)

Returns:



23
24
25
# File 'lib/recurify/rule.rb', line 23

def frequency
  @frequency
end

#intervalFixnum (readonly)

Returns:



23
24
25
# File 'lib/recurify/rule.rb', line 23

def interval
  @interval
end

#starts_onDate (readonly)

Returns:



31
32
33
# File 'lib/recurify/rule.rb', line 31

def starts_on
  @starts_on
end

Instance Method Details

#==(other) ⇒ Boolean

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.

Parameters:

Returns:



75
76
77
# File 'lib/recurify/rule.rb', line 75

def ==(other)
  normalize.attributes == other.normalize.attributes
end

#attributesHash<Symbol,Object>

Convert self to a Hash, representing the same Rule. Note, that #attributes is essentially the inverse of #initialize.

Returns:



83
84
85
86
87
88
89
90
91
# 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 = {}) ⇒ Rule

Creates a new instance of Rule with substituted attributes. Attributes that haven’t been specified are copied from self.

Parameters:

  • (defaults to: {})

    for the the new Rule

Returns:

See Also:



64
65
66
# File 'lib/recurify/rule.rb', line 64

def substitute(substitutions = {})
  self.class.new(attributes.merge(substitutions))
end