measured 2.4.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +31 -0
  3. data/CHANGELOG.md +32 -0
  4. data/README.md +12 -10
  5. data/Rakefile +18 -0
  6. data/cache/.keep +0 -0
  7. data/cache/length.json +2553 -0
  8. data/cache/volume.json +4163 -0
  9. data/cache/weight.json +3195 -0
  10. data/dev.yml +1 -1
  11. data/gemfiles/{activesupport-4.2.gemfile → activesupport-5.2.gemfile} +1 -1
  12. data/gemfiles/activesupport-6.0.gemfile +5 -0
  13. data/gemfiles/activesupport-6.1.gemfile +5 -0
  14. data/lib/measured.rb +1 -0
  15. data/lib/measured/arithmetic.rb +1 -0
  16. data/lib/measured/base.rb +11 -5
  17. data/lib/measured/cache/json.rb +49 -0
  18. data/lib/measured/cache/json_writer.rb +9 -0
  19. data/lib/measured/cache/null.rb +16 -0
  20. data/lib/measured/conversion_table_builder.rb +42 -12
  21. data/lib/measured/cycle_detected.rb +11 -0
  22. data/lib/measured/measurable.rb +18 -21
  23. data/lib/measured/missing_conversion_path.rb +12 -0
  24. data/lib/measured/parser.rb +2 -1
  25. data/lib/measured/unit.rb +16 -26
  26. data/lib/measured/unit_already_added.rb +11 -0
  27. data/lib/measured/unit_error.rb +4 -0
  28. data/lib/measured/unit_system.rb +26 -22
  29. data/lib/measured/unit_system_builder.rb +11 -4
  30. data/lib/measured/units/length.rb +3 -0
  31. data/lib/measured/units/volume.rb +4 -1
  32. data/lib/measured/units/weight.rb +3 -0
  33. data/lib/measured/version.rb +2 -1
  34. data/measured.gemspec +10 -2
  35. data/test/arithmetic_test.rb +1 -0
  36. data/test/cache/json_test.rb +43 -0
  37. data/test/cache/json_writer_test.rb +23 -0
  38. data/test/cache/null_test.rb +20 -0
  39. data/test/cache_consistency_test.rb +19 -0
  40. data/test/conversion_table_builder_test.rb +29 -0
  41. data/test/measurable_test.rb +5 -5
  42. data/test/parser_test.rb +1 -0
  43. data/test/support/always_true_cache.rb +14 -0
  44. data/test/support/fake_system.rb +1 -0
  45. data/test/support/subclasses.rb +7 -0
  46. data/test/test_helper.rb +5 -3
  47. data/test/unit_error_test.rb +1 -0
  48. data/test/unit_system_builder_test.rb +49 -3
  49. data/test/unit_system_test.rb +15 -0
  50. data/test/unit_test.rb +1 -0
  51. data/test/units/length_test.rb +5 -4
  52. data/test/units/volume_test.rb +6 -5
  53. data/test/units/weight_test.rb +1 -0
  54. metadata +39 -13
  55. data/.travis.yml +0 -20
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
@@ -11,15 +12,17 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
11
12
  end
12
13
 
13
14
  test "#unit cannot add duplicate unit names" do
14
- assert_raises Measured::UnitError do
15
+ error = assert_raises Measured::UnitAlreadyAdded do
15
16
  Measured.build do
16
17
  unit :m
17
18
  unit :in, aliases: [:inch], value: "0.0254 m"
18
19
  unit :in, aliases: [:thing], value: "123 m"
19
20
  end
20
21
  end
22
+ assert_equal("in", error.unit_name)
23
+ assert_equal("Unit in has already been added.", error.message)
21
24
 
22
- assert_raises Measured::UnitError do
25
+ assert_raises Measured::UnitAlreadyAdded do
23
26
  Measured.build do
24
27
  unit :m
25
28
  unit :in, aliases: [:inch], value: "0.0254 m"
@@ -28,6 +31,19 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
28
31
  end
29
32
  end
30
33
 
