Module: Timetastic

Defined in:
lib/timetastic.rb

Defined Under Namespace

Classes: Traveller

Constant Summary collapse

Domains =
[ :hours, :weeks, :days, :months, :years ]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.fixed_timeObject

see Timetastic#fixate() below



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

def fixed_time
  @fixed_time
end

Class Method Details

.days_in_month(year, month) ⇒ Object

Snippet that calculates the number of days in any given month taking into account leap years.

Credit goes to this SO thread: bit.ly/4GjMor



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

def days_in_month(year, month)
  (Date.new(year, 12, 31) << (12-month)).day
end

.fixate(y, m = 1, d = 1, h = 0, mi = 0, s = 0, &block) ⇒ Object

Fixes the relative time by which all of Timetastic operations are carried out for the duration of the given block.

Mainly used for tests and gem development.

An alternative way to ‘fix’ the time is to directly use the exposed attribute Timetastic#fixed_time, ie:

> Timetastic.fixed_time = Time.new(Time.now.year, 6, 1)

You can simply set the attribute to nil to reset the time back to Time.now



37
38
39
40
41
42
43
44
45
# File 'lib/timetastic.rb', line 37

def fixate(y, m = 1, d = 1, h = 0, mi = 0, s = 0, &block)
  if y.is_a?(Time)
    @fixed_time = y
  else
    @fixed_time = Time.new(y,m,d,h,mi,s)
  end
  block.call(@fixed_time)
  @fixed_time = nil
end

.last(delta = 1, relative_to = nil) ⇒ Object Also known as: past



10
11
12
# File 'lib/timetastic.rb', line 10

def last(delta = 1, relative_to = nil)
  Traveller.new(-1, delta, relative_to)
end

.next(delta = 1, relative_to = nil) ⇒ Object Also known as: coming



16
17
18
# File 'lib/timetastic.rb', line 16

def next(delta = 1, relative_to = nil)
  Traveller.new(1, delta, relative_to)
end

.nowObject

Returns the relative time by which operations are carried out



56
57
58
# File 'lib/timetastic.rb', line 56

def now()
  @fixed_time || Time.now
end

.thisObject



20
21
22
# File 'lib/timetastic.rb', line 20

def this()
  Traveller.new(1, 0)
end