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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2dd4d0bc6713072108f25718f88c150d19d7df99
4
- data.tar.gz: 8784e9371860547d7dfc2a71d17623ac00b18b8b
3
+ metadata.gz: c04f266562852781a9572cf76d3cf30135b9cb5f
4
+ data.tar.gz: acd24b99e103f35723cbce335d0fd1c0f741f9eb
5
5
  SHA512:
6
- metadata.gz: a9eea0965524a1a9de594f023164aa8a35738dd21c167bafecad79ca7f9618645c3351e43522d686453655973678233cfde5df8d72d534869748b6a6fa297be2
7
- data.tar.gz: 32487abec932b185419981ffab7dabe831eed02b6a49550c00f3f026ab179a80d2c10017e221f25f29b39e6a5a441927231c190f7516f63fc2b6a70dff57ed1d
6
+ metadata.gz: 7306ed225b7b733602482fe381971439b2ceac58a25526cb404544422726cb8398f0af5ba8139380982a18fcf8cd4b8b537026fdfea451e890d8ab9042a5bca2
7
+ data.tar.gz: 8aa69d0efdf0ce91cf0c0204882f7cabedf95b78a15e3f6236074a192fcd1bf781807def6b61e8a1f64375a4c4be55d411548fc044f4809bd659a175b2129ae1
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.4
1
+ 2.2.5
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Weighable
2
- VERSION = '0.5.1'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
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.5.1
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-04-08 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport