comparison 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: baea273a06f0a0abb98d762a042fa9839ffd1c0e
4
- data.tar.gz: 0b801fed057465fd098120a14e9cf931851c9198
2
+ SHA256:
3
+ metadata.gz: b1789756840509d6bfc178a7fce308b15e8097b8321362f36d2ceba7970862c0
4
+ data.tar.gz: cfd348911fee4ae30d3857985017d290dadb3f89af2e826b086b84a0c46c180e
5
5
  SHA512:
6
- metadata.gz: 6511bc82b1f7fde3f6c13f4d2ae1ad56622d08f3ec65a15a670c8fa370684996ea395d36455763f4894460ce8ca1920ff2f37ff7db4f473ef9305944e27fc3b7
7
- data.tar.gz: ccd9104172486a78800166080e0ba1268008b41bc36ae34524bc491f3576fc30562446110a48b05933b6cffbe051b33f431b47b6be7ba2b8e6dc45ffff950c4b
6
+ metadata.gz: ce42dd5c8370367bdca1c66fa40277515b80c352c6db76222b4f0d5a9f8e40581a56d04396075802f86bbd4a41043167c719bb4d9406272e9d0ddd67792e43d9
7
+ data.tar.gz: f1d697351b7f16e05fc3b5394c5f22c70fac9f5e6db17cf5898cdd4adef82ecb8e4d83b3bbfd5fac113daa2f558f8ce83d3e5417b29c288d8c821cb4da325d70
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2016 John Parker
1
+ Copyright 2018 John Parker
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,40 +1,45 @@
1
- # Comparison
1
+ # [![Gem Version](https://badge.fury.io/rb/comparison.svg)](https://badge.fury.io/rb/comparison)
2
2
 
3
- I have often found myself implementing reporting features that compare two
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
- I've tackled this task enough times and in enough applications that I felt it
12
- would simplify my life to extract and package the code for future re-use.
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
- The library has three components: the Comparison class for performing the
16
- actual math, the Presenter class for decorating the Comparison with
17
- view-friendly output, and a helper module for using the Presenter with the
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
- Comparison presenter to a block.
20
+ The `#compare` helper takes the two numbers to be compared and yields the
21
+ presenter to a block.
22
22
 
23
- `#difference` provides the absolute difference between the two numbers,
24
- literally `m - n`.
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 `ActionView::Helpers::NumberHelper#number_to_percentage` to
28
- format the percentage. Options are passed through to that method.
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 m, n do |cmp| %>
35
- <td><%= number_to_currency m %></td>
36
- <td><%= number_to_currency n %></td>
37
- <td><%= cmp.difference %></td>
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
- classes:
69
+ dom_classes:
64
70
  positive: 'comparison positive'
65
71
  negative: 'comparison negative'
66
72
  nochange: 'comparison nochange'
67
- css:
68
- positive_html: 'color: #3c763d; background-color: #dff0d8;'
69
- negative_html: 'color: #a94442; background-color: #f2dede;'
70
- nochange_html: 'color: #777777;'
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: '&uarr;'
77
83
  negative_html: '&darr;'
78
84
  nochange_html: ''
85
+ infinity_html: '&infin;'
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
- Or install it yourself as:
94
- ```bash
95
- $ gem install comparison
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 +m+ and +n+.
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(m, n)
8
- comparison = Presenter.new Comparator.new m, n
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
@@ -1,17 +1,20 @@
1
1
  en:
2
2
  comparison:
3
- # classes:
3
+ # dom_classes:
4
4
  # positive: 'comparison positive'
5
5
  # negative: 'comparison negative'
6
6
  # nochange: 'comparison nochange'
7
- css:
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: '&uarr;'
17
20
  negative_html: '&darr;'
@@ -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, +m+ and +n+.
10
- def initialize(m, n)
11
- @m = m.to_d
12
- @n = n.to_d
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
- attr_reader :m, :n
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 +@m+ and +@n+.
28
+ # Returns the difference between +@value+ and +@other+.
29
+ def difference
30
+ value - other
31
+ end
32
+
21
33
  def absolute
22
- @absolute ||= m - n
34
+ Kernel.warn "DEPRECATION WARNING: use #difference instead of #absolute (called from #{caller(1..1).first})"
35
+ difference
23
36
  end
24
37
 
25
- alias_method :difference, :absolute
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 percentage difference of +@m+ to +@n+.
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
- @relative ||= if n.negative?
31
- (1 - m / n) * 100
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
- alias_method :percentage, :relative
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
@@ -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
@@ -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 = { up: '&uarr;', down: '&darr;', none: '' }
12
-
13
- # TODO: This shouldn't necessarily return a currency representation.
12
+ ARROWS = { positive: '&uarr;', negative: '&darr;', nochange: '' }.freeze
14
13
 
15
14
  ##
16
- # Returns `Comparator#absolute` presented as currency.
17
- def difference(**options)
15
+ # Returns Comparator#difference formatted as currency.
16
+ def difference_as_currency(**options)
18
17
  if positive?
19
- number_to_currency absolute, format: '+%u%n', **options
18
+ number_to_currency __getobj__.difference, format: '+%u%n', **options
20
19
  else
