Class: DOTIW::TimeHash

Inherits:
Object
  • Object
show all
Defined in:
lib/dotiw/time_hash.rb

Constant Summary collapse

TIME_FRACTIONS =
%i[seconds minutes hours days weeks months years].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distance, from_time, to_time = nil, options = {}) ⇒ TimeHash

Returns a new instance of TimeHash.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dotiw/time_hash.rb', line 9

def initialize(distance, from_time, to_time = nil, options = {})
  @output     = {}
  @options    = options.dup
  @distance   = distance
  @from_time  = from_time || Time.current
  @to_time    = to_time   || (@to_time_not_given = true && @from_time + distance.seconds)
  @smallest, @largest = [@from_time, @to_time].minmax
  @to_time   += 1.hour if @to_time_not_given && smallest.dst? && !largest.dst?
  @to_time   -= 1.hour if @to_time_not_given && !smallest.dst? && largest.dst?
  @smallest, @largest = [@from_time, @to_time].minmax
  @distance ||= begin
    d = largest - smallest
    d -= 1.hour if smallest.dst? && !largest.dst?
    d += 1.hour if !smallest.dst? && largest.dst?
    d
  end

  build_time_hash
end

Instance Attribute Details

#distanceObject (readonly)

Returns the value of attribute distance.



7
8
9
# File 'lib/dotiw/time_hash.rb', line 7

def distance
  @distance
end

#from_timeObject (readonly)

Returns the value of attribute from_time.



7
8
9
# File 'lib/dotiw/time_hash.rb', line 7

def from_time
  @from_time
end

#largestObject (readonly)

Returns the value of attribute largest.



7
8
9
# File 'lib/dotiw/time_hash.rb', line 7

def largest
  @largest
end

#smallestObject (readonly)

Returns the value of attribute smallest.



7
8
9
# File 'lib/dotiw/time_hash.rb', line 7

def smallest
  @smallest
end

#to_timeObject (readonly)

Returns the value of attribute to_time.



7
8
9
# File 'lib/dotiw/time_hash.rb', line 7

def to_time
  @to_time
end

Instance Method Details

#to_hashObject



29
30
31
# File 'lib/dotiw/time_hash.rb', line 29

def to_hash
  output
end