class Timely::TemporalPatterns::Frequency

Constants

UNITS

Attributes

duration[RW]

Public Class Methods

decompose(duration) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 78
def decompose(duration)
  whole = duration
  parts = []
  plural_units.reverse_each do |unit|
    if whole >= (one_unit = 1.send(unit))
      current_unit_value = ((whole / one_unit).floor)
      if current_unit_value > 0
        parts << [unit, current_unit_value]
        whole -= current_unit_value.send(unit)
      end
    end
  end
  parts
end
decompose_to_hash(duration) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 93
def decompose_to_hash(duration)
  decompose(duration).inject({}) do |hash, unit|
    hash[unit.first] = unit.last
    hash
  end
end
new(duration) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 8
def initialize(duration)
  self.duration = duration
end
parse(duration) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 70
def parse(duration)
  parsed = 0.seconds
  decompose(duration).each do |part|
    parsed += part.last.send(part.first)
  end
  parsed
end
plural_units() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 54
def plural_units
  UNITS.map { |unit| unit.to_s.pluralize.to_sym }
end
singular_units() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 50
def singular_units
  UNITS.dup
end
unit_durations() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 58
def unit_durations
  UNITS.map { |unit| 1.call(unit) }
end
valid_unit?(unit) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 66
def valid_unit?(unit)
  valid_units.include?(unit.to_s.to_sym)
end
valid_units() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 62
def valid_units
  singular_units + plural_units
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 18
def <=>(other)
  self.duration <=> other.duration
end
duration=(duration) click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 12
def duration=(duration)
  raise ArgumentError, "Frequency (#{duration}) must be a duration" unless duration.is_a?(ActiveSupport::Duration)
  raise ArgumentError, "Frequency (#{duration}) must be positive" unless duration > 0
  @duration = self.class.parse(duration)
end
max_unit() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 40
def max_unit
  parts = self.class.decompose(duration)
  {parts.first.first => parts.first.last}
end
min_unit() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 35
def min_unit
  parts = self.class.decompose(duration)
  {parts.last.first => parts.last.last}
end
to_s() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 22
def to_s
   "every " + duration.parts.
                reduce(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }.
                sort_by {|unit,  _ | [:years, :months, :days, :minutes, :seconds].index(unit)}.
                map     {|unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}" if val.nonzero?}.compact.
                to_sentence(:locale => :en)
end
unit() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 30
def unit
  parts = self.class.decompose(duration)
  parts.size == 1 && parts.first.last == 1? parts.first.first : nil
end
units() click to toggle source
# File lib/timely/temporal_patterns/frequency.rb, line 45
def units
  self.class.decompose_to_hash(duration)
end