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
-
#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.
57 58 59 60 61 |
# File 'lib/time_calc/op.rb', line 57 def call(date_or_time) @chain.reduce(Value.new(date_or_time)) { |val, (name, *args)| val.public_send(name, *args) }.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 |
#to_proc ⇒ Proc
Allows to pass operation with ‘&operation`.
66 67 68 |
# File 'lib/time_calc/op.rb', line 66 def to_proc method(:call).to_proc end |