Class: AhoyCaptain::ComparisonMode

Inherits:
Object
  • Object
show all
Includes:
RangeOptions, Rangeable
Defined in:
app/models/ahoy_captain/comparison_mode.rb

Constant Summary collapse

VALID_COMPARISONS =
%w{previous year true}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rangeable

#period

Constructor Details

#initialize(params) ⇒ ComparisonMode

Returns a new instance of ComparisonMode.



9
10
11
# File 'app/models/ahoy_captain/comparison_mode.rb', line 9

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'app/models/ahoy_captain/comparison_mode.rb', line 8

def params
  @params
end

Instance Method Details

#compared_to_rangeObject



37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/ahoy_captain/comparison_mode.rb', line 37

def compared_to_range
  return custom_compare if custom_compare?

  if type == :true || type == :previous
    RangeFromParams.new(period: nil, start_date: (range[0] - (range[1] - range[0])).utc.to_s, end_date: range[0].utc.to_s).build
  elsif type == :year
    RangeFromParams.new(period: nil, start_date: range[0].change(year: range[0].year - 1).utc.to_s, end_date: range[1].change(year: range[1].year - 1).utc.to_s).build
  else
    raise ArgumentError
  end
end

#custom_compareObject



62
63
64
65
66
# File 'app/models/ahoy_captain/comparison_mode.rb', line 62

def custom_compare
  return nil unless (params[:compare_to_start_date].present? && params[:compare_to_end_date].present?)

  RangeFromParams.new(period: nil, start_date: params[:compare_to_start_date], end_date: params[:compare_to_end_date]).build
end

#custom_compare?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/ahoy_captain/comparison_mode.rb', line 68

def custom_compare?
  custom_compare
end

#enabled?(strict = true) ⇒ Boolean

can’t compare realtime

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'app/models/ahoy_captain/comparison_mode.rb', line 14

def enabled?(strict = true)
  comparing = (params[:comparison].in?(VALID_COMPARISONS) || custom_compare?)
  if strict
    comparing && !range.realtime?
  else
    comparing
  end
end

#labelObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/ahoy_captain/comparison_mode.rb', line 23

def label
  if custom_compare?
    return "Custom period"
  end

  if type == :true || type == :previous
    "Previous period"
  elsif type == :year
    "Year-over-year"
  else
    raise ArgumentError
  end
end

#match_toObject



56
57
58
59
60
# File 'app/models/ahoy_captain/comparison_mode.rb', line 56

def match_to
  return params[:compare_to].to_sym if params[:compare_to].in?(%w{dow date})

  nil
end

#typeObject



49
50
51
52
53
54
# File 'app/models/ahoy_captain/comparison_mode.rb', line 49

def type
  return params[:comparison].to_sym if params[:comparison].in?(VALID_COMPARISONS)
  return :custom if custom_compare?

  nil
end