34
+ test "#unit raises when cannot find conversion path" do
35
+ error = assert_raises Measured::MissingConversionPath do
36
+ Measured.build do
37
+ unit :m
38
+ unit :in, value: "0.0254 m"
39
+ unit :pallets, value: "5 cases"
40
+ end
41
+ end
42
+ assert_equal("pallets", error.from)
43
+ assert_equal("m", error.to)
44
+ assert_equal("Cannot find conversion path from pallets to m.", error.message)
45
+ end
46
+
31
47
  test "#unit is case sensitive" do
32
48
  measurable = Measured.build do
33
49
  unit :normal
@@ -38,6 +54,16 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
38
54
  assert_equal 'BOLD', measurable.unit_system.unit_for!(:BOLD).name
39
55
  end
40
56
 
57
+ test "#unit traverses aliases" do
58
+ measurable = Measured.build do
59
+ unit :piece, aliases: [:pieces]
60
+ unit :dozen, aliases: [:dz], value: "12 pieces"
61
+ end
62
+
63
+ assert_equal 12, measurable.unit_system.units.last.conversion_amount
64
+ assert_equal "piece", measurable.unit_system.units.last.conversion_unit
65
+ end
66
+
41
67
  test "#si_unit adds 21 new units" do
42
68
  measurable = Measured.build do
43
69
  unit :ft
@@ -54,7 +80,7 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
54
80
  end
55
81
 
56
82
  prefixes = Measured::UnitSystemBuilder::SI_PREFIXES.map { |short, _, _| "#{short}ft" }
57
- prefixes += %w(in ft)
83
+ prefixes += %w(in ft)
58
84
  assert_equal prefixes.sort, measurable.unit_names
59
85
  end
60
86
 
@@ -109,4 +135,24 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
109
135
  assert_equal (2240), measurable.unit_system.unit_for!(:long_ton).conversion_amount
110
136
  assert_equal "lb", measurable.unit_system.unit_for!(:long_ton).conversion_unit
111
137
  end
138
+
139
+ test "#cache sets no cache by default" do
140
+ measurable = Measured.build do
141
+ unit :m
142
+ unit :km, value: "1000 m"
143
+ end
144
+
145
+ refute_predicate measurable.unit_system, :cached?
146
+ end
147
+
148
+ test "#cache sets the class and args" do
149
+ measurable = Measured.build do
150
+ unit :m
151
+ unit :km, value: "1000 m"
152
+
153
+ cache AlwaysTrueCache
154
+ end
155
+
156
+ assert_predicate measurable.unit_system, :cached?
157
+ end
112
158
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::UnitSystemTest < ActiveSupport::TestCase
@@ -99,4 +100,18 @@ class Measured::UnitSystemTest < ActiveSupport::TestCase
99
100
  test "#convert handles the same unit" do
100
101
  assert_equal 2, @conversion.convert(2, from: @unit_in, to: @unit_in)
101
102
  end
103
+
104
+ test "#update_cache calls the builder" do
105
+ Measured::ConversionTableBuilder.any_instance.expects(:update_cache)
106
+ @conversion.update_cache
107
+ end
108
+
109
+ test "#cached? calls the builder and is false" do
110
+ refute_predicate @conversion, :cached?
111
+ end
112
+
113
+ test "#cached? calls the builder and is true" do
114
+ conversion = Measured::UnitSystem.new([@unit_m, @unit_in, @unit_ft], cache: { class: AlwaysTrueCache })
115
+ assert_predicate conversion, :cached?
116
+ end
102
117
  end
data/test/unit_test.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::UnitTest < ActiveSupport::TestCase
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::LengthTest < ActiveSupport::TestCase
@@ -142,7 +143,7 @@ class Measured::LengthTest < ActiveSupport::TestCase
142
143
  end
143
144
 
144
145
  test ".convert_to from km to mi" do
145
- assert_conversion Measured::Length, "2000 km", "0.1242742384475E4 mi"
146
+ assert_conversion Measured::Length, "2000 km", "0.1242742384475E4 mi"
146
147
  end
147
148
 
148
149
  test ".convert_to from km to mm" do
@@ -150,7 +151,7 @@ class Measured::LengthTest < ActiveSupport::TestCase
150
151
  end
151
152
 
152
153
  test ".convert_to from km to yd" do
153
- assert_conversion Measured::Length, "2000 km", "0.218722659667542E7 yd"
154
+ assert_conversion Measured::Length, "2000 km", "0.218722659667542E7 yd"
154
155
  end
