Class: TimeCalc::Op
- Inherits:
-
Object
- Object
- TimeCalc::Op
- Defined in:
- lib/time_calc/op.rb
Overview
Abstraction over chain of time math operations that can be applied to a time or date.
Instance Attribute Summary collapse
- #chain ⇒ Object readonly
Instance Method Summary collapse
-
#+(span, unit) ⇒ Op
Adds ‘+(span, unit)` to method chain.
-
#-(span, unit) ⇒ Op
Adds ‘-(span, unit)` to method chain.
-
#call(date_or_time) ⇒ Date, ...
Performs the whole chain of operation on parameter, returning the result.
-
#ceil(unit) ⇒ Op
Adds ‘ceil(span, unit)` to method chain.
-
#floor(unit) ⇒ Op
Adds ‘floor(span, unit)` to method chain.
-
#initialize(chain = []) ⇒ Op
constructor
A new instance of Op.
- #inspect ⇒ Object
-
#iterate(span, unit, &block) ⇒ Op
Adds ‘iterate(span, unit, &block)` to method chain.
-
#round(unit) ⇒ Op
Adds ‘round(span, unit)` to method chain.
-
#to_proc ⇒ Proc
Allows to pass operation with ‘&operation`.
Constructor Details
#initialize(chain = []) ⇒ Op
Note:
Prefer ‘TimeCalc.<operation>` (for example TimeCalc#+) to create operations.
Returns a new instance of Op.
19 20 21 |
# File 'lib/time_calc/op.rb', line 19 def initialize(chain = []) @chain = chain end |
Instance Attribute Details
#chain ⇒ Object (readonly)
15 16 17 |
# File 'lib/time_calc/op.rb', line 15 def chain @chain end |
Instance Method Details
#call(date_or_time) ⇒ Date, ...
Performs the whole chain of operation on parameter, returning the result.
61 62 63 64 65 |
# 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 ⇒ Object
24 25 26 |
# File 'lib/time_calc/op.rb', line 24 def inspect '<%s %s>' % [self.class, @chain.map { |name, args, _| "#{name}(#{args.join(' ')})" }.join('.')] end |
#iterate(span, unit, &block) ⇒ Op
Adds ‘iterate(span, unit, &block)` to method chain
|
# File 'lib/time_calc/op.rb', line 32
|
#to_proc ⇒ Proc
Allows to pass operation with ‘&operation`.
70 71 72 |
# File 'lib/time_calc/op.rb', line 70 def to_proc method(:call).to_proc end |