21
- number_to_currency absolute, **options
20
+ number_to_currency __getobj__.difference, **options
22
21
  end
23
22
  end
24
23
 
25
24
  ##
26
- # Returns `Comparator#relative` formatted as a percentage.
27
- #
28
- # If the relative percentage evaluates to Infinity or -Infinity, +nil+ is
29
- # returned. If it evaluates to NaN, 0 is returned.
30
- def percentage(delimiter: ',', precision: 0, **options)
31
- case
32
- when nan? || zero?
33
- number_to_percentage 0, precision: precision, **options
34
- when infinite?
35
- # TODO: Return nil, or lookup an optional representation in I18n?
36
- nil
37
- when positive?
38
- number_to_percentage relative, delimiter: delimiter,
39
- precision: precision, format: '+%n%', **options
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 relative, delimiter: delimiter,
42
- precision: precision, **options
51
+ number_to_percentage __getobj__.percentage, **options
43
52
  end
44
53
  end
45
54
 
46
- alias_method :change, :percentage
47
- deprecate :change
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
- delegate %i[number_to_currency number_to_percentage] => :'ActiveSupport::NumberHelper'
65
+ number_to_percentage __getobj__.percentage.abs, **options
66
+ end
50
67
 
51
68
  ##
52
- # Returns the I18n translation for `comparison.icons`. (See also `#arrow`.)
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
- # en:
65
- # comparison:
66
- # icons:
67
- # positive_html: '<span class="glyphicon glyphicon-arrow-up"></span>'
68
- # negative_html: '<span class="glyphicon glyphicon-arrow-down"></span>'
69
- # nochange_html: '<span class="glyphicon glyphicon-minus"></span>'
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
- case
72
- when positive?
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 `#icon`.)
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
- # en:
90
- # comparison:
91
- # arrows:
92
- # positive_html: '&uarr;'
93
- # negative_html: '&darr;'
94
- # nochange_html: ''
101
+ # en:
102
+ # comparison:
103
+ # arrows:
104
+ # positive_html: '&uarr;'
105
+ # negative_html: '&darr;'
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
- # `#arrows` and its sister method `#icon` perform roughly identical tasks
101
- # with roughly identical intentions. The difference between the two methods
102
- # is in the context in which they are intended to be used.
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
- # `#arrows` is meant to be used from view contexts with limited
105
- # functionality such as an HTML email. As such, the translations you
106
- # specify should be simple enough, like HTML character entities, to work
107
- # within said view context.
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
- # `#icons` is meant to be used from full-featured view contexts. As such,
110
- # `#icons` is the one to use to generate HTML tags.
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
- case
113
- when positive?
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.classes`. (See also `#css`.)
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
- # en:
129
- # comparison:
130
- # classes:
131
- # positive: 'comparison positive'
132
- # negative: 'comparison negative'
133
- # nochange: 'comparison nochange'
134
- #
135
- # .comparison.positive {
136
- # color: #3c763d;
137
- # background-color: #dff0d8;
138
- # }
139
- # .comparison.negative {
140
- # color: #a94442;
141
- # background-color: #f2dede;
142
- # }
143
- # .comparison.nochange {
144
- # color: #777777;
145
- # }
146
- #
147
- # content_tag cmp.difference, class: cmp.classes
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
- case
150
- when positive?
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.css`. (See also `#classes`.)
169
+ # Returns the I18n translation for `comparison.style`.
161
170
  #
162
- # Use these translations to specify raw CSS style rules to be used when
171
+ # Use these translations to specify inline CSS style rules to be used when
163
172
  # formatting comparison data. For example:
164
173
  #
165
- # en:
166
- # comparison:
167
- # css:
168
- # positive: 'color: #3c763d; background-color: #dff0d8;'
169
- # negative: 'color: #a94442; background-color: #f2dede;'
170
- # nochange: 'color: #777777;'
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
- # content_tag cmp.difference, style: cmp.css
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
- # `#css` and its sister method `#classes` perform very similar tasks. Use
175
- # `#css` when you need to embed the CSS style rules in an HTML tag using
176
- # the style attribute. Use `#classes` when you want have the CSS style
177
- # rules defined in a class and want to add that class to the HTML tag.
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
- case
180
- when positive?
181
- t 'comparison.css.positive', default: ''
182
- when negative?
183
- t 'comparison.css.negative', default: ''
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
- t 'comparison.css.nochange', default: ''
209
+ 'nochange'
186
210
  end
187
211
  end
188
212
 
189
- alias_method :style, :css
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
@@ -1,3 +1,5 @@
1
+ # frozen-string-literal: true
2
+
1
3
  module Comparison
2
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
3
5
  end
data/lib/comparison.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen-string-literal: true
2
+
1
3
  require 'comparison/engine'
2
4
  require 'comparison/comparator'
3
5
  require 'comparison/presenter'
4
6
 
5
- module Comparison
7
+ module Comparison # :nodoc:
6
8
  end
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.1.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: 2016-09-28 00:00:00.000000000 Z
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: '6.0'
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: '6.0'
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: pry-rails
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: '0'
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
- rubyforge_project:
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.
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :comparison do
3
- # # Task goes here
4
- # end