comparison 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/MIT-LICENSE +1 -1
- data/README.md +51 -31
- data/Rakefile +4 -5
- data/app/helpers/comparison/application_helper.rb +6 -4
- data/config/locales/en.yml +8 -5
- data/lib/comparison/comparator.rb +43 -17
- data/lib/comparison/engine.rb +3 -5
- data/lib/comparison/presenter.rb +153 -116
- data/lib/comparison/version.rb +3 -1
- data/lib/comparison.rb +3 -1
- metadata +35 -9
- data/lib/tasks/comparison_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b1789756840509d6bfc178a7fce308b15e8097b8321362f36d2ceba7970862c0
|
4
|
+
data.tar.gz: cfd348911fee4ae30d3857985017d290dadb3f89af2e826b086b84a0c46c180e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce42dd5c8370367bdca1c66fa40277515b80c352c6db76222b4f0d5a9f8e40581a56d04396075802f86bbd4a41043167c719bb4d9406272e9d0ddd67792e43d9
|
7
|
+
data.tar.gz: f1d697351b7f16e05fc3b5394c5f22c70fac9f5e6db17cf5898cdd4adef82ecb8e4d83b3bbfd5fac113daa2f558f8ce83d3e5417b29c288d8c821cb4da325d70
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,40 +1,45 @@
|
|
1
|
-
#
|
1
|
+
# [](https://badge.fury.io/rb/comparison)
|
2
2
|
|
3
|
-
|
4
|
-
numbers to each other. For example, a report that compares the outcome for a
|
5
|
-
quarter to the outcome of the same quarter in the prior year. Frequently I end
|
6
|
-
up displaying both the raw difference between the two numbers, the percentage
|
7
|
-
difference, and maybe a simple visual indicator such as an arrow (pointing up
|
8
|
-
or down). Sometimes these comparisons require special handling for Infinity and
|
9
|
-
NaN when one or both numbers are zero.
|
3
|
+
# Comparison
|
10
4
|
|
11
|
-
|
12
|
-
|
5
|
+
Comparison bundles up into Rails helpers the logic for rendering visually
|
6
|
+
informative comparisons of numbers. For example, say you were comparing the
|
7
|
+
sales figures from one quarter to the same quarter in a previous year. You
|
8
|
+
might want to show the percentage change, accompanied by an arrow or icon and
|
9
|
+
color-coded to indicate positive or negative growth. This plugin provides
|
10
|
+
helpers that abstract the logic of deciding what to show into a handful of
|
11
|
+
simple methods and leveraging I18n.
|
13
12
|
|
14
13
|
## Usage
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
|
15
|
+
The library has three components: the `Comparator` class for performing the
|
16
|
+
actual math, the `Presenter` class for decorating the comparator with
|
17
|
+
view-friendly output, and a helper module for using the presenter with the
|
18
18
|
view.
|
19
19
|
|
20
|
-
`#compare` helper takes the two numbers to be compared and yields the
|
21
|
-
|
20
|
+
The `#compare` helper takes the two numbers to be compared and yields the
|
21
|
+
presenter to a block.
|
22
22
|
|
23
|
-
`#
|
24
|
-
literally `
|
23
|
+
`#difference_as_currency` provides the difference between the two numbers,
|
24
|
+
literally `value - other`, formatted as currency. Formatting is handled by
|
25
|
+
`ActiveSupport::NumberHelper#number_to_currency`.
|
25
26
|
|
26
27
|
`#percentage` provides the percentage difference between the two numbers. Under
|
27
|
-
the hood it uses `
|
28
|
-
|
28
|
+
the hood it uses `ActiveSupport#NumberHelper#number_to_percentage` to format
|
29
|
+
the percentage. Options are passed through to that method.
|
30
|
+
|
31
|
+
`#unsigned_percentage` returns the absolute value of the percentage. Use this
|
32
|
+
if you prefer to use other cues, such as colors or icons, to indicate positive
|
33
|
+
or negative changes.
|
29
34
|
|
30
35
|
`#arrow` returns an HTML character entity for an arrow (up, down, or, for no
|
31
36
|
change, an empty string).
|
32
37
|
|
33
38
|
```erb
|
34
|
-
<%= compare
|
35
|
-
<td><%= number_to_currency
|
36
|
-
<td><%= number_to_currency
|
37
|
-
<td><%= cmp.
|
39
|
+
<%= compare value, other do |cmp| %>
|
40
|
+
<td><%= number_to_currency value %></td>
|
41
|
+
<td><%= number_to_currency other %></td>
|
42
|
+
<td><%= cmp.difference_as_currency %></td>
|
38
43
|
<td>
|
39
44
|
<%= cmp.arrow %>
|
40
45
|
<%= cmp.percentage precision: 1 %>
|
@@ -53,6 +58,7 @@ change, an empty string).
|
|
53
58
|
|
54
59
|
|
55
60
|
## Configuration
|
61
|
+
|
56
62
|
Comparison uses I18n to configure the output of some of the Presenter methods.
|
57
63
|
Default implementations are provided where it makes sense. You can provide your
|
58
64
|
own implementations by adding translations to your application.
|
@@ -60,14 +66,14 @@ own implementations by adding translations to your application.
|
|
60
66
|
```yml
|
61
67
|
en:
|
62
68
|
comparison:
|
63
|
-
|
69
|
+
dom_classes:
|
64
70
|
positive: 'comparison positive'
|
65
71
|
negative: 'comparison negative'
|
66
72
|
nochange: 'comparison nochange'
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
73
|
+
inline_style:
|
74
|
+
positive: 'color: #3c763d; background-color: #dff0d8;'
|
75
|
+
negative: 'color: #a94442; background-color: #f2dede;'
|
76
|
+
nochange: 'color: #777777;'
|
71
77
|
icons:
|
72
78
|
positive_html: '<span class="glyphicon glyphicon-arrow-up"></span>'
|
73
79
|
negative_html: '<span class="glyphicon glyphicon-arrow-down"></span>'
|
@@ -76,9 +82,16 @@ en:
|
|
76
82
|
positive_html: '↑'
|
77
83
|
negative_html: '↓'
|
78
84
|
nochange_html: ''
|
85
|
+
infinity_html: '∞'
|
79
86
|
```
|
80
87
|
|
88
|
+
If the `dom_classes` keys are not present, translation falls back on `classes`.
|
89
|
+
If the `inline_style` keys are not present, translation falls back on `style`
|
90
|
+
and `css`. In both cases, those fallback keys are deprecated. Expect support
|
91
|
+
for those keys to eventually be dropped.
|
92
|
+
|
81
93
|
## Installation
|
94
|
+
|
82
95
|
Add this line to your application's Gemfile:
|
83
96
|
|
84
97
|
```ruby
|
@@ -90,15 +103,22 @@ And then execute:
|
|
90
103
|
$ bundle
|
91
104
|
```
|
92
105
|
|
93
|
-
|
94
|
-
|
95
|
-
|
106
|
+
Include the Comparison helpers by adding the following to your
|
107
|
+
application\_helper.rb:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
# app/helpers/application_helper.rb
|
111
|
+
module ApplicationHelper
|
112
|
+
include Comparison::ApplicationHelper
|
113
|
+
end
|
96
114
|
```
|
97
115
|
|
98
116
|
## Contributing
|
117
|
+
|
99
118
|
Open an GitHub issue for problems and suggestions. This library is in its
|
100
119
|
infancy, so use it at your own risk.
|
101
120
|
|
102
121
|
## License
|
122
|
+
|
103
123
|
The gem is available as open source under the terms of the
|
104
124
|
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'bundler/setup'
|
3
5
|
rescue LoadError
|
@@ -14,22 +16,19 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
16
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
17
|
end
|
16
18
|
|
17
|
-
|
19
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
20
|
+
load 'rails/tasks/engine.rake'
|
18
21
|
|
19
22
|
load 'rails/tasks/statistics.rake'
|
20
23
|
|
21
|
-
|
22
|
-
|
23
24
|
require 'bundler/gem_tasks'
|
24
25
|
|
25
26
|
require 'rake/testtask'
|
26
27
|
|
27
28
|
Rake::TestTask.new(:test) do |t|
|
28
|
-
t.libs << 'lib'
|
29
29
|
t.libs << 'test'
|
30
30
|
t.pattern = 'test/**/*_test.rb'
|
31
31
|
t.verbose = false
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
34
|
task default: :test
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
module Comparison
|
2
|
-
module ApplicationHelper
|
4
|
+
module ApplicationHelper # :nodoc:
|
3
5
|
##
|
4
|
-
# Returns a Presenter for a Comparator for +
|
6
|
+
# Returns a Presenter for a Comparator for +value+ and +other+.
|
5
7
|
#
|
6
8
|
# If a block is given, the Presenter is yielded to the block.
|
7
|
-
def compare(
|
8
|
-
comparison = Presenter.new Comparator.new
|
9
|
+
def compare(value, other)
|
10
|
+
comparison = Presenter.new Comparator.new value, other
|
9
11
|
yield comparison if block_given?
|
10
12
|
comparison
|
11
13
|
end
|
data/config/locales/en.yml
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
en:
|
2
2
|
comparison:
|
3
|
-
#
|
3
|
+
# dom_classes:
|
4
4
|
# positive: 'comparison positive'
|
5
5
|
# negative: 'comparison negative'
|
6
6
|
# nochange: 'comparison nochange'
|
7
|
-
|
8
|
-
positive_html: ''
|
9
|
-
negative_html: ''
|
10
|
-
nochange_html: ''
|
7
|
+
|
11
8
|
# icons:
|
12
9
|
# positive_html: '<span class="glyphicon glyphicon-arrow-up"></span>'
|
13
10
|
# negative_html: '<span class="glyphicon glyphicon-arrow-down"></span>'
|
14
11
|
# nochange_html: '<span class="glyphicon glyphicon-minus"></span>'
|
12
|
+
|
13
|
+
inline_style:
|
14
|
+
positive: ''
|
15
|
+
negative: ''
|
16
|
+
nochange: ''
|
17
|
+
|
15
18
|
arrows:
|
16
19
|
positive_html: '↑'
|
17
20
|
negative_html: '↓'
|
@@ -1,39 +1,65 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
require 'bigdecimal'
|
2
4
|
require 'forwardable'
|
3
5
|
|
4
6
|
module Comparison
|
7
|
+
##
|
8
|
+
# The Comparator object compares two numbers to each other and exposes the
|
9
|
+
# raw and percentage differences.
|
5
10
|
class Comparator
|
6
11
|
extend Forwardable
|
7
12
|
|
13
|
+
attr_reader :value, :other
|
14
|
+
|
8
15
|
##
|
9
|
-
# Instantiates a new Comparator to compare two numbers, +
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
# Instantiates a new Comparator to compare two numbers, +value+ and +other+.
|
17
|
+
#
|
18
|
+
# Both numbers will be converted to instances of `BigDecimal`.
|
19
|
+
def initialize(value, other)
|
20
|
+
@value = value.to_d
|
21
|
+
@other = other.to_d
|
13
22
|
end
|
14
23
|
|
15
|
-
|
16
|
-
|
17
|
-
delegate %i[infinite? nan? negative? positive? zero?] => :relative
|
24
|
+
delegate %i[negative? positive? zero? nonzero?] => :difference
|
25
|
+
delegate %i[infinite? nan?] => :change
|
18
26
|
|
19
27
|
##
|
20
|
-
# Returns the difference between +@
|
28
|
+
# Returns the difference between +@value+ and +@other+.
|
29
|
+
def difference
|
30
|
+
value - other
|
31
|
+
end
|
32
|
+
|
21
33
|
def absolute
|
22
|
-
|
34
|
+
Kernel.warn "DEPRECATION WARNING: use #difference instead of #absolute (called from #{caller(1..1).first})"
|
35
|
+
difference
|
23
36
|
end
|
24
37
|
|
25
|
-
|
38
|
+
##
|
39
|
+
# Returns the relative change of +@value+ from +@other+.
|
40
|
+
def change
|
41
|
+
difference / other.abs
|
42
|
+
end
|
26
43
|
|
27
44
|
##
|
28
|
-
# Returns the
|
45
|
+
# Returns the relative change of +@value+ from +@other+ as a percentage.
|
46
|
+
def percentage
|
47
|
+
change * 100
|
48
|
+
end
|
49
|
+
|
29
50
|
def relative
|
30
|
-
|
31
|
-
|
32
|
-
else
|
33
|
-
(m / n - 1) * 100
|
34
|
-
end
|
51
|
+
Kernel.warn "DEPRECATION WARNING: use #percentage instead of #relative (called from #{caller(1..1).first})"
|
52
|
+
change
|
35
53
|
end
|
36
54
|
|
37
|
-
|
55
|
+
def m
|
56
|
+
Kernel.warn "DEPRECATION WARNING: use #value instead of #m (called from #{caller(1..1).first})"
|
57
|
+
value
|
58
|
+
end
|
59
|
+
|
60
|
+
def n
|
61
|
+
Kernel.warn "DEPRECATION WARNING: use #other instead of #n (called from #{caller(1..1).first})"
|
62
|
+
other
|
63
|
+
end
|
38
64
|
end
|
39
65
|
end
|
data/lib/comparison/engine.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
module Comparison
|
2
|
-
class Engine < ::Rails::Engine
|
4
|
+
class Engine < ::Rails::Engine # :nodoc:
|
3
5
|
isolate_namespace Comparison
|
4
|
-
|
5
|
-
initializer 'comparison.view_helpers' do
|
6
|
-
ActionView::Base.send :include, ApplicationHelper
|
7
|
-
end
|
8
6
|
end
|
9
7
|
end
|
data/lib/comparison/presenter.rb
CHANGED
@@ -1,55 +1,72 @@
|
|
1
1
|
# frozen-string-literal: true
|
2
2
|
|
3
3
|
require 'delegate'
|
4
|
-
require 'forwardable'
|
5
4
|
|
6
5
|
module Comparison
|
6
|
+
##
|
7
|
+
# The Presenter object wraps a Comparator with methods that return
|
8
|
+
# view-friendly output.
|
7
9
|
class Presenter < DelegateClass(Comparator)
|
8
|
-
extend Forwardable
|
9
10
|
include ActionView::Helpers::TranslationHelper
|
10
11
|
|
11
|
-
ARROWS = {
|
12
|
-
|
13
|
-
# TODO: This shouldn't necessarily return a currency representation.
|
12
|
+
ARROWS = { positive: '↑', negative: '↓', nochange: '' }.freeze
|
14
13
|
|
15
14
|
##
|
16
|
-
# Returns
|
17
|
-
def
|
15
|
+
# Returns Comparator#difference formatted as currency.
|
16
|
+
def difference_as_currency(**options)
|
18
17
|
if positive?
|
19
|
-
number_to_currency
|
18
|
+
number_to_currency __getobj__.difference, format: '+%u%n', **options
|
20
19
|
else
|
21
|
-
number_to_currency
|
20
|
+
number_to_currency __getobj__.difference, **options
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
24
|
##
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
25
|
+
# Deprecated alias for `#difference_as_currency`.
|
26
|
+
#
|
27
|
+
# In a future release, this method will be changed to delegate directly
|
28
|
+
# `Comparator#difference`.
|
29
|
+
def difference(...)
|
30
|
+
Kernel.warn 'DEPRECATION WARNING: use #difference_as_currency instead of #difference' \
|
31
|
+
" (called from #{caller(3..3).first})"
|
32
|
+
difference_as_currency(...)
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Returns Comparator#change formatted as a percentage.
|
37
|
+
#
|
38
|
+
# If the change evaluates to Infinity or -Infinity, the I18n translaation
|
39
|
+
# for infinity is returned. If no translation is defined, `nil` is
|
40
|
+
# returned.
|
41
|
+
#
|
42
|
+
# If the change evaluates to NaN, it is returned as 0.
|
43
|
+
def percentage(**options)
|
44
|
+
if nan? || zero?
|
45
|
+
number_to_percentage 0, **options
|
46
|
+
elsif infinite?
|
47
|
+
t 'comparison.infinity_html', default: nil
|
48
|
+
elsif positive?
|
49
|
+
number_to_percentage __getobj__.percentage, format: '+%n%', **options
|
40
50
|
else
|
41
|
-
number_to_percentage
|
42
|
-
precision: precision, **options
|
51
|
+
number_to_percentage __getobj__.percentage, **options
|
43
52
|
end
|
44
53
|
end
|
45
54
|
|
46
|
-
|
47
|
-
|
55
|
+
##
|
56
|
+
# Returns the absolute value of Comparator#change formatted as a percentage.
|
57
|
+
#
|
58
|
+
# See `#percentage` for additional information on special return scenarios.
|
59
|
+
#
|
60
|
+
# Use this if you are relying on other cues (colors and/or icons) to
|
61
|
+
# indicate positive or negative values.
|
62
|
+
def unsigned_percentage(**options)
|
63
|
+
return percentage(**options) if nan? || infinite?
|
48
64
|
|
49
|
-
|
65
|
+
number_to_percentage __getobj__.percentage.abs, **options
|
66
|
+
end
|
50
67
|
|
51
68
|
##
|
52
|
-
# Returns the I18n translation for `comparison.icons`. (See also
|
69
|
+
# Returns the I18n translation for `comparison.icons`. (See also #arrow.)
|
53
70
|
#
|
54
71
|
# This method is intended to display a graphical representation of the
|
55
72
|
# comparison. Typically this would be an arrow pointing up or down.
|
@@ -61,131 +78,151 @@ module Comparison
|
|
61
78
|
# with Bootstrap, you could add the following translations to your
|
62
79
|
# application:
|
63
80
|
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
81
|
+
# en:
|
82
|
+
# comparison:
|
83
|
+
# icons:
|
84
|
+
# positive_html: '<span class="glyphicon glyphicon-arrow-up"></span>'
|
85
|
+
# negative_html: '<span class="glyphicon glyphicon-arrow-down"></span>'
|
86
|
+
# nochange_html: '<span class="glyphicon glyphicon-minus"></span>'
|
87
|
+
#
|
70
88
|
def icon
|
71
|
-
|
72
|
-
|
73
|
-
t 'comparison.icons.positive_html'
|
74
|
-
when negative?
|
75
|
-
t 'comparison.icons.negative_html'
|
76
|
-
else
|
77
|
-
t 'comparison.icons.nochange_html'
|
78
|
-
end
|
89
|
+
key, = expand_i18n_keys 'icons', suffix: '_html'
|
90
|
+
t key
|
79
91
|
end
|
80
92
|
|
81
93
|
##
|
82
|
-
# Returns the I18n translation for `comparison.icons`. (See also
|
94
|
+
# Returns the I18n translation for `comparison.icons`. (See also #icon.)
|
83
95
|
#
|
84
96
|
# This method is intended to display a graphical representation of the
|
85
97
|
# comparison. Typically this would be an arrow pointing up or down.
|
86
98
|
#
|
87
99
|
# The default implementation is as follows:
|
88
100
|
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
101
|
+
# en:
|
102
|
+
# comparison:
|
103
|
+
# arrows:
|
104
|
+
# positive_html: '↑'
|
105
|
+
# negative_html: '↓'
|
106
|
+
# nochange_html: ''
|
95
107
|
#
|
96
108
|
# For example, to generate up and down arrows using Glyphicons included
|
97
109
|
# with Bootstrap, you could add the following translations to your
|
98
110
|
# application:
|
99
111
|
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
112
|
+
# #arrows and its sister method #icon perform roughly identical tasks with
|
113
|
+
# roughly identical intentions. The difference between the two methods is
|
114
|
+
# in the context in which they are intended to be used.
|
103
115
|
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
116
|
+
# #arrows is meant to be used from view contexts with limited functionality
|
117
|
+
# such as an HTML email. As such, the translations you specify should be
|
118
|
+
# simple enough, like HTML character entities, to work within said view
|
119
|
+
# context.
|
108
120
|
#
|
109
|
-
#
|
110
|
-
#
|
121
|
+
# #icons is meant to be used from full-featured view contexts. As such,
|
122
|
+
# #icons is the one to use to generate HTML tags.
|
111
123
|
def arrow
|
112
|
-
|
113
|
-
|
114
|
-
t 'comparison.arrows.positive_html', default: ARROWS[:up]
|
115
|
-
when negative?
|
116
|
-
t 'comparison.arrows.negative_html', default: ARROWS[:down]
|
117
|
-
else
|
118
|
-
t 'comparison.arrows.nochange_html', default: ARROWS[:none]
|
119
|
-
end
|
124
|
+
key, = expand_i18n_keys 'arrows', suffix: '_html'
|
125
|
+
t key, default: ARROWS[description.to_sym]
|
120
126
|
end
|
121
127
|
|
122
128
|
##
|
123
|
-
# Returns the I18n translation for `comparison.
|
129
|
+
# Returns the I18n translation for `comparison.dom_classes`.
|
124
130
|
#
|
125
131
|
# Use these translations to specify CSS classes for tags that contain
|
126
132
|
# comparison data. For example:
|
127
133
|
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
134
|
+
# en:
|
135
|
+
# comparison:
|
136
|
+
# dom_classes:
|
137
|
+
# positive: 'comparison positive'
|
138
|
+
# negative: 'comparison negative'
|
139
|
+
# nochange: 'comparison nochange'
|
140
|
+
#
|
141
|
+
# .comparison.positive {
|
142
|
+
# color: #3c763d;
|
143
|
+
# background-color: #dff0d8;
|
144
|
+
# }
|
145
|
+
# .comparison.negative {
|
146
|
+
# color: #a94442;
|
147
|
+
# background-color: #f2dede;
|
148
|
+
# }
|
149
|
+
# .comparison.nochange {
|
150
|
+
# color: #777777;
|
151
|
+
# }
|
152
|
+
#
|
153
|
+
# content_tag :span, cmp.difference, class: cmp.dom_classes
|
154
|
+
# # => "<span class=\"comparison positive\">+10%</span>"
|
155
|
+
#
|
156
|
+
# If you need to work with inline styles instead of CSS classes, see the
|
157
|
+
# `#inline_style` method.
|
158
|
+
def dom_classes
|
159
|
+
key, *deprecated_keys = expand_i18n_keys(%w[dom_classes classes])
|
160
|
+
t key, default: deprecated_keys
|
161
|
+
end
|
162
|
+
|
148
163
|
def classes
|
149
|
-
|
150
|
-
|
151
|
-
t 'comparison.classes.positive'
|
152
|
-
when negative?
|
153
|
-
t 'comparison.classes.negative'
|
154
|
-
else
|
155
|
-
t 'comparison.classes.nochange'
|
156
|
-
end
|
164
|
+
Kernel.warn "DEPRECATION WARNING: use #dom_classes instead of #classes (called from #{caller(3..3).first})"
|
165
|
+
dom_classes
|
157
166
|
end
|
158
167
|
|
159
168
|
##
|
160
|
-
# Returns the I18n translation for `comparison.
|
169
|
+
# Returns the I18n translation for `comparison.style`.
|
161
170
|
#
|
162
|
-
# Use these translations to specify
|
171
|
+
# Use these translations to specify inline CSS style rules to be used when
|
163
172
|
# formatting comparison data. For example:
|
164
173
|
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
174
|
+
# en:
|
175
|
+
# comparison:
|
176
|
+
# inline_style:
|
177
|
+
# positive: 'color: #3c763d; background-color: #dff0d8;'
|
178
|
+
# negative: 'color: #a94442; background-color: #f2dede;'
|
179
|
+
# nochange: 'color: #777777;'
|
180
|
+
#
|
181
|
+
# content_tag :span, cmp.difference, style: cmp.inline_style
|
182
|
+
# # => "<span style=\"color: #3c763d; background-color: #dff0d8;\">+10%</span>"
|
171
183
|
#
|
172
|
-
#
|
184
|
+
# In general, it's probably preferable to use `#dom_classes` in conjunction
|
185
|
+
# with CSS style rules defined separate CSS files, but this isn't always
|
186
|
+
# possible.
|
173
187
|
#
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
188
|
+
def inline_style
|
189
|
+
key, *deprecated_keys = expand_i18n_keys(%w[inline_style style css])
|
190
|
+
t key, default: [*deprecated_keys, '']
|
191
|
+
end
|
192
|
+
|
193
|
+
alias style inline_style
|
194
|
+
|
178
195
|
def css
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
196
|
+
Kernel.warn "DEPRECATION WARNING: use #inline_style instead of #css (called from #{caller(3..3).first})"
|
197
|
+
inline_style
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# Returns a string description of the direction of change. Possible
|
202
|
+
# descriptions are "positive", "negative", and "nochange".
|
203
|
+
def description
|
204
|
+
if positive?
|
205
|
+
'positive'
|
206
|
+
elsif negative?
|
207
|
+
'negative'
|
184
208
|
else
|
185
|
-
|
209
|
+
'nochange'
|
186
210
|
end
|
187
211
|
end
|
188
212
|
|
189
|
-
|
213
|
+
private
|
214
|
+
|
215
|
+
def number_to_percentage(value, **options)
|
216
|
+
options = { delimiter: ',', precision: 0, **options }
|
217
|
+
ActiveSupport::NumberHelper.number_to_percentage value, options
|
218
|
+
end
|
219
|
+
|
220
|
+
def number_to_currency(*args)
|
221
|
+
ActiveSupport::NumberHelper.number_to_currency(*args)
|
222
|
+
end
|
223
|
+
|
224
|
+
def expand_i18n_keys(names, suffix: nil)
|
225
|
+
Array(names).map { |name| :"comparison.#{name}.#{description}#{suffix}" }
|
226
|
+
end
|
190
227
|
end
|
191
228
|
end
|
data/lib/comparison/version.rb
CHANGED
data/lib/comparison.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comparison
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Parker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: minitest-focus
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -45,7 +45,35 @@ dependencies:
|
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
48
|
+
name: mocha
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rubocop
|
49
77
|
requirement: !ruby/object:Gem::Requirement
|
50
78
|
requirements:
|
51
79
|
- - ">="
|
@@ -75,7 +103,6 @@ files:
|
|
75
103
|
- lib/comparison/engine.rb
|
76
104
|
- lib/comparison/presenter.rb
|
77
105
|
- lib/comparison/version.rb
|
78
|
-
- lib/tasks/comparison_tasks.rake
|
79
106
|
homepage: https://github.com/jparker/comparison
|
80
107
|
licenses:
|
81
108
|
- MIT
|
@@ -88,15 +115,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
115
|
requirements:
|
89
116
|
- - ">="
|
90
117
|
- !ruby/object:Gem::Version
|
91
|
-
version: '
|
118
|
+
version: '2.7'
|
92
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
120
|
requirements:
|
94
121
|
- - ">="
|
95
122
|
- !ruby/object:Gem::Version
|
96
123
|
version: '0'
|
97
124
|
requirements: []
|
98
|
-
|
99
|
-
rubygems_version: 2.6.6
|
125
|
+
rubygems_version: 3.3.12
|
100
126
|
signing_key:
|
101
127
|
specification_version: 4
|
102
128
|
summary: Helpers for displaying details of comparing two numbers.
|