Class: AhoyCaptain::Stats::ComparableContainerComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- AhoyCaptain::Stats::ComparableContainerComponent
show all
- Includes:
- CompareMode, Rangeable
- Defined in:
- app/components/ahoy_captain/stats/comparable_container_component.rb
Instance Method Summary
collapse
Methods included from Rangeable
#period
#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/ahoy_captain/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
#arrow ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 29
def arrow
if percentage.negative?
"↓"
elsif percentage.positive?
"↑"
else
"〰"
end
end
|
#compare? ⇒ Boolean
16
17
18
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 16
def compare?
@compare
end
|
#compare_range_string ⇒ Object
59
60
61
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 59
def compare_range_string
range_to_string(@comparable.compare_range)
end
|
75
76
77
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 75
def formatted(value)
public_send(@formatter, value)
end
|
#klass ⇒ Object
20
21
22
23
24
25
26
|
# File 'app/components/ahoy_captain/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
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 51
def number_to_duration(duration)
if duration
"#{duration.in_minutes.to_i}M #{duration.parts[:seconds].to_i}S"
else
"0M 0S"
end
end
|
#percentage ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
|
# File 'app/components/ahoy_captain/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_string ⇒ Object
63
64
65
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 63
def range_string
range_to_string(@comparable.range)
end
|
71
72
73
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 71
def tooltip
"#{formatted(value.current)} vs #{formatted(value.compared_to)} — #{number_to_percentage percentage} (#{arrow}) "
end
|
#value ⇒ Object
67
68
69
|
# File 'app/components/ahoy_captain/stats/comparable_container_component.rb', line 67
def value
@comparable.result
end
|