class TimeCalc::Op

Abstraction over chain of time math operations that can be applied to a time or date.

@example

op = TimeCalc.+(1, :day).floor(:hour)
# => <TimeCalc::Op +(1 day).floor(hour)>
op.call(Time.now)
# => 2019-07-04 22:00:00 +0300
array_of_time_values.map(&op)
# => array of "next day, floor to hour" for each element

Attributes

chain[R]

@private

Public Class Methods

new(chain = []) click to toggle source

@note

Prefer `TimeCalc.<operation>` (for example {TimeCalc#+}) to create operations.
# File lib/time_calc/op.rb, line 19
def initialize(chain = [])
  @chain = chain
end

Public Instance Methods

call(date_or_time) click to toggle source

Performs the whole chain of operation on parameter, returning the result.

@param date_or_time [Date, Time, DateTime] @return [Date, Time, DateTime] Type of the result is always the same as type of the parameter

# File lib/time_calc/op.rb, line 61
def call(date_or_time)
  @chain.reduce(Value.new(date_or_time)) { |val, (name, args, block)|
    val.public_send(name, *args, &block)
  }.unwrap
end
inspect() click to toggle source

@private

# File lib/time_calc/op.rb, line 24
def inspect
  '<%s %s>' % [self.class, @chain.map { |name, args, _| "#{name}(#{args.join(' ')})" }.join('.')]
end
to_proc() click to toggle source

Allows to pass operation with `&operation`.

@return [Proc]

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