measured-rails 1.5.0 → 1.6.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 +4 -4
- data/README.md +10 -1
- data/lib/measured/rails/active_record.rb +22 -10
- data/lib/measured/rails/validations.rb +5 -2
- data/lib/measured/rails/version.rb +1 -1
- data/measured-rails.gemspec +2 -1
- metadata +13 -14
- data/repodb.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92ddafd5d1a33f79eee76d8b642bc787aacc8234
|
4
|
+
data.tar.gz: b31d4d3fefbfce82776255ce6dd9d035ffb2fb1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 440b341b39ad63d13c36a233a6e1943bd7557a863e32a2a7393f25c2e42021afeb12c5b90d2718ffd5b37b5fa70c3890051ed43f3d8de9b060a4b3cf2d180884
|
7
|
+
data.tar.gz: 835c01c1bba6432b34c1817bfc7910bd05561a790b1363388f932e7f4e2d4bb7cdbc3daa5edaed7d585d7bc682f2d1b783f3d148eb2b28baac3a590362f9f667
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or stand alone:
|
|
20
20
|
|
21
21
|
### ActiveRecord
|
22
22
|
|
23
|
-
Columns are expected to have the `_value` and `_unit` suffix, and be `DECIMAL` and `VARCHAR`, and defaults are accepted
|
23
|
+
Columns are expected to have the `_value` and `_unit` suffix, and be `DECIMAL` and `VARCHAR`, and defaults are accepted. Customizing the column used to hold units is supported, see below for details.
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
class AddWeightAndLengthToThings < ActiveRecord::Migration
|
@@ -43,6 +43,15 @@ class Thing < ActiveRecord::Base
|
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
|
+
You can optionally customize the model's unit column by specifying it in the `unit_field_name` option, as follows:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class ThingWithCustomUnitAccessor < ActiveRecord::Base
|
50
|
+
measured_length :length, :width, :height, unit_field_name: :size_unit
|
51
|
+
measured_weight :total_weight, :extra_weight, unit_field_name: :weight_unit
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
46
55
|
There are some simpler methods for predefined types:
|
47
56
|
|
48
57
|
```ruby
|
@@ -5,6 +5,7 @@ module Measured::Rails::ActiveRecord
|
|
5
5
|
def measured(measured_class, *fields)
|
6
6
|
options = fields.extract_options!
|
7
7
|
options = {}.merge(options)
|
8
|
+
defined_unit_accessors = []
|
8
9
|
|
9
10
|
measured_class = measured_class.constantize if measured_class.is_a?(String)
|
10
11
|
|
@@ -14,12 +15,22 @@ module Measured::Rails::ActiveRecord
|
|
14
15
|
|
15
16
|
fields.map(&:to_sym).each do |field|
|
16
17
|
raise Measured::Rails::Error, "The field #{ field } has already been measured" if measured_fields.keys.include?(field)
|
18
|
+
|
17
19
|
measured_fields[field] = options
|
18
20
|
|
21
|
+
if options[:unit_field_name]
|
22
|
+
unit_field_name = options[:unit_field_name].to_s
|
23
|
+
measured_fields[field][:unit_field_name] = unit_field_name
|
24
|
+
else
|
25
|
+
unit_field_name = "#{ field }_unit"
|
26
|
+
end
|
27
|
+
|
28
|
+
value_field_name = "#{ field }_value"
|
29
|
+
|
19
30
|
# Reader to retrieve measured object
|
20
31
|
define_method(field) do
|
21
|
-
value = public_send(
|
22
|
-
unit = public_send(
|
32
|
+
value = public_send(value_field_name)
|
33
|
+
unit = public_send(unit_field_name)
|
23
34
|
|
24
35
|
return nil unless value && unit
|
25
36
|
|
@@ -41,11 +52,10 @@ module Measured::Rails::ActiveRecord
|
|
41
52
|
define_method("#{ field }=") do |incoming|
|
42
53
|
if incoming.is_a?(measured_class)
|
43
54
|
instance_variable_set("@measured_#{ field }", incoming)
|
44
|
-
value_field_name = "#{ field }_value"
|
45
55
|
precision = self.column_for_attribute(value_field_name).precision
|
46
56
|
scale = self.column_for_attribute(value_field_name).scale
|
47
57
|
rounded_to_scale_value = incoming.value.round(scale)
|
48
|
-
|
58
|
+
|
49
59
|
max = self.class.measured_fields[field][:max_on_assignment]
|
50
60
|
if max && rounded_to_scale_value > max
|
51
61
|
rounded_to_scale_value = max
|
@@ -53,20 +63,22 @@ module Measured::Rails::ActiveRecord
|
|
53
63
|
raise Measured::Rails::Error, "The value #{rounded_to_scale_value} being set for column '#{value_field_name}' has too many significant digits. Please ensure it has no more than #{precision - scale} significant digits."
|
54
64
|
end
|
55
65
|
public_send("#{ value_field_name }=", rounded_to_scale_value)
|
56
|
-
public_send("#{
|
66
|
+
public_send("#{ unit_field_name }=", incoming.unit)
|
57
67
|
else
|
58
68
|
instance_variable_set("@measured_#{ field }", nil)
|
59
|
-
public_send("#{
|
60
|
-
public_send("#{
|
69
|
+
public_send("#{ value_field_name}=", nil)
|
70
|
+
public_send("#{ unit_field_name }=", nil)
|
61
71
|
end
|
62
72
|
end
|
63
73
|
|
74
|
+
next if defined_unit_accessors.include?(unit_field_name)
|
75
|
+
|
64
76
|
# Writer to override unit assignment
|
65
|
-
define_method("#{
|
77
|
+
define_method("#{ unit_field_name }=") do |incoming|
|
78
|
+
defined_unit_accessors << unit_field_name
|
66
79
|
incoming = measured_class.conversion.to_unit_name(incoming) if measured_class.valid_unit?(incoming)
|
67
|
-
write_attribute(
|
80
|
+
write_attribute(unit_field_name, incoming)
|
68
81
|
end
|
69
|
-
|
70
82
|
end
|
71
83
|
end
|
72
84
|
|
@@ -9,10 +9,13 @@ class MeasuredValidator < ActiveModel::EachValidator
|
|
9
9
|
|
10
10
|
def validate_each(record, attribute, measurable)
|
11
11
|
measured_config = record.class.measured_fields[attribute]
|
12
|
+
unit_field_name = measured_config[:unit_field_name] || "#{ attribute }_unit"
|
13
|
+
value_field_name = "#{ attribute }_value"
|
12
14
|
|
13
15
|
measured_class = measured_config[:class]
|
14
|
-
|
15
|
-
|
16
|
+
|
17
|
+
measurable_unit = record.public_send(unit_field_name)
|
18
|
+
measurable_value = record.public_send(value_field_name)
|
16
19
|
|
17
20
|
return unless measurable_unit.present? || measurable_value.present?
|
18
21
|
|
data/measured-rails.gemspec
CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_runtime_dependency "measured", Measured::Rails::VERSION
|
22
|
+
|
21
23
|
spec.add_runtime_dependency "railties", ">= 4.0"
|
22
24
|
spec.add_runtime_dependency "activemodel", ">= 4.0"
|
23
|
-
spec.add_runtime_dependency "measured", "1.5.0"
|
24
25
|
|
25
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
27
|
spec.add_development_dependency "minitest", "~> 5.5.1"
|
metadata
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: measured-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: measured
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.6.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: 1.6.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: railties
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,19 +39,19 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '4.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: activemodel
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: '4.0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: '4.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,7 +149,6 @@ files:
|
|
149
149
|
- lib/measured/rails/validations.rb
|
150
150
|
- lib/measured/rails/version.rb
|
151
151
|
- measured-rails.gemspec
|
152
|
-
- repodb.yml
|
153
152
|
- shipit.rubygems.yml
|
154
153
|
homepage: https://github.com/Shopify/measured-rails
|
155
154
|
licenses:
|
data/repodb.yml
DELETED