class TimeBoots::Boot
Constants
- BOOTS
- DEFAULT_STEP_VALUES
- NATURAL_STEPS
Attributes
step[R]
Public Class Methods
new(step)
click to toggle source
# File lib/time_boots/boot.rb, line 4 def initialize(step) @step = step end
Protected Class Methods
get(step)
click to toggle source
# File lib/time_boots/boot.rb, line 126 def get(step) BOOTS[step] or raise ArgumentError, "Unsupported step: #{step}" end
steps()
click to toggle source
# File lib/time_boots/boot.rb, line 122 def steps BOOTS.keys end
Public Instance Methods
advance(tm, steps = 1)
click to toggle source
# File lib/time_boots/boot.rb, line 37 def advance(tm, steps = 1) return decrease(tm, -steps) if steps < 0 _advance(tm, steps) end
ceil(tm)
click to toggle source
# File lib/time_boots/boot.rb, line 21 def ceil(tm) f = floor(tm) f == tm ? f : advance(f) end
decrease(tm, steps = 1)
click to toggle source
# File lib/time_boots/boot.rb, line 42 def decrease(tm, steps = 1) return advance(tm, -steps) if steps < 0 _decrease(tm, steps) end
floor(tm)
click to toggle source
# File lib/time_boots/boot.rb, line 10 def floor(tm) components = [tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec].first(step_idx + 1) new_from_components(tm, *components) end
jump(steps)
click to toggle source
# File lib/time_boots/boot.rb, line 64 def jump(steps) Jump.new(step, steps) end
lace(from, to, options = {})
click to toggle source
# File lib/time_boots/boot.rb, line 68 def lace(from, to, options = {}) Lace.new(step, from, to, options) end
measure(_from, _to)
click to toggle source
# File lib/time_boots/boot.rb, line 55 def measure(_from, _to) raise NotImplementedError, '#measure should be implemented in subclasses' end
measure_rem(from, to)
click to toggle source
# File lib/time_boots/boot.rb, line 59 def measure_rem(from, to) m = measure(from, to) [m, advance(from, m)] end
range(tm, steps = 1)
click to toggle source
# File lib/time_boots/boot.rb, line 47 def range(tm, steps = 1) (tm...advance(tm, steps)) end
range_back(tm, steps = 1)
click to toggle source
# File lib/time_boots/boot.rb, line 51 def range_back(tm, steps = 1) (decrease(tm, steps)...tm) end
round(tm)
click to toggle source
# File lib/time_boots/boot.rb, line 27 def round(tm) f, c = floor(tm), ceil(tm) (tm - f).abs < (tm - c).abs ? f : c end
round?(tm)
click to toggle source
# File lib/time_boots/boot.rb, line 33 def round?(tm) floor(tm) == tm end
Protected Instance Methods
generate(tm, replacements = {})
click to toggle source
# File lib/time_boots/boot.rb, line 82 def generate(tm, replacements = {}) hash_to_tm(tm, tm_to_hash(tm).merge(replacements)) end
hash_to_tm(origin, hash)
click to toggle source
# File lib/time_boots/boot.rb, line 90 def hash_to_tm(origin, hash) components = NATURAL_STEPS.map { |s| hash[s] || 0 } new_from_components(origin, *components) end
new_from_components(origin, *components)
click to toggle source
# File lib/time_boots/boot.rb, line 95 def new_from_components(origin, *components) components = DEFAULT_STEP_VALUES.zip(components).map { |d, c| c || d } case origin when Time Time.mktime(*components.reverse, nil, nil, nil, origin.zone) when DateTime DateTime.new(*components, origin.zone) else raise ArgumentError, "Expected Time or DateTime, got #{origin.class}" end end
step_idx()
click to toggle source
# File lib/time_boots/boot.rb, line 77 def step_idx NATURAL_STEPS.index(step) or raise NotImplementedError, "Can not be used for step #{step}" end
tm_to_hash(tm)
click to toggle source
# File lib/time_boots/boot.rb, line 86 def tm_to_hash(tm) Hash[*NATURAL_STEPS.flat_map { |s| [s, tm.send(s)] }] end