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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 796c7341c87131931e5896422b919270a952c9d87763a7e3696697682d7236ab
4
- data.tar.gz: 7cf5162848d619bb19863bffaccda5cf09725a689dca2db94424e9ecd5ee6596
3
+ metadata.gz: 31c05d666a97b448105e5a074c65a5f3f55a174f9e594f04567d36abf07ec3b2
4
+ data.tar.gz: eecfc7259d196bc8480bec0a8a65960359ccbfd2a5a1bec6491f421890f46706
5
5
  SHA512:
6
- metadata.gz: b798ed54c99730c1d4d8504a2eb246d34105d313aca1bc3e6c0b2fc5b13f6cd7fba76531e38ef1df827e99a0730c74a93577e820d8a93ebbb084632b0e45e11c
7
- data.tar.gz: 34e84421edec24cfa3f4b3b01425e366b991024c70481d0d6b160ce6683d5f4da747243acaf2933f6472b8b0c8120e85b33cb1dd285381ce2f9b38ca9c5db1d3
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
@@ -1,6 +1,6 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
3
+ /Gemfile*.lock
4
4
  /_yardoc/
5
5
  /coverage/
6
6
  /doc/
@@ -1 +1 @@
1
- 2.2.8
1
+ 2.7.0
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '> 4.2.4', '< 5'
4
+
5
+ # Specify your gem's dependencies in weighable.gemspec
6
+ gemspec
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'activesupport', '> 5', '< 6'
4
+
3
5
  # Specify your gem's dependencies in weighable.gemspec
4
6
  gemspec
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '> 6', '< 7'
4
+
5
+ # Specify your gem's dependencies in weighable.gemspec
6
+ gemspec
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/greenbits/weighable.
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/trobrock/weighable.
87
87
 
88
88
  ## License
89
89
 
@@ -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 = Weight.new(weight['value'], weight['unit']) if weight.is_a?(Hash)
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]
@@ -1,3 +1,3 @@
1
1
  module Weighable
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.5.0'.freeze
3
3
  end
@@ -7,102 +7,126 @@ module Weighable
7
7
  attr_reader :value, :unit
8
8
 
9
9
  UNIT = {
10
- gram: 0,
11
- ounce: 1,
12
- pound: 2,
13
- milligram: 3,
14
- kilogram: 4,
15
- unit: 5
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: 'g',
20
- ounce: 'oz',
21
- pound: 'lb',
22
- milligram: 'mg',
23
- kilogram: 'kg',
24
- unit: nil
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' => :gram,
29
- 'oz' => :ounce,
30
- 'lb' => :pound,
31
- 'mg' => :milligram,
32
- 'kg' => :kilogram,
33
- 'ea' => :unit,
34
- 'each' => :unit,
35
- nil => :unit
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.new('28.34952')
39
- GRAMS_PER_POUND = BigDecimal.new('453.59237')
40
- OUNCES_PER_POUND = BigDecimal.new('16')
41
- MILLIGRAMS_PER_GRAM = BigDecimal.new('1000')
42
- KILOGRAMS_PER_GRAM = BigDecimal.new('0.001')
43
- IDENTITY = BigDecimal.new('1')
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
- KILOGRAMS_PER_MILLIGRAM = MILLIGRAMS_PER_GRAM**2
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] => [:*, IDENTITY],
57
- UNIT[:ounce] => [:/, GRAMS_PER_OUNCE],
58
- UNIT[:pound] => [:/, GRAMS_PER_POUND],
59
- UNIT[:milligram] => [:*, MILLIGRAMS_PER_GRAM],
60
- UNIT[:kilogram] => [:*, KILOGRAMS_PER_GRAM]
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] => [:/, KILOGRAMS_PER_MILLIGRAM]
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] => [:*, KILOGRAMS_PER_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
- value, unit = string.split(' ')
94
- from_value_and_unit(value, unit)
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.nil?
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.new(@value / other.value)
187
+ BigDecimal(@value / other.value)
164
188
  end
165
189
  end
166
190
 
@@ -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', '~> 4.2.4'
23
+ spec.add_dependency 'activesupport'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.11'
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.1.0
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: 2018-07-09 00:00:00.000000000 Z
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: 4.2.4
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: 4.2.4
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.11'
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.11'
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
- rubyforge_project:
192
- rubygems_version: 2.7.6
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