ruby-measurement 1.2.3 → 1.3.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: 31687bfdd62b83ca04ffba2a8d1c106d767f8ac4
4
- data.tar.gz: 5eb61379bb4dc5b1eaf051c96cf1c55353bb4b0b
2
+ SHA256:
3
+ metadata.gz: e9ad871d0be538a1fe0b81b6f7e30d88c843a6d8c26b0682dd4ce68289ebfdf8
4
+ data.tar.gz: fad2d4f562bbca29e685fd64f36ce7473b2b470479daaeb3a02bd707d409c198
5
5
  SHA512:
6
- metadata.gz: 25e76a22f35f3e5af25a1f79bf0f396eeba2273da6d6d8011dc11f34e02e868b374227aa1f95ee6a541ddaf1b98d6408ddb30f6898a49c43d12852128e8ba79f
7
- data.tar.gz: e7f70d5166d49109632bbc8c8a0b8cf3c706fea85c5dc4b3abaddebc0af46581468394ffdae920fdea235bad66dcf1cc0d9b6aa9afe6ab52988ac593454d8d4c
6
+ metadata.gz: 9476515fecd08634e5f194ec1333e8d23ed7978046c99f72aa1a120b462961b711c2c49948bd4927eacfea9add0f378dfd1da865aded3f0f4ec9cff345a0a562
7
+ data.tar.gz: b14f4807067541c3d498f021e0cf79108a768358b8969258ee2452988cb70a3cc06a1cec83bfc6236ecf399f69459845dbb0b98e9cdf9226396cdd28f173d9e5
@@ -0,0 +1,30 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby:
16
+ - head
17
+ - '3.0'
18
+ - '2.7'
19
+ - '2.6'
20
+ - '2.5'
21
+ continue-on-error: ${{ matrix.ruby == 'head' }}
22
+ name: Ruby ${{ matrix.ruby }}
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby }}
28
+ bundler-cache: true
29
+ - run: |
30
+ bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## ruby-measurement 1.3.0 (Aug 1, 2021)
2
+
3
+ * Added `Comparable` module to `Measurement`, supporting `<` and `>` operators. *Edmund Lee*
4
+
1
5
  ## ruby-measurement 1.2.3 (Mar 27, 2016)
2
6
 