155
156
 
156
157
  test ".convert_to from m to cm" do
@@ -206,7 +207,7 @@ class Measured::LengthTest < ActiveSupport::TestCase
206
207
  end
207
208
 
208
209
  test ".convert_to from mi to mi" do
209
- assert_exact_conversion Measured::Length, "2000 mi", "2000 mi"
210
+ assert_exact_conversion Measured::Length, "2000 mi", "2000 mi"
210
211
  end
211
212
 
212
213
  test ".convert_to from mi to mm" do
@@ -214,7 +215,7 @@ class Measured::LengthTest < ActiveSupport::TestCase
214
215
  end
215
216
 
216
217
  test ".convert_to from mi to yd" do
217
- assert_exact_conversion Measured::Length, "2000 mi", "3520000 yd"
218
+ assert_exact_conversion Measured::Length, "2000 mi", "3520000 yd"
218
219
  end
219
220
 
220
221
  test ".convert_to from mm to cm" do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::VolumeTest < ActiveSupport::TestCase
@@ -68,7 +69,7 @@ class Measured::VolumeTest < ActiveSupport::TestCase
68
69
  test ".unit_names should be the list of base unit names" do
69
70
  expected_units = %w(l m3 ft3 in3 gal us_gal qt us_qt pt us_pt oz us_oz)
70
71
  expected_units += Measured::UnitSystemBuilder::SI_PREFIXES.map { |short, _, _| "#{short}l" }
71
- assert_equal expected_units.sort, Measured::Volume.unit_names
72
+ assert_equal expected_units.sort, Measured::Volume.unit_names
72
73
  end
73
74
 
74
75
  test ".name" do
@@ -95,7 +96,7 @@ class Measured::VolumeTest < ActiveSupport::TestCase
95
96
  test ".convert_to from gal to gal" do
96
97
  assert_conversion Measured::Volume, "2000 gal", "2000 gal"
97
98
  end
98
-
99
+
99
100
  test ".convert_to from us_gal to us_gal" do
100
101
  assert_conversion Measured::Volume, "2000 us_gal", "2000 us_gal"
101
102
  end
@@ -123,7 +124,7 @@ class Measured::VolumeTest < ActiveSupport::TestCase
123
124
  test ".convert_to from us_oz to us_oz" do
124
125
  assert_conversion Measured::Volume, "2000 us_oz", "2000 us_oz"
125
126
  end
126
-
127
+
127
128
  test ".convert_to from ml to m3" do
128
129
  assert_conversion Measured::Volume, "2000 ml", "0.002 m3"
129
130
  end
@@ -303,7 +304,7 @@ class Measured::VolumeTest < ActiveSupport::TestCase
303
304
  test ".convert_to from gal to us_oz" do
304
305
  assert_conversion Measured::Volume, "2 gal", "307.4431809292 us_oz"
305
306
  end
306
-
307
+
307
308
  test ".convert_to from us_gal to qt" do
308
309
  assert_conversion Measured::Volume, "2 us_gal", "6.661393477 qt"
309
310
  end
@@ -387,4 +388,4 @@ class Measured::VolumeTest < ActiveSupport::TestCase
387
388
  test ".convert_to from oz to us_oz" do
388
389
  assert_conversion Measured::Volume, "2 oz", "1.9215207864 us_oz"
389
390
  end
390
- end
391
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "test_helper"
2
3
 
3
4
  class Measured::WeightTest < ActiveSupport::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-01-24 00:00:00.000000000 Z
13
+ date: 2021-05-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: '4.2'
21
+ version: '5.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: '4.2'
28
+ version: '5.2'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: rake
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -72,16 +72,16 @@ dependencies:
72
72
  name: mocha
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - ">"
75
+ - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 1.1.0
77
+ version: 1.4.0
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ">"
82
+ - - ">="
83
83
  - !ruby/object:Gem::Version
84
- version: 1.1.0
84
+ version: 1.4.0
85
85
  - !ruby/object:Gem::Dependency
86
86
  name: pry
87
87
  requirement: !ruby/object:Gem::Requirement
@@ -104,21 +104,35 @@ executables: []
104
104
  extensions: []
105
105
  extra_rdoc_files: []
106
106
  files:
107
+ - ".github/workflows/ci.yml"
107
108
  - ".gitignore"
