Class: Timely::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/timely/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period, starts_at, options = {}) ⇒ Column

Returns a new instance of Column.



6
7
8
9
10
# File 'lib/timely/column.rb', line 6

def initialize(period, starts_at, options={})
  self.period     = period
  self.starts_at  = starts_at
  self.options    = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/timely/column.rb', line 4

def options
  @options
end

#periodObject

Returns the value of attribute period.



4
5
6
# File 'lib/timely/column.rb', line 4

def period
  @period
end

#starts_atObject

Returns the value of attribute starts_at.



4
5
6
# File 'lib/timely/column.rb', line 4

def starts_at
  @starts_at
end

Instance Method Details

#cache_keyObject



42
43
44
# File 'lib/timely/column.rb', line 42

def cache_key
  [starts_at.to_i, ends_at.to_i].join(Timely.cache_separator)
end

#cacheable?Boolean

only cache values when the period is over

Returns:

  • (Boolean)


47
48
49
# File 'lib/timely/column.rb', line 47

def cacheable?
  ends_at < Time.zone.now
end

#ends_atObject

calculate the end time



17
18
19
20
21
22
# File 'lib/timely/column.rb', line 17

def ends_at
  @ends_at ||= begin
    args = period == :quarter ? { months: 3 } : { periods => 1 }
    starts_at.advance args
  end
end

#inspectObject



12
13
14
# File 'lib/timely/column.rb', line 12

def inspect
  "#<#{self.class.name} period: #{period}, starts_at: #{starts_at}, ends_at: #{ends_at}>"
end

#midpointObject

calculate the time between the start and the end. useful as the x value for bar and line graphs



26
27
28
# File 'lib/timely/column.rb', line 26

def midpoint
  @midpoint ||= Time.at((starts_at.to_i + ends_at.to_i) / 2)
end

#titleObject



30
31
32
# File 'lib/timely/column.rb', line 30

def title
  format_time_for_human
end

#to_iObject



38
39
40
# File 'lib/timely/column.rb', line 38

def to_i
  starts_at.to_i
end

#to_sObject



34
35
36
# File 'lib/timely/column.rb', line 34

def to_s
  format_time_for_group
end