3
7
  * Updated parsing to accept UTF-8 fractions (½, ⅓, etc.). *Matt Huggins*
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Measurement
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/mhuggins/ruby-measurement.png)](http://travis-ci.org/mhuggins/ruby-measurement)
3
+ [![Build Status](https://github.com/mhuggins/ruby-measurement/actions/workflows/CI.yml/badge.svg)](https://github.com/mhuggins/ruby-measurement/actions/workflows/CI.yml)
4
4
  [![Code Climate](https://codeclimate.com/github/mhuggins/ruby-measurement.png)](https://codeclimate.com/github/mhuggins/ruby-measurement)
5
5
 
6
6
  [ruby-measurement](https://github.com/mhuggins/ruby-measurement) is a simple
@@ -108,10 +108,10 @@ To include just one of these systems of measure, require the gem in per the
108
108
  following.
109
109
 
110
110
  require 'ruby-measurement/measurement'
111
-
111
+
112
112
  # Metric units/conversions
113
113
  require 'ruby-measurement/definitions/metric'
114
-
114
+
115
115
  # U.S. customary units/conversions
116
116
  require 'ruby-measurement/definitions/us_customary'
117
117
 
@@ -119,13 +119,13 @@ Additionally, specific categories of units of measure can be included per
119
119
  system.
120
120
 
121
121
  require 'ruby-measurement/measurement'
122
-
122
+
123
123
  # Metric units/conversions
124
124
  require 'ruby-measurement/definitions/metric/area'
125
125
  require 'ruby-measurement/definitions/metric/length'
126
126
  require 'ruby-measurement/definitions/metric/volume'
127
127
  require 'ruby-measurement/definitions/metric/weight'
128
-
128
+
129
129
  # U.S. customary units/conversions
130
130
  require 'ruby-measurement/definitions/us_customary/area'
131
131
  require 'ruby-measurement/definitions/us_customary/length'
@@ -142,13 +142,13 @@ default.
142
142
  unit.convert_to(:week) { |value| value / 7.0 }
143
143
  unit.convert_to(:year) { |value| value / 365.0 }
144
144
  end
145
-
145
+
146
146
  Measurement.define(:wk) do |unit|
147
147
  unit.alias :week, :weeks
148
148
  unit.convert_to(:day) { |value| value * 7.0 }
149
149
  unit.convert_to(:year) { |value| value / 52.0 }
150
150
  end
151
-
151
+
152
152
  Measurement.define(:yr) do |unit|
153
153
  unit.alias :year, :years
154
154
  unit.convert_to(:day) { |value| value * 365.0 }
@@ -166,7 +166,7 @@ that will be used when displaying a measurement.
166
166
 
167
167
  List all keys you can use as unit
168
168
 
169
- Measurement.names # => ['count','doz','dozen',...]
169
+ Measurement::Unit.names # => ['count','doz','dozen',...]
170
170
 
171
171
  ## Contributing
172
172
 
@@ -6,6 +6,6 @@ class String
6
6
  end
7
7
 
8
8
  def to_unit
9
- Measurement::Unit[self] or raise ArgumentError, "Invalid unit: #{self}"
9
+ Measurement::Unit[self] or raise ArgumentError, "Invalid unit: '#{self}'"
10
10
  end
11
11
  end
@@ -2,6 +2,6 @@ require 'ruby-measurement/unit'
2
2
 
3
3
  class Symbol
4
4
  def to_unit
5
- Measurement::Unit[self] or raise ArgumentError, "Invalid unit: #{self}"
5
+ Measurement::Unit[self] or raise ArgumentError, "Invalid unit: '#{self}'"
6
6
  end
7
7
  end
@@ -4,6 +4,8 @@ require 'ruby-measurement/unit'
4
4
  require 'ruby-measurement/version'
5
5
 
6
6
  class Measurement
7
+ include Comparable
8
+
7
9
  UNIT_REGEX = /([^\d\s\/].*)/.freeze
8
10
  SCIENTIFIC_NUMBER = /([+-]?\d*\.?\d+(?:[Ee][+-]?)?\d*)/.freeze
9
11
  SCIENTIFIC_REGEX = /\A#{SCIENTIFIC_NUMBER}\s*#{UNIT_REGEX}?\z/.freeze
@@ -34,8 +36,8 @@ class Measurement
34
36
  unit = unit_name
35
37
  unit = Unit[unit_name.to_s] if unit_name.kind_of?(Symbol) || unit_name.kind_of?(String)
36
38
 
37
- raise ArgumentError, "Invalid quantity: #{quantity}" unless quantity.kind_of?(Numeric)
38
- raise ArgumentError, "Invalid unit: #{unit_name}" unless unit.kind_of?(Unit)
39
+ raise ArgumentError, "Invalid quantity: '#{quantity}'" unless quantity.kind_of?(Numeric)
40
+ raise ArgumentError, "Invalid unit: '#{unit_name}'" unless unit.kind_of?(Unit)
39
41
 
40
42
  @quantity = quantity
41
43
  @unit = unit
@@ -77,8 +79,18 @@ class Measurement
77
79
  end
78
80
  end
79
81
 
80
- def ==(obj)
81
- obj.kind_of?(self.class) && quantity == obj.quantity && unit == obj.unit
82
+ def <=>(obj)
83
+ if !obj.is_a?(self.class) || obj.unit != self.unit
84
+ return nil
85
+ end
86
+
87
+ if quantity < obj.quantity
88
+ -1
89
+ elsif quantity > obj.quantity
90
+ 1
91
+ else
92
+ 0
93
+ end
82
94
  end
83
95
 
84
96
  def convert_to(unit_name)
@@ -123,8 +135,8 @@ class Measurement
123
135
 
124
136
  private
125
137
 
126
- def normalize(str)
127
- str.dup.tap do |str|
138
+ def normalize(string)
139
+ string.dup.tap do |str|
128
140
  if str =~ Regexp.new(/(#{RATIOS.keys.join('|')})/)
129
141
  RATIOS.each do |search, replace|
130
142
  str.gsub!(search) { " #{replace}" }
@@ -1,3 +1,3 @@
1
1
  class Measurement
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -12,12 +12,12 @@ Gem::Specification.new do |gem|
12
12
  gem.summary = 'Simple gem for calculating and converting measurements'
13
13
  gem.homepage = 'https://github.com/mhuggins/ruby-measurement'
14
14
  gem.license = 'MIT'
15
-
15
+
16
16
  gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_path = 'lib'
20
-
20
+
21
21
  gem.add_development_dependency 'rake'
22
22
  gem.add_development_dependency 'rspec', '~> 3.0'
23
23
  end
@@ -14,12 +14,12 @@ RSpec.describe String do
14
14
 
15
15
  describe 'with valid quantity and invalid unit' do
16
16
  subject { '3 people' }
17
- specify { expect { subject.to_measurement }.to raise_error }
17
+ specify { expect { subject.to_measurement }.to raise_error(ArgumentError, "Invalid unit: 'people'") }
18
18
  end
19
19
 
20
20
  describe 'with invalid input' do
21
21
  subject { 'foobar' }
22
- specify { expect { subject.to_measurement }.to raise_error }
22
+ specify { expect { subject.to_measurement }.to raise_error(ArgumentError, "Invalid unit: 'foobar'") }
23
23
  end
24
24
  end
25
25
 
@@ -31,7 +31,7 @@ RSpec.describe String do
31
31
 
32
32
  describe 'with invalid unit' do
33
33
  subject { 'person' }
34
- specify { expect { subject.to_unit }.to raise_error }
34
+ specify { expect { subject.to_unit }.to raise_error(ArgumentError, "Invalid unit: 'person'") }
35
35
  end
36
36
  end
37
37
  end
@@ -9,7 +9,7 @@ RSpec.describe Symbol do
9
9
 
10
10
  describe 'with invalid unit' do
11
11
  subject { :person }
12
- specify { expect { subject.to_unit }.to raise_error }
12
+ specify { expect { subject.to_unit }.to raise_error(ArgumentError, "Invalid unit: 'person'") }
13
13
  end
14
14
  end
15
15
  end
@@ -38,13 +38,13 @@ RSpec.describe Measurement do
38
38
 
39
39
  describe 'with invalid quantity' do
40
40
  it 'raises exception' do
41
- expect { subject.new('hi') }.to raise_exception
41
+ expect { subject.new('hi') }.to raise_error(ArgumentError, "Invalid quantity: 'hi'")
42
42
  end
43
43
  end
44
44
 
45
45
  describe 'with invalid unit' do
46
46
  it 'raises exception' do
47
- expect { subject.new(3, :finklebaum) }.to raise_exception
47
+ expect { subject.new(3, :finklebaum) }.to raise_error(ArgumentError, "Invalid unit: 'finklebaum'")
48
48
  end
49
49
  end
50
50
  end
@@ -146,7 +146,7 @@ RSpec.describe Measurement do
146
146
  end
147
147
 
148
148
  it 'raises exception when undefined' do
149
- expect { subject.parse('3 finklebaums') }.to raise_error
149
+ expect { subject.parse('3 finklebaums') }.to raise_error(ArgumentError, "Invalid unit: 'finklebaums'")
150
150
  end
151
151
  end
152
152
  end
@@ -174,11 +174,11 @@ RSpec.describe Measurement do
174
174
  end
175
175
 
176
176
  it 'raises exception if unit exists and is not convertable' do
177
- expect { measurement.convert_to(:inches) }.to raise_error
177
+ expect { measurement.convert_to(:inches) }.to raise_error(ArgumentError, "Invalid conversion: 'count' to 'in.'")
178
178
  end
179
179
 
180
180
  it 'raises exception if unit does not exist' do
181
- expect { measurement.convert_to(:finklebaum) }.to raise_error
181
+ expect { measurement.convert_to(:finklebaum) }.to raise_error(ArgumentError, "Invalid unit: 'finklebaum'")
182
182
  end
183
183
  end
184
184
 
@@ -222,7 +222,7 @@ RSpec.describe Measurement do
222
222
  it 'raises exception for incompatible units' do
223
223
  other = subject.new(4, :inches)
224
224
  expect(other.unit).to_not eq measurement.unit
225
- expect { measurement + other }.to raise_error
225
+ expect { measurement + other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'")
226
226
  end
227
227
  end
228
228
 
@@ -231,7 +231,7 @@ RSpec.describe Measurement do
231
231
 
232
232
  it 'subtracts numeric values' do
233
233
  result = measurement - 4
234
- expect(result.quantity).to eq -1
234
+ expect(result.quantity).to eq(-1)
235
235
  expect(result.unit).to eq measurement.unit
236
236
  end
237
237
 
@@ -240,7 +240,7 @@ RSpec.describe Measurement do
240
240
  expect(other.unit).to eq measurement.unit
241
241
 
242
242
  result = measurement - other
243
- expect(result.quantity).to eq -1
243
+ expect(result.quantity).to eq(-1)
244
244
  expect(result.unit).to eq measurement.unit
245
245
  end
246
246
 
@@ -249,14 +249,14 @@ RSpec.describe Measurement do
249
249
  expect(other.unit).to_not eq measurement.unit
250
250
 
251
251
  result = measurement - other
252
- expect(result.quantity).to eq -21
252
+ expect(result.quantity).to eq(-21)
253
253
  expect(result.unit).to eq measurement.unit
254
254
  end
255
255
 
256
256
  it 'raises exception for incompatible units' do
257
257
  other = subject.new(4, :inches)
258
258
  expect(other.unit).to_not eq measurement.unit
259
- expect { measurement - other }.to raise_error
259
+ expect { measurement - other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'")
260
260
  end
261
261
  end
262
262
 
@@ -290,7 +290,7 @@ RSpec.describe Measurement do
290
290
  it 'raises exception for incompatible units' do
291
291
  other = subject.new(4, :inches)
292
292
  expect(other.unit).to_not eq measurement.unit
293
- expect { measurement * other }.to raise_error
293
+ expect { measurement * other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'")
294
294
  end
295
295
  end
296
296
 
@@ -324,7 +324,7 @@ RSpec.describe Measurement do
324
324
  it 'raises exception for incompatible units' do
325
325
  other = subject.new(4, :inches)
326
326
  expect(other.unit).to_not eq measurement.unit
327
- expect { measurement / other }.to raise_error
327
+ expect { measurement / other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'")
328
328
  end
329
329
  end
330
330
 
@@ -336,7 +336,7 @@ RSpec.describe Measurement do
336
336
  end
337
337
 
338
338
  it 'raises exception for non-numeric values' do
339
- expect { measurement ** subject.new(3) }.to raise_error
339
+ expect { measurement ** subject.new(3) }.to raise_error(ArgumentError, 'Invalid arithmetic: 3 count ** 3 count')
340
340
  end
341
341
  end
342
342
 
@@ -360,6 +360,46 @@ RSpec.describe Measurement do
360
360
  end
361
361
  end
362
362
 
363
+ describe '#>' do
364
+ let(:measurement) { subject.new(3) }
365
+
366
+ it 'raises ArgumentError for non-measurement objects' do
367
+ expect { measurement > 2 }.to raise_error(ArgumentError)
368
+ end
369
+
370
+ it 'returns false for measurements for different unit' do
371
+ expect { measurement > subject.new(2, :dozen) }.to raise_error(ArgumentError)
372
+ end
373
+
374
+ it 'returns true for measurements with same unit and larger quantity' do
375
+ expect(measurement > subject.new(2)).to be true
376
+ end
377
+
378
+ it 'returns false for measurements with same unit and smaller quantity' do
379
+ expect(measurement > subject.new(4)).to be false
380
+ end
381
+ end
382
+
383
+ describe '#<' do
384
+ let(:measurement) { subject.new(3) }
385
+
386
+ it 'raises ArgumentError for non-measurement objects' do
387
+ expect { measurement < 4 }.to raise_error(ArgumentError)
388
+ end
389
+
390
+ it 'returns false for measurements for different unit' do
391
+ expect { measurement < subject.new(4, :dozen) }.to raise_error(ArgumentError)
392
+ end
393
+
394
+ it 'returns true for measurements with same unit and smaller quantity' do
395
+ expect(measurement < subject.new(4)).to be true
396
+ end
397
+
398
+ it 'returns false for measurements with same unit and larger quantity' do
399
+ expect(measurement < subject.new(2)).to be false
400
+ end
401
+ end
402
+
363
403
  describe '#to_s' do
364
404
  it 'returns the quantity and unit' do
365
405
  expect(subject.new(3.5).to_s).to eq '3.5 count'
data/tasks/rspec.rake CHANGED
@@ -2,5 +2,5 @@ require 'rspec/core/rake_task'
2
2
 
3
3
  desc 'Run all examples'
4
4
  RSpec::Core::RakeTask.new(:spec) do |t|
5
- t.rspec_opts = %w(--color)
5
+ t.rspec_opts = %w(--force-color)
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-measurement
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Huggins
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-27 00:00:00.000000000 Z
11
+ date: 2021-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -44,8 +44,8 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
+ - ".github/workflows/CI.yml"
47
48
  - ".gitignore"
48
- - ".travis.yml"
49
49
  - CHANGELOG.md
50
50
  - Gemfile
51
51
  - LICENSE.txt
@@ -96,7 +96,7 @@ homepage: https://github.com/mhuggins/ruby-measurement
96
96
  licenses:
97
97
  - MIT
98
98
  metadata: {}
99
- post_install_message:
99
+ post_install_message:
100
100
  rdoc_options: []
101
101
  require_paths:
102
102
  - lib
@@ -111,9 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  requirements: []
114
- rubyforge_project:
115
- rubygems_version: 2.5.1
116
- signing_key:
114
+ rubygems_version: 3.2.3
115
+ signing_key:
117
116
  specification_version: 4
118
117
  summary: Simple gem for calculating and converting measurements
119
118
  test_files:
data/.travis.yml DELETED
@@ -1,11 +0,0 @@
1
- language: ruby
2
- sudo: false
3
-
4
- rvm:
5
- - 1.9.3
6
- - 2.0.0
7
- - ruby-head
8
-
9
- matrix:
10
- allow_failures:
11
- - rvm: ruby-head