Class: TimeBoots::Boot

Inherits:
Object
  • Object
show all
Includes:
TimeBoots
Defined in:
lib/time_boots/boot.rb

Direct Known Subclasses

MonthBoot, SimpleBoot, YearBoot

Constant Summary

Constants included from TimeBoots

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeBoots

#day, #hour, #min, #month, #sec, #steps, #week, #year

Constructor Details

#initialize(step) ⇒ Boot

Returns a new instance of Boot.



4
5
6
# File 'lib/time_boots/boot.rb', line 4

def initialize(step)
  @step = step
end

Instance Attribute Details

#stepObject (readonly)

Returns the value of attribute step.



8
9
10
# File 'lib/time_boots/boot.rb', line 8

def step
  @step
end

Class Method Details

.get(step) ⇒ Object



126
127
128
129
# File 'lib/time_boots/boot.rb', line 126

def get(step)
  BOOTS[step] or
    raise ArgumentError, "Unsupported step: #{step}"
end

.stepsObject



122
123
124
# File 'lib/time_boots/boot.rb', line 122

def steps
  BOOTS.keys
end

Instance Method Details

#advance(tm, steps = 1) ⇒ Object



37
38
39
40
# 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) ⇒ Object



21
22
23
24
25
# File 'lib/time_boots/boot.rb', line 21

def ceil(tm)
  f = floor(tm)

  f == tm ? f : advance(f)
end

#decrease(tm, steps = 1) ⇒ Object



42
43
44
45
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



64
65
66
# File 'lib/time_boots/boot.rb', line 64

def jump(steps)
  Jump.new(step, steps)
end

#lace(from, to, options = {}) ⇒ Object



68
69
70
# File 'lib/time_boots/boot.rb', line 68

def lace(from, to, options = {})
  Lace.new(step, from, to, options)
end

#measure(_from, _to) ⇒ Object

Raises:

  • (NotImplementedError)


55
56
57
# 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) ⇒ Object



59
60
61
62
# 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) ⇒ Object



47
48
49
# File 'lib/time_boots/boot.rb', line 47

def range(tm, steps = 1)
  (tm...advance(tm, steps))
end

#range_back(tm, steps = 1) ⇒ Object



51
52
53
# File 'lib/time_boots/boot.rb', line 51

def range_back(tm, steps = 1)
  (decrease(tm, steps)...tm)
end

#round(tm) ⇒ Object



27
28
29
30
31
# 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) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/time_boots/boot.rb', line 33

def round?(tm)
  floor(tm) == tm
end