Class: AhoyCaptain::PeriodCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy_captain/period_collection.rb

Defined Under Namespace

Classes: Period

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePeriodCollection

Returns a new instance of PeriodCollection.



59
60
61
62
63
# File 'lib/ahoy_captain/period_collection.rb', line 59

def initialize
  @periods = {}
  @default = nil
  @max = nil
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



58
59
60
# File 'lib/ahoy_captain/period_collection.rb', line 58

def default
  @default
end

#maxObject

Returns the value of attribute max.



58
59
60
# File 'lib/ahoy_captain/period_collection.rb', line 58

def max
  @max
end

Class Method Details

.load_defaultObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ahoy_captain/period_collection.rb', line 12

def self.load_default
  instance = new
  {
    realtime: {
      label: "Realtime",
      range: -> { [1.minute.ago] },
    },
    day: {
      label: "Day",
      range: -> { [Time.current.beginning_of_day, Time.current] },
    },
    '7d': {
      label: "7 Days",
      range: -> {  [7.days.ago, Time.current] },
    },
    '30d': {
      label: "30 Days",
      range: -> { [30.days.ago, Time.current] },
    },
    mtd: {
      label: "Month-to-date",
      range: -> { [Time.current.beginning_of_month, Time.current] },
    },
    lastmonth: {
      label: "Last month",
      range: -> { [1.month.ago.beginning_of_month, 1.month.ago.end_of_month] },
    },
    ytd: {
      label: "This year",
      range: -> { [Time.current.beginning_of_year, Time.current] },
    },
    '12mo': {
      label: "12 months",
      range: -> { [12.months.ago.to_datetime, Time.current] },
    },
    all: {
      label: "All-time",
      range: -> { [Date.new(2004, 8, 1).to_datetime, Time.current] },
    },
  }.each do |param, options|
    instance.add(param, options[:label], options[:range])
  end
  instance.default = :'30d'
  instance
end

Instance Method Details

#add(param, label, range_proc) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/ahoy_captain/period_collection.rb', line 65

def add(param, label, range_proc)
  raise ArgumentError, "range must be a proc, or respond to .call" unless range_proc.respond_to?(:call)
  raise ArgumentError, "range.call must return a range or an array" unless range_proc.call.is_a?(Range) || range_proc.call.is_a?(Array)

  @periods[param.to_sym] = Period.new(param: param, label: label, range: range_proc)
end

#allObject



89
90
91
# File 'lib/ahoy_captain/period_collection.rb', line 89

def all
  @periods
end

#delete(param) ⇒ Object



72
73
74
# File 'lib/ahoy_captain/period_collection.rb', line 72

def delete(param)
  @periods.delete(param.to_sym)
end

#each(&block) ⇒ Object



81
82
83
# File 'lib/ahoy_captain/period_collection.rb', line 81

def each(&block)
  @periods.each(&block)
end

#find(value) ⇒ Object



85
86
87
# File 'lib/ahoy_captain/period_collection.rb', line 85

def find(value)
  @periods[value.try(:to_sym)]
end

#for(value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ahoy_captain/period_collection.rb', line 101

def for(value)
  if value.nil?
    period = @periods[@default]
  else
    period = (@periods[value.to_sym] || @periods[@default])
  end

  if period
    period.range.call
  else
    nil
  end
end

#resetObject



76
77
78
79
# File 'lib/ahoy_captain/period_collection.rb', line 76

def reset
  @periods = {}
  @default = nil
end