Module: Recurify::RuleAnalysis

Included in:
Rule
Defined in:
lib/recurify/rule_analysis.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#denormalized?Boolean

Returns true if self is denormalized. By definition, a Rule is denormalized if and only if it is not normalized.

Returns:

  • (Boolean)

See Also:



19
20
21
# File 'lib/recurify/rule_analysis.rb', line 19

def denormalized?
  !normalized?
end

#ends_after?(lower) ⇒ Boolean

Returns true if self ends after or on lower. If lower == nil, it is interpreted as “negative infinity”. In that case, #ends_after? always returns true.

Parameters:

  • lower (#to_date, nil)

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/recurify/rule_analysis.rb', line 105

def ends_after?(lower)
  false ||
    # Case (1): 'infinite' >= [anything] ==> true
    normalized_ends_on.nil? ||
    # Case (2): 'finite' >= 'infinite' ==> true
    lower.nil? ||
    # Case (3): 'finite' >= 'finite' ==> depends on actual comparison
    normalized_ends_on >= lower.to_date
end

#ends_before?(upper) ⇒ Boolean

Returns true if self ends before or on upper. If upper == nil, it is interpreted as “positive infinity”. In that case, #ends_before? always returns true.

Parameters:

  • upper (#to_date, nil)

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/recurify/rule_analysis.rb', line 90

def ends_before?(upper)
  false ||
    # Case (1): [anything] <= 'infinite' ==> true
    upper.nil? ||
    # Case (2): 'finite' <= 'finite' ==> depends on actual comparison
    !normalized_ends_on.nil? && normalized_ends_on <= upper.to_date
end

#ends_between?(lower: nil, upper: nil) ⇒ Boolean

Returns true if self ends after lower and ends before upper (including boundaries).

Parameters:

  • lower (#to_date, nil) (defaults to: nil)
  • upper (#to_date, nil) (defaults to: nil)

Returns:

  • (Boolean)

See Also:



125
126
127
# File 'lib/recurify/rule_analysis.rb', line 125

def ends_between?(lower: nil, upper: nil)
  ends_after?(lower) && ends_before?(upper)
end

#finite?Boolean

Returns true if self is finite. By definition, a Rule is finite if and only if it is not infinite.

Returns:

  • (Boolean)

See Also:



29
30
31
32
33
# File 'lib/recurify/rule_analysis.rb', line 29

def finite?
  # @todo Checking of the #count is not needed anymore, once #normalize
  #   supports minifying of #ends_on.
  !count.nil? || !normalized_ends_on.nil?
end

#infinite?Boolean

Returns true if self is infinite. By definition, a Rule is infinite if and only if it is not finite.

Returns:

  • (Boolean)

See Also:



41
42
43
# File 'lib/recurify/rule_analysis.rb', line 41

def infinite?
  !finite?
end

#normalized?Boolean

Returns true if self is normalized. By definition, a Rule is normalized if and only if it is not denormalized.

Returns:

  • (Boolean)

See Also:



9
10
11
# File 'lib/recurify/rule_analysis.rb', line 9

def normalized?
  Rule::BASE_FREQUENCIES.include?(frequency)
end

#overlaps?(lower: nil, upper: nil) ⇒ Boolean

Returns true if self starts and/or ends between lower and upper (including boundaries).

Parameters:

  • lower (#to_date, nil) (defaults to: nil)
  • upper (#to_date, nil) (defaults to: nil)

Returns:

  • (Boolean)

See Also:



139
140
141
142
143
144
145
# File 'lib/recurify/rule_analysis.rb', line 139

def overlaps?(lower: nil, upper: nil)
  false ||
    # Case (1): +lower+ <= starts_on <= +upper+ ==> true
    starts_between?(lower: lower, upper: upper) ||
    # Case (2): +lower+ <= normalized_ends_on <= +upper+ ==> true
    ends_between?(lower: lower, upper: upper)
end

#starts_after?(lower) ⇒ Boolean

Returns true if self starts after or on lower. If lower == nil, it is interpreted as “negative infinity”. In that case, #starts_after? always returns true, because all Rule objects must have a finite #starts_on attribute.

Parameters:

  • lower (#to_date, nil)

Returns:

  • (Boolean)


65
66
67
# File 'lib/recurify/rule_analysis.rb', line 65

def starts_after?(lower)
  lower.nil? || starts_on >= lower.to_date
end

#starts_before?(upper) ⇒ Boolean

Returns true if self starts before or on upper. If upper == nil, it is interpreted as “positive infinity”. In that case, #starts_before? always returns true, because all Rule objects must have a finite #starts_on attribute.

Parameters:

  • upper (#to_date, nil)

Returns:

  • (Boolean)


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

def starts_before?(upper)
  upper.nil? || starts_on <= upper.to_date
end

#starts_between?(lower: nil, upper: nil) ⇒ Boolean

Returns true if self starts after lower and starts before upper (including boundaries).

Parameters:

  • lower (#to_date, nil) (defaults to: nil)
  • upper (#to_date, nil) (defaults to: nil)

Returns:

  • (Boolean)

See Also:



79
80
81
# File 'lib/recurify/rule_analysis.rb', line 79

def starts_between?(lower: nil, upper: nil)
  starts_after?(lower) && starts_before?(upper)
end