108
- - ".travis.yml"
109
+ - CHANGELOG.md
109
110
  - Gemfile
110
111
  - LICENSE
111
112
  - README.md
112
113
  - Rakefile
114
+ - cache/.keep
115
+ - cache/length.json
116
+ - cache/volume.json
117
+ - cache/weight.json
113
118
  - dev.yml
114
- - gemfiles/activesupport-4.2.gemfile
119
+ - gemfiles/activesupport-5.2.gemfile
120
+ - gemfiles/activesupport-6.0.gemfile
121
+ - gemfiles/activesupport-6.1.gemfile
115
122
  - lib/measured.rb
116
123
  - lib/measured/arithmetic.rb
117
124
  - lib/measured/base.rb
125
+ - lib/measured/cache/json.rb
126
+ - lib/measured/cache/json_writer.rb
127
+ - lib/measured/cache/null.rb
118
128
  - lib/measured/conversion_table_builder.rb
129
+ - lib/measured/cycle_detected.rb
119
130
  - lib/measured/measurable.rb
131
+ - lib/measured/missing_conversion_path.rb
120
132
  - lib/measured/parser.rb
121
133
  - lib/measured/unit.rb
134
+ - lib/measured/unit_already_added.rb
135
+ - lib/measured/unit_error.rb
122
136
  - lib/measured/unit_system.rb
123
137
  - lib/measured/unit_system_builder.rb
124
138
  - lib/measured/units/length.rb
@@ -128,10 +142,16 @@ files:
128
142
  - measured.gemspec
129
143
  - shipit.rubygems.yml
130
144
  - test/arithmetic_test.rb
145
+ - test/cache/json_test.rb
146
+ - test/cache/json_writer_test.rb
147
+ - test/cache/null_test.rb
148
+ - test/cache_consistency_test.rb
131
149
  - test/conversion_table_builder_test.rb
132
150
  - test/measurable_test.rb
133
151
  - test/parser_test.rb
152
+ - test/support/always_true_cache.rb
134
153
  - test/support/fake_system.rb
154
+ - test/support/subclasses.rb
135
155
  - test/test_helper.rb
136
156
  - test/unit_error_test.rb
137
157
  - test/unit_system_builder_test.rb
@@ -143,7 +163,8 @@ files:
143
163
  homepage: https://github.com/Shopify/measured
144
164
  licenses:
145
165
  - MIT
146
- metadata: {}
166
+ metadata:
167
+ allowed_push_host: https://rubygems.org
147
168
  post_install_message:
148
169
  rdoc_options: []
149
170
  require_paths:
@@ -159,17 +180,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
180
  - !ruby/object:Gem::Version
160
181
  version: '0'
161
182
  requirements: []
162
- rubyforge_project:
163
- rubygems_version: 2.6.14
183
+ rubygems_version: 3.0.3
164
184
  signing_key:
165
185
  specification_version: 4
166
186
  summary: Encapsulate measurements with their units in Ruby
167
187
  test_files:
168
188
  - test/arithmetic_test.rb
189
+ - test/cache/json_test.rb
190
+ - test/cache/json_writer_test.rb
191
+ - test/cache/null_test.rb
192
+ - test/cache_consistency_test.rb
169
193
  - test/conversion_table_builder_test.rb
170
194
  - test/measurable_test.rb
171
195
  - test/parser_test.rb
196
+ - test/support/always_true_cache.rb
172
197
  - test/support/fake_system.rb
198
+ - test/support/subclasses.rb
173
199
  - test/test_helper.rb
174
200
  - test/unit_error_test.rb
175
201
  - test/unit_system_builder_test.rb
data/.travis.yml DELETED
@@ -1,20 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- cache: bundler
4
- rvm:
5
- - 2.1.8
6
- - 2.2.4
7
- - 2.3.1
8
- - 2.4.0
9
- - 2.5.0
10
- gemfile:
11
- - Gemfile
12
- - gemfiles/activesupport-4.2.gemfile
13
- before_script:
14
- - gem update --system
15
- matrix:
16
- exclude:
17
- - gemfile: Gemfile
18
- rvm: 2.1.8
19
- - gemfile: gemfiles/activesupport-4.2.gemfile
20
- rvm: 2.4.0