weighable 1.1.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +32 -0
- data/.github/workflows/ci.yml +19 -0
- data/.gitignore +1 -1
- data/.ruby-version +1 -1
- data/Gemfile-rails4 +6 -0
- data/{Gemfile → Gemfile-rails5} +2 -0
- data/Gemfile-rails6 +6 -0
- data/README.md +1 -1
- data/lib/weighable/model.rb +3 -1
- data/lib/weighable/version.rb +1 -1
- data/lib/weighable/weight.rb +66 -42
- data/weighable.gemspec +2 -2
- metadata +17 -15
- data/circle.yml +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31c05d666a97b448105e5a074c65a5f3f55a174f9e594f04567d36abf07ec3b2
|
4
|
+
data.tar.gz: eecfc7259d196bc8480bec0a8a65960359ccbfd2a5a1bec6491f421890f46706
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47017d565bce95f6b597459155d83d7d4b5ba372e3e94c1aa89a3581706ec1a98cd0f746b04154bd97d23a369453d25c7de51278562948ebee7f6d2bb61d5a79
|
7
|
+
data.tar.gz: 0a9238a5c9ca54d15a4cd3d766dc27285faea5d8d1a42e1584e33a239326086ba03a8e613a464526bf007b004ddacac12aa0a036c47f2e5def83d77709c01198
|
@@ -0,0 +1,32 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
environment:
|
5
|
+
CC_TEST_REPORTER_ID: 0f917edc031f16b4e113269d2a97c6db32df02fb034919c35d95c896d68e5c2a
|
6
|
+
docker:
|
7
|
+
- image: circleci/ruby:2.2.8
|
8
|
+
steps:
|
9
|
+
- checkout
|
10
|
+
- restore_cache:
|
11
|
+
keys:
|
12
|
+
- weighable-{{ checksum "Gemfile" }}
|
13
|
+
- weighable-
|
14
|
+
- run:
|
15
|
+
name: install dependencies
|
16
|
+
command: bundle check || bundle install
|
17
|
+
- save_cache:
|
18
|
+
key: weighable-{{ checksum "Gemfile" }}
|
19
|
+
paths:
|
20
|
+
- vendor/bundle
|
21
|
+
- run:
|
22
|
+
name: Setup Code Climate test-reporter
|
23
|
+
command: |
|
24
|
+
# download test reporter as a static binary
|
25
|
+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
26
|
+
chmod +x ./cc-test-reporter
|
27
|
+
- run:
|
28
|
+
name: Run rspec in parallel
|
29
|
+
command: |
|
30
|
+
./cc-test-reporter before-build
|
31
|
+
bundle exec rspec --format documentation --format RspecJunitFormatter -o rspec.xml
|
32
|
+
./cc-test-reporter after-build --exit-code $?
|
@@ -0,0 +1,19 @@
|
|
1
|
+
on: push
|
2
|
+
|
3
|
+
jobs:
|
4
|
+
build:
|
5
|
+
runs-on: ubuntu-16.04
|
6
|
+
strategy:
|
7
|
+
matrix:
|
8
|
+
ruby: [ '2.5', '2.6', '2.7' ]
|
9
|
+
rails: [ 4, 5, 6 ]
|
10
|
+
name: weighable ruby:${{matrix.ruby}} rails:${{matrix.rails}}
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v2
|
13
|
+
- uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: ${{ matrix.ruby }}
|
16
|
+
- run: |
|
17
|
+
gem install bundler
|
18
|
+
bundle install --jobs 4 --retry 3 --gemfile=Gemfile-rails${{ matrix.rails }}
|
19
|
+
bundle exec rspec
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.0
|
data/Gemfile-rails4
ADDED
data/{Gemfile → Gemfile-rails5}
RENAMED
data/Gemfile-rails6
ADDED
data/README.md
CHANGED
@@ -83,7 +83,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
83
83
|
|
84
84
|
## Contributing
|
85
85
|
|
86
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/trobrock/weighable.
|
87
87
|
|
88
88
|
## License
|
89
89
|
|
data/lib/weighable/model.rb
CHANGED
@@ -13,7 +13,9 @@ module Weighable
|
|
13
13
|
|
14
14
|
def define_setter(column, store_as: :gram, precision: nil)
|
15
15
|
define_method "#{column}=" do |weight|
|
16
|
-
weight
|
16
|
+
if weight.respond_to?(:key?) && weight.key?('value') && weight.key?('unit')
|
17
|
+
weight = Weight.new(weight['value'], weight['unit'])
|
18
|
+
end
|
17
19
|
original_unit = weight.try(:unit)
|
18
20
|
|
19
21
|
if original_unit && original_unit != Weight::UNIT[:unit]
|
data/lib/weighable/version.rb
CHANGED
data/lib/weighable/weight.rb
CHANGED
@@ -7,102 +7,126 @@ module Weighable
|
|
7
7
|
attr_reader :value, :unit
|
8
8
|
|
9
9
|
UNIT = {
|
10
|
-
gram:
|
11
|
-
ounce:
|
12
|
-
pound:
|
13
|
-
milligram:
|
14
|
-
kilogram:
|
15
|
-
unit:
|
10
|
+
gram: 0,
|
11
|
+
ounce: 1,
|
12
|
+
pound: 2,
|
13
|
+
milligram: 3,
|
14
|
+
kilogram: 4,
|
15
|
+
unit: 5,
|
16
|
+
fluid_ounce: 6
|
16
17
|
}.freeze
|
17
18
|
|
18
19
|
UNIT_ABBREVIATION = {
|
19
|
-
gram:
|
20
|
-
ounce:
|
21
|
-
pound:
|
22
|
-
milligram:
|
23
|
-
kilogram:
|
24
|
-
|
20
|
+
gram: 'g',
|
21
|
+
ounce: 'oz',
|
22
|
+
pound: 'lb',
|
23
|
+
milligram: 'mg',
|
24
|
+
kilogram: 'kg',
|
25
|
+
fluid_ounce: 'fl oz',
|
26
|
+
unit: nil
|
25
27
|
}.freeze
|
26
28
|
|
27
29
|
ABBREVIATION_ALIASES = {
|
28
|
-
'g'
|
29
|
-
'oz'
|
30
|
-
'lb'
|
31
|
-
'mg'
|
32
|
-
'kg'
|
33
|
-
'
|
34
|
-
'
|
35
|
-
|
30
|
+
'g' => :gram,
|
31
|
+
'oz' => :ounce,
|
32
|
+
'lb' => :pound,
|
33
|
+
'mg' => :milligram,
|
34
|
+
'kg' => :kilogram,
|
35
|
+
'fl oz' => :fluid_ounce,
|
36
|
+
'ea' => :unit,
|
37
|
+
'each' => :unit,
|
38
|
+
nil => :unit
|
36
39
|
}.freeze
|
37
40
|
|
38
|
-
GRAMS_PER_OUNCE = BigDecimal
|
39
|
-
GRAMS_PER_POUND = BigDecimal
|
40
|
-
OUNCES_PER_POUND = BigDecimal
|
41
|
-
MILLIGRAMS_PER_GRAM = BigDecimal
|
42
|
-
KILOGRAMS_PER_GRAM = BigDecimal
|
43
|
-
IDENTITY = BigDecimal
|
41
|
+
GRAMS_PER_OUNCE = BigDecimal('28.34952')
|
42
|
+
GRAMS_PER_POUND = BigDecimal('453.59237')
|
43
|
+
OUNCES_PER_POUND = BigDecimal('16')
|
44
|
+
MILLIGRAMS_PER_GRAM = BigDecimal('1000')
|
45
|
+
KILOGRAMS_PER_GRAM = BigDecimal('0.001')
|
46
|
+
IDENTITY = BigDecimal('1')
|
44
47
|
|
45
48
|
MILLIGRAMS_PER_OUNCE = GRAMS_PER_OUNCE * MILLIGRAMS_PER_GRAM
|
46
49
|
KILOGRAMS_PER_OUNCE = GRAMS_PER_OUNCE * KILOGRAMS_PER_GRAM
|
47
50
|
MILLIGRAMS_PER_POUND = GRAMS_PER_POUND * MILLIGRAMS_PER_GRAM
|
48
51
|
KILOGRAMS_PER_POUND = GRAMS_PER_POUND * KILOGRAMS_PER_GRAM
|
49
|
-
|
52
|
+
MILLIGRAMS_PER_KILOGRAM = MILLIGRAMS_PER_GRAM**2
|
53
|
+
FLUID_OUNCE_PER_OUNCE = IDENTITY
|
50
54
|
|
51
55
|
CONVERSIONS = {
|
52
56
|
UNIT[:unit] => {
|
53
57
|
UNIT[:unit] => [:*, IDENTITY]
|
54
58
|
},
|
55
59
|
UNIT[:gram] => {
|
56
|
-
UNIT[:gram]
|
57
|
-
UNIT[:ounce]
|
58
|
-
UNIT[:pound]
|
59
|
-
UNIT[:milligram]
|
60
|
-
UNIT[:kilogram]
|
60
|
+
UNIT[:gram] => [:*, IDENTITY],
|
61
|
+
UNIT[:ounce] => [:/, GRAMS_PER_OUNCE],
|
62
|
+
UNIT[:pound] => [:/, GRAMS_PER_POUND],
|
63
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_GRAM],
|
64
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_GRAM],
|
65
|
+
UNIT[:fluid_ounce] => [:/, GRAMS_PER_OUNCE]
|
61
66
|
},
|
62
67
|
UNIT[:ounce] => {
|
63
68
|
UNIT[:gram] => [:*, GRAMS_PER_OUNCE],
|
64
69
|
UNIT[:ounce] => [:*, IDENTITY],
|
65
70
|
UNIT[:pound] => [:/, OUNCES_PER_POUND],
|
66
71
|
UNIT[:milligram] => [:*, MILLIGRAMS_PER_OUNCE],
|
67
|
-
UNIT[:kilogram] => [:*, KILOGRAMS_PER_OUNCE]
|
72
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_OUNCE],
|
73
|
+
UNIT[:fluid_ounce] => [:*, IDENTITY]
|
68
74
|
},
|
69
75
|
UNIT[:pound] => {
|
70
76
|
UNIT[:gram] => [:*, GRAMS_PER_POUND],
|
71
77
|
UNIT[:ounce] => [:*, OUNCES_PER_POUND],
|
72
78
|
UNIT[:pound] => [:*, IDENTITY],
|
73
79
|
UNIT[:milligram] => [:*, MILLIGRAMS_PER_POUND],
|
74
|
-
UNIT[:kilogram] => [:*, KILOGRAMS_PER_POUND]
|
80
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_POUND],
|
81
|
+
UNIT[:fluid_ounce] => [:*, OUNCES_PER_POUND]
|
75
82
|
},
|
76
83
|
UNIT[:milligram] => {
|
77
84
|
UNIT[:gram] => [:/, MILLIGRAMS_PER_GRAM],
|
78
85
|
UNIT[:ounce] => [:/, MILLIGRAMS_PER_OUNCE],
|
79
86
|
UNIT[:pound] => [:/, MILLIGRAMS_PER_POUND],
|
80
87
|
UNIT[:milligram] => [:*, IDENTITY],
|
81
|
-
UNIT[:kilogram] => [:/,
|
88
|
+
UNIT[:kilogram] => [:/, MILLIGRAMS_PER_KILOGRAM],
|
89
|
+
UNIT[:fluid_ounce] => [:/, MILLIGRAMS_PER_OUNCE]
|
82
90
|
},
|
83
91
|
UNIT[:kilogram] => {
|
84
92
|
UNIT[:gram] => [:/, KILOGRAMS_PER_GRAM],
|
85
93
|
UNIT[:ounce] => [:/, KILOGRAMS_PER_OUNCE],
|
86
94
|
UNIT[:pound] => [:/, KILOGRAMS_PER_POUND],
|
87
|
-
UNIT[:milligram] => [:*,
|
88
|
-
UNIT[:kilogram] => [:*, IDENTITY]
|
95
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_KILOGRAM],
|
96
|
+
UNIT[:kilogram] => [:*, IDENTITY],
|
97
|
+
UNIT[:fluid_ounce] => [:/, KILOGRAMS_PER_OUNCE]
|
98
|
+
},
|
99
|
+
UNIT[:fluid_ounce] => {
|
100
|
+
UNIT[:fluid_ounce] => [:*, IDENTITY],
|
101
|
+
UNIT[:gram] => [:*, GRAMS_PER_OUNCE],
|
102
|
+
UNIT[:ounce] => [:*, IDENTITY],
|
103
|
+
UNIT[:pound] => [:/, OUNCES_PER_POUND],
|
104
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_OUNCE],
|
105
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_OUNCE],
|
89
106
|
}
|
90
107
|
}.freeze
|
91
108
|
|
92
109
|
def self.parse(string)
|
93
|
-
|
94
|
-
|
110
|
+
trimmed = string.strip
|
111
|
+
unit_start = trimmed.index(' ')
|
112
|
+
if unit_start
|
113
|
+
unit = trimmed.slice!(unit_start + 1..-1)
|
114
|
+
value = trimmed.slice!(0..unit_start - 1)
|
115
|
+
from_value_and_unit(value, unit)
|
116
|
+
else
|
117
|
+
from_value_and_unit(trimmed, nil)
|
118
|
+
end
|
95
119
|
end
|
96
120
|
|
97
121
|
def self.from_value_and_unit(value, unit)
|
98
122
|
unit = parse_unit(unit)
|
99
|
-
fail ArgumentError, 'invalid weight' if unit.nil? || value.
|
123
|
+
fail ArgumentError, 'invalid weight' if unit.nil? || value.to_s.strip.empty?
|
100
124
|
Weight.new(value, unit)
|
101
125
|
end
|
102
126
|
|
103
127
|
def self.parse_unit(unit)
|
104
128
|
unit = ActiveSupport::Inflector.singularize(unit.downcase) unless unit.nil?
|
105
|
-
unit_symbol = unit ? unit.to_sym : unit
|
129
|
+
unit_symbol = unit ? unit.tr(' ', '_').to_sym : unit
|
106
130
|
UNIT[unit_symbol] || ABBREVIATION_ALIASES[unit]
|
107
131
|
end
|
108
132
|
|
@@ -160,7 +184,7 @@ module Weighable
|
|
160
184
|
Weight.new(@value / to_math_value(other), unit_name)
|
161
185
|
else
|
162
186
|
other = other.to(unit_name)
|
163
|
-
BigDecimal
|
187
|
+
BigDecimal(@value / other.value)
|
164
188
|
end
|
165
189
|
end
|
166
190
|
|
data/weighable.gemspec
CHANGED
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
-
spec.add_dependency 'activesupport'
|
23
|
+
spec.add_dependency 'activesupport'
|
24
24
|
|
25
|
-
spec.add_development_dependency 'bundler', '~> 1
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
27
|
spec.add_development_dependency 'rubocop', '~> 0.49.0'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.4.0'
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weighable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trae Robrock
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1
|
33
|
+
version: '2.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1
|
40
|
+
version: '2.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,20 +143,23 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
+
- ".circleci/config.yml"
|
147
|
+
- ".github/workflows/ci.yml"
|
146
148
|
- ".gitignore"
|
147
149
|
- ".hound.yml"
|
148
150
|
- ".rspec"
|
149
151
|
- ".rubocop.yml"
|
150
152
|
- ".ruby-style.yml"
|
151
153
|
- ".ruby-version"
|
152
|
-
- Gemfile
|
154
|
+
- Gemfile-rails4
|
155
|
+
- Gemfile-rails5
|
156
|
+
- Gemfile-rails6
|
153
157
|
- Guardfile
|
154
158
|
- LICENSE.txt
|
155
159
|
- README.md
|
156
160
|
- Rakefile
|
157
161
|
- bin/console
|
158
162
|
- bin/setup
|
159
|
-
- circle.yml
|
160
163
|
- lib/weighable.rb
|
161
164
|
- lib/weighable/active_record/migration_extensions/schema_statements.rb
|
162
165
|
- lib/weighable/active_record/migration_extensions/table.rb
|
@@ -173,7 +176,7 @@ homepage: https://github.com/greenbits/weighable
|
|
173
176
|
licenses:
|
174
177
|
- MIT
|
175
178
|
metadata: {}
|
176
|
-
post_install_message:
|
179
|
+
post_install_message:
|
177
180
|
rdoc_options: []
|
178
181
|
require_paths:
|
179
182
|
- lib
|
@@ -188,9 +191,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
188
191
|
- !ruby/object:Gem::Version
|
189
192
|
version: '0'
|
190
193
|
requirements: []
|
191
|
-
|
192
|
-
|
193
|
-
signing_key:
|
194
|
+
rubygems_version: 3.1.2
|
195
|
+
signing_key:
|
194
196
|
specification_version: 4
|
195
197
|
summary: Like BigDecimal, but for weight. Includes Rails integration.
|
196
198
|
test_files: []
|
data/circle.yml
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
machine:
|
2
|
-
environment:
|
3
|
-
CC_TEST_REPORTER_ID: 0f917edc031f16b4e113269d2a97c6db32df02fb034919c35d95c896d68e5c2a
|
4
|
-
|
5
|
-
dependencies:
|
6
|
-
pre:
|
7
|
-
- gem update bundler
|
8
|
-
post:
|
9
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
10
|
-
- chmod +x ./cc-test-reporter
|
11
|
-
|
12
|
-
test:
|
13
|
-
pre:
|
14
|
-
- ./cc-test-reporter before-build
|
15
|
-
override:
|
16
|
-
- bundle exec rspec --format documentation --format RspecJunitFormatter -o $CIRCLE_TEST_REPORTS/rspec.xml
|
17
|
-
post:
|
18
|
-
- ./cc-test-reporter after-build --exit-code $EXIT_CODE
|