Class: TimeMath::Op
- Inherits:
-
Object
- Object
- TimeMath::Op
- Defined in:
- lib/time_math/op.rb
Overview
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:
# 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([array_of_times]).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).
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#advance(unit, amount = 1) ⇒ Op
Non-destructive version of #advance!.
-
#advance!(unit, amount = 1) ⇒ self
Adds Units::Base#advance to list of operations.
-
#call(*tms) ⇒ Time, ...
Performs op.
-
#ceil(unit, span = 1) ⇒ Op
Non-destructive version of #ceil!.
-
#ceil!(unit, span = 1) ⇒ self
Adds Units::Base#ceil to list of operations.
-
#decrease(unit, amount = 1) ⇒ Op
Non-destructive version of #decrease!.
-
#decrease!(unit, amount = 1) ⇒ self
Adds Units::Base#decrease to list of operations.
-
#floor(unit, span = 1) ⇒ Op
Non-destructive version of #floor!.
-
#floor!(unit, span = 1) ⇒ self
Adds Units::Base#floor to list of operations.
-
#initialize(*arguments) ⇒ Op
constructor
Creates Op.
- #inspect ⇒ Object
-
#next(unit, span = 1) ⇒ Op
Non-destructive version of #next!.
-
#next!(unit, span = 1) ⇒ self
Adds Units::Base#next to list of operations.
-
#prev(unit, span = 1) ⇒ Op
Non-destructive version of #prev!.
-
#prev!(unit, span = 1) ⇒ self
Adds Units::Base#prev to list of operations.
-
#round(unit, span = 1) ⇒ Op
Non-destructive version of #round!.
-
#round!(unit, span = 1) ⇒ self
Adds Units::Base#round to list of operations.
-
#to_proc ⇒ Proc
Allows to use Op as a block:.
Constructor Details
#initialize(*arguments) ⇒ Op
Creates Op. Could (and recommended be also by its alias -- just
TimeMath(*arguments)
.
45 46 47 48 |
# File 'lib/time_math/op.rb', line 45 def initialize(*arguments) @arguments = arguments @operations = [] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
38 39 40 |
# File 'lib/time_math/op.rb', line 38 def arguments @arguments end |
#operations ⇒ Object (readonly)
Returns the value of attribute operations.
38 39 40 |
# File 'lib/time_math/op.rb', line 38 def operations @operations end |
Instance Method Details
#==(other) ⇒ Object
165 166 167 168 |
# File 'lib/time_math/op.rb', line 165 def ==(other) self.class == other.class && operations == other.operations && arguments == other.arguments end |
#advance(unit, amount = 1) ⇒ Op
Non-destructive version of #advance!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#advance!(unit, amount = 1) ⇒ self
Adds Units::Base#advance to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#call(*tms) ⇒ Time, ...
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
.
177 178 179 180 181 182 183 184 |
# 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 |
#ceil(unit, span = 1) ⇒ Op
Non-destructive version of #ceil!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#ceil!(unit, span = 1) ⇒ self
Adds Units::Base#ceil to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#decrease(unit, amount = 1) ⇒ Op
Non-destructive version of #decrease!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#decrease!(unit, amount = 1) ⇒ self
Adds Units::Base#decrease to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#floor(unit, span = 1) ⇒ Op
Non-destructive version of #floor!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#floor!(unit, span = 1) ⇒ self
Adds Units::Base#floor to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#inspect ⇒ Object
154 155 156 |
# File 'lib/time_math/op.rb', line 154 def inspect "#<#{self.class}#{inspect_args}" + inspect_operations + '>' end |
#next(unit, span = 1) ⇒ Op
Non-destructive version of #next!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#next!(unit, span = 1) ⇒ self
Adds Units::Base#next to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#prev(unit, span = 1) ⇒ Op
Non-destructive version of #prev!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#prev!(unit, span = 1) ⇒ self
Adds Units::Base#prev to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#round(unit, span = 1) ⇒ Op
Non-destructive version of #round!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#round!(unit, span = 1) ⇒ self
Adds Units::Base#round to list of operations.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/time_math/op.rb', line 142 OPERATIONS.each do |op| define_method "#{op}!" do |unit, *args| Units.names.include?(unit) or raise(ArgumentError, "Unknown unit #{unit}") @operations << [op, unit, args] self end define_method op do |unit, *args| dup.send("#{op}!", unit, *args) end end |
#to_proc ⇒ Proc
Allows to use Op as a block:
.map(&TimeMath().ceil(:week).advance(:day, 1))
192 193 194 |
# File 'lib/time_math/op.rb', line 192 def to_proc method(:call).to_proc end |