class TimeMath::Op

`Op` is value object, incapsulating several operations performed on time unit. The names of operations are the same the single unit can perform, first parameter is always a unit.

Ops can be created by `TimeMath::Op.new` or with pretty shortcut `TimeMath()`.

Available usages:

“`ruby # 1. chain operations: # without Op: 10:25 at first day of next week: TimeMath.min.advance(TimeMath.hour.advance(TimeMath.week.ceil(tm), 10), 25) # FOOOOOO # …but with Op: TimeMath(tm).ceil(:week).advance(:hour, 10).advance(:min, 25).call

# 2. chain operations on multiple objects: TimeMath(tm1, tm2, tm3).ceil(:week).advance(:hour, 10).advance(:min, 25).call # or TimeMath().ceil(:week).advance(:hour, 10).advance(:min, 25).call

# 3. preparing operation to be used on any objects: op = TimeMath().ceil(:week).advance(:hour, 10).advance(:min, 25) op.call(tm) op.call(tm1, tm2, tm3) op.call(array_of_times) # or even block-ish behavior: [tm1, tm2, tm3].map(&op) “`

Note that Op also plays well with {Sequence} (see its docs for more).

Constants

OPERATIONS

@private

Attributes

arguments[R]
operations[R]

Public Class Methods

new(*arguments) click to toggle source

Creates Op. Could (and recommended be also by its alias – just `TimeMath(*arguments)`.

@param arguments one, or several, or an array of time-y values

(Time, Date, DateTime).
# File lib/time_math/op.rb, line 45
def initialize(*arguments)
  @arguments = arguments
  @operations = []
end

Public Instance Methods

==(other) click to toggle source
# File lib/time_math/op.rb, line 165
def ==(other)
  self.class == other.class && operations == other.operations &&
    arguments == other.arguments
end
call(*tms) click to toggle source

Performs op. If an Op was created with arguments, just performs all operations on them and returns the result. If it was created without arguments, performs all operations on arguments provided to `call`.

@param tms one, or several, or an array of time-y values; should not

be passed if Op was created with arguments.

@return [Time,Date,DateTime,Array] one, or an array of processed arguments

# File lib/time_math/op.rb, line 177
def call(*tms)
  unless @arguments.empty?
    tms.empty? or raise(ArgumentError, 'Op arguments is already set, use call()')
    tms = @arguments
  end
  res = [*tms].flatten.map(&method(:perform))
  tms.count == 1 && Util.timey?(tms.first) ? res.first : res
end
initialize_copy(other) click to toggle source

@private

# File lib/time_math/op.rb, line 51
def initialize_copy(other)
  @arguments = other.arguments.dup
  @operations = other.operations.dup
end
inspect() click to toggle source
# File lib/time_math/op.rb, line 154
def inspect
  "#<#{self.class}#{inspect_args}" + inspect_operations + '>'
end
inspect_operations() click to toggle source

@private

# File lib/time_math/op.rb, line 159
def inspect_operations
  operations.map { |op, unit, args|
    "#{op}(#{[unit, *args].map(&:inspect).join(', ')})"
  }.join('.')
end
to_proc() click to toggle source

Allows to use Op as a block:

“`ruby timestamps.map(&TimeMath().ceil(:week).advance(:day, 1)) “` @return [Proc]

# File lib/time_math/op.rb, line 192
def to_proc
  method(:call).to_proc
end

Private Instance Methods

inspect_args() click to toggle source
# File lib/time_math/op.rb, line 198
def inspect_args
  return ' ' if @arguments.empty?

  '(' + [*@arguments].map(&:inspect).join(', ') + ').'
end
perform(tm) click to toggle source
# File lib/time_math/op.rb, line 204
def perform(tm)
  operations.inject(tm) { |memo, (op, unit, args)|
    TimeMath::Units.get(unit).send(op, memo, *args)
  }
end