Class: Lookout::Stats::ComparableContainerComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
CompareMode, Rangeable
Defined in:
app/components/lookout/stats/comparable_container_component.rb

Instance Method Summary collapse

Methods included from Rangeable

#period

Methods included from CompareMode

#compare_mode?, #comparison_mode, included

Constructor Details

#initialize(url, label, comparable, formatter = nil, selected = false, compare = false) ⇒ ComparableContainerComponent

Returns a new instance of ComparableContainerComponent.



7
8
9
10
11
12
13
14
# File 'app/components/lookout/stats/comparable_container_component.rb', line 7

def initialize(url, label, comparable, formatter = nil, selected = false, compare = false)
  @url = url
  @label = label
  @comparable = comparable
  @formatter = formatter
  @selected = selected
  @compare = compare
end

Instance Method Details

#arrowObject

〰 ↓ ↑



29
30
31
32
33
34
35
36
37
# File 'app/components/lookout/stats/comparable_container_component.rb', line 29

def arrow
  if percentage.negative?
    ""
  elsif percentage.positive?
    ""
  else
    ""
  end
end

#compare?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/components/lookout/stats/comparable_container_component.rb', line 16

def compare?
  @compare
end

#compare_range_stringObject



76
77
78
# File 'app/components/lookout/stats/comparable_container_component.rb', line 76

def compare_range_string
  range_to_string(@comparable.compare_range)
end

#formatted(value) ⇒ Object



92
93
94
# File 'app/components/lookout/stats/comparable_container_component.rb', line 92

def formatted(value)
  public_send(@formatter, value)
end

#klassObject



20
21
22
23
24
25
26
# File 'app/components/lookout/stats/comparable_container_component.rb', line 20

def klass
  if percentage.negative?
    "text-red-400"
  else
    "text-green-400"
  end
end

#number_to_duration(duration) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/lookout/stats/comparable_container_component.rb', line 51

def number_to_duration(duration)
  seconds =
    case duration
    when nil
      nil
    when ActiveSupport::Duration
      duration.in_seconds
    else
      duration.to_f
    end

  if seconds && seconds > 0
    minutes = (seconds / 60).to_i
    seconds = (seconds % 60).to_i
    "#{minutes}m #{seconds}s"
  else
    "0m 0s"
  end
end

#number_to_percentage(number, options = {}) ⇒ Object



71
72
73
74
# File 'app/components/lookout/stats/comparable_container_component.rb', line 71

def number_to_percentage(number, options = {})
  precision = options.fetch(:precision, 0)
  "#{number.round(precision)}%"
end

#percentageObject



39
40
41
42
43
44
45
46
47
48
49
# File 'app/components/lookout/stats/comparable_container_component.rb', line 39

def percentage
  begin
    diff = value.current - value.compared_to
    if diff.zero?
      return 0
    end
    (value.current / diff).round(2) * 100
  rescue ZeroDivisionError
    0
  end
end

#range_stringObject



80
81
82
# File 'app/components/lookout/stats/comparable_container_component.rb', line 80

def range_string
  range_to_string(@comparable.range)
end

#tooltipObject



88
89
90
# File 'app/components/lookout/stats/comparable_container_component.rb', line 88

def tooltip
  "#{formatted(value.current)} vs #{formatted(value.compared_to)}#{number_to_percentage percentage} (#{arrow}) "
end

#valueObject



84
85
86
# File 'app/components/lookout/stats/comparable_container_component.rb', line 84

def value
  @comparable.result
end