weighable 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/lib/weighable/model.rb +14 -3
- data/lib/weighable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c04f266562852781a9572cf76d3cf30135b9cb5f
|
4
|
+
data.tar.gz: acd24b99e103f35723cbce335d0fd1c0f741f9eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7306ed225b7b733602482fe381971439b2ceac58a25526cb404544422726cb8398f0af5ba8139380982a18fcf8cd4b8b537026fdfea451e890d8ab9042a5bca2
|
7
|
+
data.tar.gz: 8aa69d0efdf0ce91cf0c0204882f7cabedf95b78a15e3f6236074a192fcd1bf781807def6b61e8a1f64375a4c4be55d411548fc044f4809bd659a175b2129ae1
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.2.
|
1
|
+
2.2.5
|
data/lib/weighable/model.rb
CHANGED
@@ -3,9 +3,15 @@ module Weighable
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
class_methods do
|
6
|
-
def weighable(column, presence: false, store_as: :gram)
|
6
|
+
def weighable(column, presence: false, store_as: :gram, precision: nil)
|
7
7
|
apply_validations(column, presence: presence)
|
8
|
+
define_setter(column, store_as: store_as, precision: precision)
|
9
|
+
define_getter(column)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
8
13
|
|
14
|
+
def define_setter(column, store_as: :gram, precision: nil)
|
9
15
|
define_method "#{column}=" do |weight|
|
10
16
|
weight = Weight.new(weight['value'], weight['unit']) if weight.is_a?(Hash)
|
11
17
|
original_unit = weight.try(:unit)
|
@@ -14,11 +20,18 @@ module Weighable
|
|
14
20
|
weight = weight.try(:to, store_as)
|
15
21
|
end
|
16
22
|
|
23
|
+
if precision.present?
|
24
|
+
local_precision = precision.is_a?(Proc) ? instance_exec(&precision) : precision
|
25
|
+
weight = weight.round(local_precision)
|
26
|
+
end
|
27
|
+
|
17
28
|
public_send("#{column}_value=", weight.try(:value))
|
18
29
|
public_send("#{column}_unit=", weight.try(:unit))
|
19
30
|
public_send("#{column}_display_unit=", original_unit)
|
20
31
|
end
|
32
|
+
end
|
21
33
|
|
34
|
+
def define_getter(column)
|
22
35
|
define_method column do
|
23
36
|
value = public_send("#{column}_value")
|
24
37
|
unit = public_send("#{column}_unit")
|
@@ -29,8 +42,6 @@ module Weighable
|
|
29
42
|
end
|
30
43
|
end
|
31
44
|
|
32
|
-
private
|
33
|
-
|
34
45
|
def apply_validations(column, presence: false)
|
35
46
|
if presence
|
36
47
|
validates "#{column}_value", presence: presence
|
data/lib/weighable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weighable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trae Robrock
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|