weighable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.hound.yml +2 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.ruby-style.yml +255 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +3 -0
- data/lib/weighable/active_record/migration_extensions/schema_statements.rb +19 -0
- data/lib/weighable/active_record/migration_extensions/table.rb +19 -0
- data/lib/weighable/core_ext/numeric.rb +12 -0
- data/lib/weighable/core_ext/string.rb +12 -0
- data/lib/weighable/core_ext.rb +2 -0
- data/lib/weighable/errors.rb +4 -0
- data/lib/weighable/inflections.rb +4 -0
- data/lib/weighable/model.rb +56 -0
- data/lib/weighable/railtie.rb +18 -0
- data/lib/weighable/version.rb +3 -0
- data/lib/weighable/weight.rb +171 -0
- data/lib/weighable.rb +10 -0
- data/weighable.gemspec +31 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 075f2df68dabd18abf769e897b901c6d1481fa48
|
4
|
+
data.tar.gz: 0968b3a9d07bd772ade4cc17e82f425e5a61a81f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b1c3daa50fb95b1d86b66da11e37d2795328d2ad5cd89dc89342b49b1c87fb5a3a69152448c32bbbf64b1d8b7051d8c43d2aa63dde6f368b55756174592fc32c
|
7
|
+
data.tar.gz: 84877638d4ab602cd99d5d6b85ce5d89d3329a9763dd5fe1f57cdab706286b62189f9831b32e422d16575354097cbb3a1deeed9a990d6db952b65fe717c3def1
|
data/.gitignore
ADDED
data/.hound.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-style.yml
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "vendor/**/*"
|
4
|
+
- "db/schema.rb"
|
5
|
+
UseCache: false
|
6
|
+
Style/CollectionMethods:
|
7
|
+
Description: Preferred collection methods.
|
8
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
|
9
|
+
Enabled: true
|
10
|
+
PreferredMethods:
|
11
|
+
collect: map
|
12
|
+
collect!: map!
|
13
|
+
find_all: select
|
14
|
+
inject: reduce
|
15
|
+
detect: find
|
16
|
+
Style/DotPosition:
|
17
|
+
Description: Checks the position of the dot in multi-line method calls.
|
18
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
|
19
|
+
Enabled: true
|
20
|
+
EnforcedStyle: leading
|
21
|
+
SupportedStyles:
|
22
|
+
- leading
|
23
|
+
- trailing
|
24
|
+
Style/FileName:
|
25
|
+
Description: Use snake_case for source file names.
|
26
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
|
27
|
+
Enabled: true
|
28
|
+
Exclude: []
|
29
|
+
Style/GuardClause:
|
30
|
+
Description: Check for conditionals that can be replaced with guard clauses
|
31
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
|
32
|
+
Enabled: true
|
33
|
+
MinBodyLength: 1
|
34
|
+
Style/IfUnlessModifier:
|
35
|
+
Description: Favor modifier if/unless usage when you have a single-line body.
|
36
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
|
37
|
+
Enabled: true
|
38
|
+
MaxLineLength: 80
|
39
|
+
Style/MultilineOperationIndentation:
|
40
|
+
EnforcedStyle: indented
|
41
|
+
SupportedStyles:
|
42
|
+
- aligned
|
43
|
+
- indented
|
44
|
+
Style/MultilineMethodCallIndentation:
|
45
|
+
EnforcedStyle: indented
|
46
|
+
SupportedStyles:
|
47
|
+
- aligned
|
48
|
+
- indented
|
49
|
+
Style/OptionHash:
|
50
|
+
Description: Don't use option hashes when you can use keyword arguments.
|
51
|
+
Enabled: true
|
52
|
+
Style/PercentLiteralDelimiters:
|
53
|
+
Description: Use `%`-literal delimiters consistently
|
54
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
55
|
+
Enabled: true
|
56
|
+
PreferredDelimiters:
|
57
|
+
"%": "()"
|
58
|
+
"%i": "()"
|
59
|
+
"%q": "()"
|
60
|
+
"%Q": "()"
|
61
|
+
"%r": "{}"
|
62
|
+
"%s": "()"
|
63
|
+
"%w": "()"
|
64
|
+
"%W": "()"
|
65
|
+
"%x": "()"
|
66
|
+
Style/PredicateName:
|
67
|
+
Description: Check the names of predicate methods.
|
68
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
|
69
|
+
Enabled: true
|
70
|
+
NamePrefix:
|
71
|
+
- is_
|
72
|
+
- has_
|
73
|
+
- have_
|
74
|
+
NamePrefixBlacklist:
|
75
|
+
- is_
|
76
|
+
Exclude:
|
77
|
+
- spec/**/*
|
78
|
+
Style/RaiseArgs:
|
79
|
+
Description: Checks the arguments passed to raise/fail.
|
80
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
|
81
|
+
Enabled: true
|
82
|
+
EnforcedStyle: exploded
|
83
|
+
SupportedStyles:
|
84
|
+
- compact
|
85
|
+
- exploded
|
86
|
+
Style/SignalException:
|
87
|
+
Description: Checks for proper usage of fail and raise.
|
88
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
|
89
|
+
Enabled: true
|
90
|
+
EnforcedStyle: semantic
|
91
|
+
SupportedStyles:
|
92
|
+
- only_raise
|
93
|
+
- only_fail
|
94
|
+
- semantic
|
95
|
+
Style/SingleLineBlockParams:
|
96
|
+
Description: Enforces the names of some block params.
|
97
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
|
98
|
+
Enabled: true
|
99
|
+
Methods:
|
100
|
+
- reduce:
|
101
|
+
- a
|
102
|
+
- e
|
103
|
+
- inject:
|
104
|
+
- a
|
105
|
+
- e
|
106
|
+
Style/SingleLineMethods:
|
107
|
+
Description: Avoid single-line methods.
|
108
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
|
109
|
+
Enabled: true
|
110
|
+
AllowIfMethodIsEmpty: true
|
111
|
+
Style/StringLiterals:
|
112
|
+
Description: Checks if uses of quotes match the configured preference.
|
113
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
114
|
+
Enabled: true
|
115
|
+
EnforcedStyle: single_quotes
|
116
|
+
SupportedStyles:
|
117
|
+
- single_quotes
|
118
|
+
- double_quotes
|
119
|
+
Style/StringLiteralsInInterpolation:
|
120
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
121
|
+
match the configured preference.
|
122
|
+
Enabled: true
|
123
|
+
EnforcedStyle: single_quotes
|
124
|
+
SupportedStyles:
|
125
|
+
- single_quotes
|
126
|
+
- double_quotes
|
127
|
+
Style/TrailingCommaInArguments:
|
128
|
+
EnforcedStyleForMultiline: no_comma
|
129
|
+
SupportedStyles:
|
130
|
+
- comma
|
131
|
+
- consistent_comma
|
132
|
+
- no_comma
|
133
|
+
Style/TrailingCommaInLiteral:
|
134
|
+
EnforcedStyleForMultiline: no_comma
|
135
|
+
SupportedStyles:
|
136
|
+
- comma
|
137
|
+
- consistent_comma
|
138
|
+
- no_comma
|
139
|
+
Metrics/AbcSize:
|
140
|
+
Description: A calculated magnitude based on number of assignments, branches, and
|
141
|
+
conditions.
|
142
|
+
Enabled: false
|
143
|
+
Max: 15
|
144
|
+
Metrics/ClassLength:
|
145
|
+
Description: Avoid classes longer than 100 lines of code.
|
146
|
+
Enabled: false
|
147
|
+
CountComments: false
|
148
|
+
Max: 100
|
149
|
+
Metrics/ModuleLength:
|
150
|
+
CountComments: false
|
151
|
+
Max: 100
|
152
|
+
Description: Avoid modules longer than 100 lines of code.
|
153
|
+
Enabled: false
|
154
|
+
Metrics/CyclomaticComplexity:
|
155
|
+
Description: A complexity metric that is strongly correlated to the number of test
|
156
|
+
cases needed to validate a method.
|
157
|
+
Enabled: true
|
158
|
+
Max: 6
|
159
|
+
Metrics/MethodLength:
|
160
|
+
Description: Avoid methods longer than 10 lines of code.
|
161
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
|
162
|
+
Enabled: false
|
163
|
+
CountComments: false
|
164
|
+
Max: 10
|
165
|
+
Metrics/ParameterLists:
|
166
|
+
Description: Avoid parameter lists longer than three or four parameters.
|
167
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
|
168
|
+
Enabled: false
|
169
|
+
Max: 5
|
170
|
+
CountKeywordArgs: true
|
171
|
+
Metrics/PerceivedComplexity:
|
172
|
+
Description: A complexity metric geared towards measuring complexity for a human
|
173
|
+
reader.
|
174
|
+
Enabled: false
|
175
|
+
Max: 7
|
176
|
+
Metrics/LineLength:
|
177
|
+
Max: 100
|
178
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
179
|
+
# contaning a URI to be longer than Max.
|
180
|
+
AllowURI: true
|
181
|
+
URISchemes:
|
182
|
+
- http
|
183
|
+
- https
|
184
|
+
Lint/AssignmentInCondition:
|
185
|
+
Description: Don't use assignment in conditions.
|
186
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
|
187
|
+
Enabled: true
|
188
|
+
AllowSafeAssignment: true
|
189
|
+
Style/InlineComment:
|
190
|
+
Description: Avoid inline comments.
|
191
|
+
Enabled: false
|
192
|
+
Style/AccessorMethodName:
|
193
|
+
Description: Check the naming of accessor methods for get_/set_.
|
194
|
+
Enabled: false
|
195
|
+
Style/Alias:
|
196
|
+
Description: Use alias_method instead of alias.
|
197
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
|
198
|
+
Enabled: true
|
199
|
+
Style/Documentation:
|
200
|
+
Description: Document classes and non-namespace modules.
|
201
|
+
Enabled: false
|
202
|
+
Style/DoubleNegation:
|
203
|
+
Description: Checks for uses of double negation (!!).
|
204
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
205
|
+
Enabled: true
|
206
|
+
Style/EachWithObject:
|
207
|
+
Description: Prefer `each_with_object` over `inject` or `reduce`.
|
208
|
+
Enabled: false
|
209
|
+
Style/EmptyLiteral:
|
210
|
+
Description: Prefer literals to Array.new/Hash.new/String.new.
|
211
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
|
212
|
+
Enabled: true
|
213
|
+
Style/ModuleFunction:
|
214
|
+
Description: Checks for usage of `extend self` in modules.
|
215
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
|
216
|
+
Enabled: true
|
217
|
+
Style/OneLineConditional:
|
218
|
+
Description: Favor the ternary operator(?:) over if/then/else/end constructs.
|
219
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
|
220
|
+
Enabled: true
|
221
|
+
Style/PerlBackrefs:
|
222
|
+
Description: Avoid Perl-style regex back references.
|
223
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
|
224
|
+
Enabled: true
|
225
|
+
Style/Send:
|
226
|
+
Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
|
227
|
+
may overlap with existing methods.
|
228
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
|
229
|
+
Enabled: true
|
230
|
+
Style/SpecialGlobalVars:
|
231
|
+
Description: Avoid Perl-style global variables.
|
232
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
|
233
|
+
Enabled: true
|
234
|
+
Style/VariableInterpolation:
|
235
|
+
Description: Don't interpolate global, instance and class variables directly in
|
236
|
+
strings.
|
237
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
|
238
|
+
Enabled: true
|
239
|
+
Style/WhenThen:
|
240
|
+
Description: Use when x then ... for one-line cases.
|
241
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
|
242
|
+
Enabled: true
|
243
|
+
Lint/EachWithObjectArgument:
|
244
|
+
Description: Check for immutable argument given to each_with_object.
|
245
|
+
Enabled: true
|
246
|
+
Lint/HandleExceptions:
|
247
|
+
Description: Don't suppress exception.
|
248
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
|
249
|
+
Enabled: false
|
250
|
+
Lint/LiteralInCondition:
|
251
|
+
Description: Checks of literals used in conditions.
|
252
|
+
Enabled: true
|
253
|
+
Lint/LiteralInInterpolation:
|
254
|
+
Description: Checks for literals used in interpolation.
|
255
|
+
Enabled: true
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.4
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec", failed_mode: :keep do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# Feel free to open issues for suggestions and improvements
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Trae Robrock
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Weighable
|
2
|
+
|
3
|
+
A gem for dealing with weights in ruby and rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'weighable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install weighable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Using the weight class is as simple as:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
weight = Weighable::Weight.new(5, :gram)
|
27
|
+
weight = weight + Weighable::Weight.new(1, :ounce)
|
28
|
+
weight = weight.round(4)
|
29
|
+
```
|
30
|
+
|
31
|
+
This gets even cleaner when you include the core extensions (included by default with rails):
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'weighable/core_ext'
|
35
|
+
weight = 5.grams
|
36
|
+
weight = weight + 1.ounce
|
37
|
+
weight = weight.round(4)
|
38
|
+
```
|
39
|
+
|
40
|
+
## Usage with Rails
|
41
|
+
|
42
|
+
By default core extensions are included with rails. You also get some helpers to integrate weight
|
43
|
+
into active record.
|
44
|
+
|
45
|
+
Add a weight attribute to a model:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class AddWeightToItem < ActiveRecord::Migration
|
49
|
+
def change
|
50
|
+
change_table :items do |t|
|
51
|
+
t.weighable :my_weight
|
52
|
+
end
|
53
|
+
|
54
|
+
# OR
|
55
|
+
|
56
|
+
add_weighable :items, :my_weight
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Item < ActiveRecord::Base
|
61
|
+
include Weighable::Model
|
62
|
+
|
63
|
+
weighable :my_weight
|
64
|
+
|
65
|
+
# OR require the field to be present
|
66
|
+
|
67
|
+
weighable :my_weight, presence: true
|
68
|
+
end
|
69
|
+
|
70
|
+
# Now use it
|
71
|
+
|
72
|
+
item = Item.first
|
73
|
+
item.weight = 6.grams
|
74
|
+
item.save!
|
75
|
+
p item.weight == 6.grams # => true
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
81
|
+
|
82
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/greenbits/weighable.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'weighable'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Weighable
|
2
|
+
module ActiveRecord
|
3
|
+
module MigrationExtensions
|
4
|
+
module SchemaStatements
|
5
|
+
def add_weighable(table_name, column)
|
6
|
+
add_column table_name, "#{column}_value", :decimal, precision: 30, scale: 15
|
7
|
+
add_column table_name, "#{column}_unit", :integer, limit: 1
|
8
|
+
add_column table_name, "#{column}_display_unit", :integer, limit: 1
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove_weighable(table_name, column)
|
12
|
+
remove_column table_name, "#{column}_value"
|
13
|
+
remove_column table_name, "#{column}_unit"
|
14
|
+
remove_column table_name, "#{column}_display_unit"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Weighable
|
2
|
+
module ActiveRecord
|
3
|
+
module MigrationExtensions
|
4
|
+
module Table
|
5
|
+
def weighable(column)
|
6
|
+
column "#{column}_value", :decimal, precision: 30, scale: 15
|
7
|
+
column "#{column}_unit", :integer, limit: 1
|
8
|
+
column "#{column}_display_unit", :integer, limit: 1
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove_weighable(column)
|
12
|
+
remove_column "#{column}_value"
|
13
|
+
remove_column "#{column}_unit"
|
14
|
+
remove_column "#{column}_display_unit"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'weighable/inflections'
|
2
|
+
|
3
|
+
class Numeric
|
4
|
+
Weighable::Weight::UNIT.each do |unit, _|
|
5
|
+
unit = unit.to_s
|
6
|
+
plural_unit = ActiveSupport::Inflector.pluralize(unit)
|
7
|
+
define_method unit do
|
8
|
+
Weighable::Weight.new(self, unit)
|
9
|
+
end
|
10
|
+
alias_method plural_unit, unit unless plural_unit == unit
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'weighable/inflections'
|
2
|
+
|
3
|
+
class String
|
4
|
+
Weighable::Weight::UNIT.each do |unit, _|
|
5
|
+
unit = unit.to_s
|
6
|
+
plural_unit = ActiveSupport::Inflector.pluralize(unit)
|
7
|
+
define_method unit do
|
8
|
+
Weighable::Weight.new(self, unit)
|
9
|
+
end
|
10
|
+
alias_method plural_unit, unit unless plural_unit == unit
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Weighable
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def weighable(column, presence: false, store_as: :gram)
|
7
|
+
apply_validations(column, presence: presence)
|
8
|
+
|
9
|
+
define_method "#{column}=" do |weight|
|
10
|
+
weight = Weight.new(weight['value'], weight['unit']) if weight.is_a?(Hash)
|
11
|
+
original_unit = weight.try(:unit)
|
12
|
+
weight = weight.try(:to, store_as) if original_unit && original_unit != :each
|
13
|
+
|
14
|
+
public_send("#{column}_value=", weight.try(:value))
|
15
|
+
public_send("#{column}_unit=", weight.try(:unit))
|
16
|
+
public_send("#{column}_display_unit=", original_unit)
|
17
|
+
end
|
18
|
+
|
19
|
+
define_method column do
|
20
|
+
value = public_send("#{column}_value")
|
21
|
+
unit = public_send("#{column}_unit")
|
22
|
+
display_unit = public_send("#{column}_display_unit")
|
23
|
+
return unless value.present? && unit.present?
|
24
|
+
|
25
|
+
Weight.new(value, unit).to(display_unit)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def apply_validations(column, presence: false)
|
32
|
+
if presence
|
33
|
+
validates "#{column}_value", presence: presence
|
34
|
+
validates "#{column}_unit", presence: presence
|
35
|
+
validates "#{column}_display_unit", presence: presence
|
36
|
+
else
|
37
|
+
validates "#{column}_value", presence: presence, if: "should_validate_#{column}?"
|
38
|
+
validates "#{column}_unit", presence: presence, if: "should_validate_#{column}?"
|
39
|
+
validates "#{column}_display_unit", presence: presence, if: "should_validate_#{column}?"
|
40
|
+
end
|
41
|
+
|
42
|
+
validates "#{column}_unit", inclusion: {
|
43
|
+
in: Weight::UNIT.values,
|
44
|
+
message: 'is not a valid unit',
|
45
|
+
allow_nil: true
|
46
|
+
}
|
47
|
+
|
48
|
+
define_method "should_validate_#{column}?" do
|
49
|
+
public_send("#{column}_value").present? ||
|
50
|
+
public_send("#{column}_unit").present? ||
|
51
|
+
public_send("#{column}_display_unit").present?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Weighable
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer 'weighable.initialize' do
|
4
|
+
ActiveSupport.on_load(:active_record) do
|
5
|
+
require 'weighable/model'
|
6
|
+
require 'weighable/active_record/migration_extensions/schema_statements'
|
7
|
+
require 'weighable/active_record/migration_extensions/table'
|
8
|
+
require 'weighable/core_ext'
|
9
|
+
::ActiveRecord::Migration
|
10
|
+
.__send__(:include, Weighable::ActiveRecord::MigrationExtensions::SchemaStatements)
|
11
|
+
::ActiveRecord::ConnectionAdapters::TableDefinition
|
12
|
+
.__send__(:include, Weighable::ActiveRecord::MigrationExtensions::Table)
|
13
|
+
::ActiveRecord::ConnectionAdapters::Table
|
14
|
+
.__send__(:include, Weighable::ActiveRecord::MigrationExtensions::Table)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'weighable/inflections'
|
2
|
+
|
3
|
+
module Weighable
|
4
|
+
class Weight
|
5
|
+
attr_reader :value, :unit
|
6
|
+
|
7
|
+
UNIT = {
|
8
|
+
gram: 0,
|
9
|
+
ounce: 1,
|
10
|
+
pound: 2,
|
11
|
+
milligram: 3,
|
12
|
+
kilogram: 4,
|
13
|
+
unit: 5
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
UNIT_ABBREVIATION = {
|
17
|
+
gram: 'g',
|
18
|
+
ounce: 'oz',
|
19
|
+
pound: 'lb',
|
20
|
+
milligram: 'mg',
|
21
|
+
kilogram: 'kg',
|
22
|
+
unit: nil
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
GRAMS_PER_OUNCE = BigDecimal.new('28.34952')
|
26
|
+
GRAMS_PER_POUND = BigDecimal.new('453.59237')
|
27
|
+
OUNCES_PER_POUND = BigDecimal.new('16')
|
28
|
+
MILLIGRAMS_PER_GRAM = BigDecimal.new('1000')
|
29
|
+
KILOGRAMS_PER_GRAM = BigDecimal.new('0.001')
|
30
|
+
IDENTITY = BigDecimal.new('1')
|
31
|
+
|
32
|
+
MILLIGRAMS_PER_OUNCE = GRAMS_PER_OUNCE * MILLIGRAMS_PER_GRAM
|
33
|
+
KILOGRAMS_PER_OUNCE = GRAMS_PER_OUNCE * KILOGRAMS_PER_GRAM
|
34
|
+
MILLIGRAMS_PER_POUND = GRAMS_PER_POUND * MILLIGRAMS_PER_GRAM
|
35
|
+
KILOGRAMS_PER_POUND = GRAMS_PER_POUND * KILOGRAMS_PER_GRAM
|
36
|
+
KILOGRAMS_PER_MILLIGRAM = MILLIGRAMS_PER_GRAM**2
|
37
|
+
|
38
|
+
CONVERSIONS = {
|
39
|
+
UNIT[:unit] => {
|
40
|
+
UNIT[:unit] => [:*, IDENTITY]
|
41
|
+
},
|
42
|
+
UNIT[:gram] => {
|
43
|
+
UNIT[:gram] => [:*, IDENTITY],
|
44
|
+
UNIT[:ounce] => [:/, GRAMS_PER_OUNCE],
|
45
|
+
UNIT[:pound] => [:/, GRAMS_PER_POUND],
|
46
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_GRAM],
|
47
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_GRAM]
|
48
|
+
},
|
49
|
+
UNIT[:ounce] => {
|
50
|
+
UNIT[:gram] => [:*, GRAMS_PER_OUNCE],
|
51
|
+
UNIT[:ounce] => [:*, IDENTITY],
|
52
|
+
UNIT[:pound] => [:/, OUNCES_PER_POUND],
|
53
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_OUNCE],
|
54
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_OUNCE]
|
55
|
+
},
|
56
|
+
UNIT[:pound] => {
|
57
|
+
UNIT[:gram] => [:*, GRAMS_PER_POUND],
|
58
|
+
UNIT[:ounce] => [:*, OUNCES_PER_POUND],
|
59
|
+
UNIT[:pound] => [:*, IDENTITY],
|
60
|
+
UNIT[:milligram] => [:*, MILLIGRAMS_PER_POUND],
|
61
|
+
UNIT[:kilogram] => [:*, KILOGRAMS_PER_POUND]
|
62
|
+
},
|
63
|
+
UNIT[:milligram] => {
|
64
|
+
UNIT[:gram] => [:/, MILLIGRAMS_PER_GRAM],
|
65
|
+
UNIT[:ounce] => [:/, MILLIGRAMS_PER_OUNCE],
|
66
|
+
UNIT[:pound] => [:/, MILLIGRAMS_PER_POUND],
|
67
|
+
UNIT[:milligram] => [:*, IDENTITY],
|
68
|
+
UNIT[:kilogram] => [:/, KILOGRAMS_PER_MILLIGRAM]
|
69
|
+
},
|
70
|
+
UNIT[:kilogram] => {
|
71
|
+
UNIT[:gram] => [:/, KILOGRAMS_PER_GRAM],
|
72
|
+
UNIT[:ounce] => [:/, KILOGRAMS_PER_OUNCE],
|
73
|
+
UNIT[:pound] => [:/, KILOGRAMS_PER_POUND],
|
74
|
+
UNIT[:milligram] => [:*, KILOGRAMS_PER_MILLIGRAM],
|
75
|
+
UNIT[:kilogram] => [:*, IDENTITY]
|
76
|
+
}
|
77
|
+
}.freeze
|
78
|
+
|
79
|
+
def self.parse(string)
|
80
|
+
value, unit = string.split(' ')
|
81
|
+
Weight.new(value, UNIT_ABBREVIATION.find { |_k, v| v == unit }.first)
|
82
|
+
end
|
83
|
+
|
84
|
+
def initialize(value, unit)
|
85
|
+
@value = value.to_d
|
86
|
+
@unit = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
|
87
|
+
end
|
88
|
+
|
89
|
+
def to(unit)
|
90
|
+
new_unit = unit.is_a?(Fixnum) ? unit : unit_from_symbol(unit.to_sym)
|
91
|
+
operator, conversion = conversion(@unit, new_unit)
|
92
|
+
new_value = @value.public_send(operator, conversion)
|
93
|
+
Weight.new(new_value, unit)
|
94
|
+
end
|
95
|
+
|
96
|
+
UNIT.keys.each do |unit|
|
97
|
+
unit = unit.to_s
|
98
|
+
define_method "to_#{unit}" do
|
99
|
+
to(unit)
|
100
|
+
end
|
101
|
+
plural = ActiveSupport::Inflector.pluralize(unit)
|
102
|
+
alias_method "to_#{plural}", "to_#{unit}" unless unit == plural
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_s(only_unit: false)
|
106
|
+
if only_unit
|
107
|
+
unit_abbreviation.to_s
|
108
|
+
else
|
109
|
+
value = @value.to_f == @value.to_i ? @value.to_i : @value.to_f
|
110
|
+
[value, unit_abbreviation].compact.join(' ')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def ==(other)
|
115
|
+
other.class == self.class && other.value == @value && other.unit == @unit
|
116
|
+
end
|
117
|
+
|
118
|
+
def +(other)
|
119
|
+
other = other.to(unit_name)
|
120
|
+
Weight.new(@value + other.value, unit_name)
|
121
|
+
end
|
122
|
+
|
123
|
+
def -(other)
|
124
|
+
other = other.to(unit_name)
|
125
|
+
Weight.new(@value - other.value, unit_name)
|
126
|
+
end
|
127
|
+
|
128
|
+
def *(other)
|
129
|
+
other = other.to(unit_name)
|
130
|
+
Weight.new(@value * other.value, unit_name)
|
131
|
+
end
|
132
|
+
|
133
|
+
def round(precision = 0)
|
134
|
+
@value = @value.round(precision)
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
def /(other)
|
139
|
+
other = other.to(unit_name)
|
140
|
+
Weight.new(@value / other.value, unit_name)
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def unit_name
|
146
|
+
unit_from_int(@unit)
|
147
|
+
end
|
148
|
+
|
149
|
+
def unit_abbreviation
|
150
|
+
UNIT_ABBREVIATION[unit_from_int(@unit)]
|
151
|
+
end
|
152
|
+
|
153
|
+
def unit_from_symbol(unit)
|
154
|
+
unit = UNIT[unit]
|
155
|
+
fail "invalid unit '#{unit}'" unless unit
|
156
|
+
unit
|
157
|
+
end
|
158
|
+
|
159
|
+
def unit_from_int(int)
|
160
|
+
UNIT.find { |_k, v| v == int }.first
|
161
|
+
end
|
162
|
+
|
163
|
+
def conversion(from, to)
|
164
|
+
conversion = CONVERSIONS[from][to]
|
165
|
+
unless conversion
|
166
|
+
fail NoConversionError, "no conversion from #{unit_from_int(from)} to #{unit_from_int(to)}"
|
167
|
+
end
|
168
|
+
conversion
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
data/lib/weighable.rb
ADDED
data/weighable.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'weighable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'weighable'
|
8
|
+
spec.version = Weighable::VERSION
|
9
|
+
spec.authors = ['Trae Robrock']
|
10
|
+
spec.email = ['trobrock@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Like BigDecimal, but for weight. Includes Rails integration.'
|
13
|
+
spec.description = 'Like BigDecimal, but for weight. Includes Rails integration.'
|
14
|
+
spec.homepage = 'https://github.com/greenbits/weighable'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_dependency 'activesupport', '~> 4.2.4'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.38.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.4.0'
|
29
|
+
spec.add_development_dependency 'guard', '~> 2.13.0'
|
30
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.6.4'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trae Robrock
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.38.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.38.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.4.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.4.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.13.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.13.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 4.6.4
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 4.6.4
|
111
|
+
description: Like BigDecimal, but for weight. Includes Rails integration.
|
112
|
+
email:
|
113
|
+
- trobrock@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".hound.yml"
|
120
|
+
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".ruby-style.yml"
|
123
|
+
- ".ruby-version"
|
124
|
+
- Gemfile
|
125
|
+
- Guardfile
|
126
|
+
- LICENSE.txt
|
127
|
+
- README.md
|
128
|
+
- Rakefile
|
129
|
+
- bin/console
|
130
|
+
- bin/setup
|
131
|
+
- circle.yml
|
132
|
+
- lib/weighable.rb
|
133
|
+
- lib/weighable/active_record/migration_extensions/schema_statements.rb
|
134
|
+
- lib/weighable/active_record/migration_extensions/table.rb
|
135
|
+
- lib/weighable/core_ext.rb
|
136
|
+
- lib/weighable/core_ext/numeric.rb
|
137
|
+
- lib/weighable/core_ext/string.rb
|
138
|
+
- lib/weighable/errors.rb
|
139
|
+
- lib/weighable/inflections.rb
|
140
|
+
- lib/weighable/model.rb
|
141
|
+
- lib/weighable/railtie.rb
|
142
|
+
- lib/weighable/version.rb
|
143
|
+
- lib/weighable/weight.rb
|
144
|
+
- weighable.gemspec
|
145
|
+
homepage: https://github.com/greenbits/weighable
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.4.5.1
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Like BigDecimal, but for weight. Includes Rails integration.
|
169
|
+
test_files: []
|