measured-rails 2.8.1 → 3.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7f5d42d6f68a07206f65244200601d778a45d98d2082fa6e3219ef6c0ef1d7a
4
- data.tar.gz: f6c02b4a808080c43694621964947b92ed3a943edc2c474a016459890e3d8e5a
3
+ metadata.gz: 9b55237558354338d84cefa5601b2598c56df76e505e9d242e634fbe0c29c06e
4
+ data.tar.gz: 4ebb417899eabda3cde19d8a2653cfeb1a58070891d31721ffa8dafffdc092a4
5
5
  SHA512:
6
- metadata.gz: 4fe858682b711f08f552b73c1cf0d2e56d7b89fa398ae7c36261178e7a1196e0f3e48e19e2104f263bc581c91b3e4a6c9a98a32f187f97818b4d503d98941b60
7
- data.tar.gz: 7ed2063fe8249390a14c2bdd455f4d99aecc2b0a630bbb8ebd8334f3bcfe493e383512b6f994e47420bccbec45dfff4369aa0688c86bbe963a7d60b42b6c9709
6
+ metadata.gz: c4bda2138bde85df71997975f97aaf475b846cc5dc544abe0a441a7b5fd59759b7c59aa6a226b37e3eb40b537e7571a61283c3e96e8b62e0119a51526ff9640b
7
+ data.tar.gz: 5789a4bf8aee26246f8c6517fd94e6fb3377cbb9d445c558eadbbb6ece4c7164718852aefe7bbbe95a2a00a5dea78a79fa8472d2be41760c91e140c0e3ba08fa
@@ -0,0 +1,23 @@
1
+ # .github/workflows/cla.yml
2
+ name: Contributor License Agreement (CLA)
3
+
4
+ on:
5
+ pull_request_target:
6
+ types: [opened, synchronize]
7
+ issue_comment:
8
+ types: [created]
9
+
10
+ jobs:
11
+ cla:
12
+ runs-on: ubuntu-latest
13
+ if: |
14
+ (github.event.issue.pull_request
15
+ && !github.event.issue.pull_request.merged_at
16
+ && contains(github.event.comment.body, 'signed')
17
+ )
18
+ || (github.event.pull_request && !github.event.pull_request.merged)
19
+ steps:
20
+ - uses: Shopify/shopify-cla-action@v1
21
+ with:
22
+ github-token: ${{ secrets.GITHUB_TOKEN }}
23
+ cla-token: ${{ secrets.CLA_TOKEN }}
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,7 +1,12 @@
1
- Unreleased
1
+ 3.0.0
2
2
  -----
3
3
 
4
+ * This gem is now deprecated. The functionality of this gem has been merged into the [measured](https://github.com/Shopify/measured).
4
5
 
6
+ 2.8.2
7
+ -----
8
+
9
+ * Bump measured to 2.8.2
5
10
 
6
11
  2.8.1
7
12
  -----
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rails'
5
+ gem "rake", "> 10.0"
data/README.md CHANGED
@@ -1,146 +1,10 @@
1
1
  # Measured Rails [![Build Status](https://travis-ci.org/Shopify/measured-rails.svg)](https://travis-ci.org/Shopify/measured-rails) [![Gem Version](https://badge.fury.io/rb/measured-rails.svg)](http://badge.fury.io/rb/measured-rails)
2
2
 
3
- This gem is the Rails integration for the [measured](https://github.com/Shopify/measured) gem.
3
+ Since version 3.0.0, the functionality of this gem has been merged into the [measured](https://github.com/Shopify/measured) gem. This gem no longer does anything useful and is not maintained.
4
4
 
5
- It provides ActiveRecord adapter for persisting and retrieving measurements with their units, and model validations.
5
+ If you are using this gem, you should switch to using the `measured` gem directly.
6
6
 
7
- ## Installation
8
-
9
- Using bundler, add to the Gemfile:
10
-
11
- ```ruby
12
- gem 'measured-rails'
13
- ```
14
-
15
- Or stand alone:
16
-
17
- $ gem install measured-rails
18
-
19
- ## Usage
20
-
21
- ### ActiveRecord
22
-
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
-
25
- ```ruby
26
- class AddWeightAndLengthToThings < ActiveRecord::Migration
27
- def change
28
- add_column :things, :minimum_weight_value, :decimal, precision: 10, scale: 2
29
- add_column :things, :minimum_weight_unit, :string, limit: 12
30
-
31
- add_column :things, :total_length_value, :decimal, precision: 10, scale: 2, default: 0
32
- add_column :things, :total_length_unit, :string, limit: 12, default: "cm"
33
- end
34
- end
35
- ```
36
-
37
- A column can be declared as a measurement with its measurement subclass:
38
-
39
- ```ruby
40
- class Thing < ActiveRecord::Base
41
- measured Measured::Weight, :minimum_weight
42
- measured Measured::Length, :total_length
43
- measured Measured::Volume, :total_volume
44
- end
45
- ```
46
-
47
- You can optionally customize the model's unit column by specifying it in the `unit_field_name` option, as follows:
48
-
49
- ```ruby
50
- class ThingWithCustomUnitAccessor < ActiveRecord::Base
51
- measured_length :length, :width, :height, unit_field_name: :size_unit
52
- measured_weight :total_weight, :extra_weight, unit_field_name: :weight_unit
53
- measured_volume :total_volume, :extra_volume, unit_field_name: :volume_unit
54
- end
55
- ```
56
-
57
- Similarly, you can optionally customize the model's value column by specifying it in the `value_field_name` option, as follows:
58
-
59
- ```ruby
60
- class ThingWithCustomValueAccessor < ActiveRecord::Base
61
- measured_length :length, value_field_name: :custom_length
62
- measured_weight :total_weight, value_field_name: :custom_weight
63
- measured_volume :volume, value_field_name: :custom_volume
64
- end
65
- ```
66
-
67
- There are some simpler methods for predefined types:
68
-
69
- ```ruby
70
- class Thing < ActiveRecord::Base
71
- measured_weight :minimum_weight
72
- measured_length :total_length
73
- measured_volume :total_volume
74
- end
75
- ```
76
-
77
- This will allow you to access and assign a measurement object:
78
-
79
- ```ruby
80
- thing = Thing.new
81
- thing.minimum_weight = Measured::Weight.new(10, "g")
82
- thing.minimum_weight_unit # "g"
83
- thing.minimum_weight_value # 10
84
- ```
85
-
86
- Order of assignment does not matter, and each property can be assigned separately and with mass assignment:
87
-
88
- ```ruby
89
- params = { total_length_unit: "cm", total_length_value: "3" }
90
- thing = Thing.new(params)
91
- thing.total_length.to_s # 3 cm
92
- ```
93
-
94
- ### Validations
95
-
96
- Validations are available:
97
-
98
- ```ruby
99
- class Thing < ActiveRecord::Base
100
- measured_length :total_length
101
-
102
- validates :total_length, measured: true
103
- end
104
- ```
105
-
106
- This will validate that the unit is defined on the measurement, and that there is a value.
107
-
108
- Rather than `true` the validation can accept a hash with the following options:
109
-
110
- * `message`: Override the default "is invalid" message.
111
- * `units`: A subset of units available for this measurement. Units must be in existing measurement.
112
- * `greater_than`
113
- * `greater_than_or_equal_to`
114
- * `equal_to`
115
- * `less_than`
116
- * `less_than_or_equal_to`
117
-
118
- All comparison validations require `Measured::Measurable` values, not scalars. Most of these options replace the `numericality` validator which compares the measurement/method name/proc to the column's value. Validations can also be combined with `presence` validator.
119
-
120
- **Note:** Validations are strongly recommended since assigning an invalid unit will cause the measurement to return `nil`, even if there is a value:
121
-
122
- ```ruby
123
- thing = Thing.new
124
- thing.total_length_value = 1
125
- thing.total_length_unit = "invalid"
126
- thing.total_length # nil
127
- ```
128
-
129
- ## Tests
130
-
131
- ```
132
- $ bundle exec rake test
133
- ```
134
-
135
- ## Contributing
136
-
137
- 1. Fork it ( https://github.com/Shopify/measured-rails/fork )
138
- 2. Create your feature branch (`git checkout -b my-new-feature`)
139
- 3. Commit your changes (`git commit -am 'Add some feature'`)
140
- 4. Push to the branch (`git push origin my-new-feature`)
141
- 5. Create a new Pull Request
142
-
143
- ## Authors
7
+ ## Original Authors
144
8
 
145
9
  * [Kevin McPhillips](https://github.com/kmcphillips) at [Shopify](http://shopify.com/careers)
146
10
  * [Sai Warang](https://github.com/cyprusad) at [Shopify](http://shopify.com/careers)
data/Rakefile CHANGED
@@ -2,20 +2,13 @@ require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
4
  $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
5
- require "measured/rails/version"
6
5
 
7
6
  task default: :test
8
7
 
9
- desc 'Run the test stuite'
8
+ desc 'Run the test suite'
10
9
  Rake::TestTask.new do |t|
11
10
  t.libs << "test"
12
11
  t.libs << "lib/**/*"
13
12
  t.test_files = FileList['test/**/*_test.rb']
14
13
  t.verbose = true
15
14
  end
16
-
17
- task tag: :build do
18
- system "git commit -m'Released version #{ Measured::VERSION }' --allow-empty"
19
- system "git tag -a v#{ Measured::VERSION } -m 'Tagging #{ Measured::VERSION }'"
20
- system "git push --tags"
21
- end
data/dev.yml CHANGED
@@ -1,8 +1,7 @@
1
1
  name: measured-rails
2
2
 
3
3
  up:
4
- - ruby:
5
- version: 3.0.2
4
+ - ruby
6
5
  - bundler
7
6
 
8
7
  commands:
@@ -1,6 +1,3 @@
1
1
  # frozen_string_literal: true
2
- require "measured/rails/base"
3
2
 
4
- require "measured/rails/units/length"
5
- require "measured/rails/units/weight"
6
- require "measured/rails/units/volume"
3
+ # don't define anything, this gem is a no-op
@@ -1,11 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'measured/rails/version'
5
-
6
2
  Gem::Specification.new do |spec|
7
3
  spec.name = "measured-rails"
8
- spec.version = Measured::Rails::VERSION
4
+ spec.version = "3.0.0"
9
5
  spec.authors = ["Kevin McPhillips"]
10
6
  spec.email = ["gems@shopify.com"]
11
7
  spec.summary = %q{Rails adaptor for measured}
@@ -26,16 +22,11 @@ Gem::Specification.new do |spec|
26
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
27
23
  spec.require_paths = ["lib"]
28
24
 
29
- spec.add_runtime_dependency "measured", Measured::Rails::VERSION
30
-
31
- spec.add_runtime_dependency "railties", ">= 5.2"
32
- spec.add_runtime_dependency "activemodel", ">= 5.2"
33
- spec.add_runtime_dependency "activerecord", ">= 5.2"
25
+ spec.post_install_message = <<~MSG
26
+ Since version 3.0.0, the functionality of the `measured-rails` gem has been
27
+ merged into the `measured` gem. This gem will no longer be maintained. Please
28
+ remove the gem from your `Gemfile` and replace it with just the `measured` gem.
29
+ MSG
34
30
 
35
- spec.add_development_dependency "rake", "> 10.0"
36
- spec.add_development_dependency "minitest", "> 5.5.1"
37
- spec.add_development_dependency "minitest-reporters"
38
- spec.add_development_dependency "mocha", ">= 1.4.0"
39
- spec.add_development_dependency "pry"
40
- spec.add_development_dependency "sqlite3"
31
+ spec.required_ruby_version = ">= 3.0"
41
32
  end
metadata CHANGED
@@ -1,155 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measured-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McPhillips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: measured
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '='
18
- - !ruby/object:Gem::Version
19
- version: 2.8.1
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '='
25
- - !ruby/object:Gem::Version
26
- version: 2.8.1
27
- - !ruby/object:Gem::Dependency
28
- name: railties
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '5.2'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '5.2'
41
- - !ruby/object:Gem::Dependency
42
- name: activemodel
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '5.2'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '5.2'
55
- - !ruby/object:Gem::Dependency
56
- name: activerecord
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '5.2'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '5.2'
69
- - !ruby/object:Gem::Dependency
70
- name: rake
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">"
74
- - !ruby/object:Gem::Version
75
- version: '10.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">"
81
- - !ruby/object:Gem::Version
82
- version: '10.0'
83
- - !ruby/object:Gem::Dependency
84
- name: minitest
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">"
88
- - !ruby/object:Gem::Version
89
- version: 5.5.1
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">"
95
- - !ruby/object:Gem::Version
96
- version: 5.5.1
97
- - !ruby/object:Gem::Dependency
98
- name: minitest-reporters
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: mocha
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: 1.4.0
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: 1.4.0
125
- - !ruby/object:Gem::Dependency
126
- name: pry
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: sqlite3
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
11
+ date: 2024-04-19 00:00:00.000000000 Z
12
+ dependencies: []
153
13
  description: Rails adapter for assigning and managing measurements with their units
154
14
  provided by the measured gem.
155
15
  email:
@@ -158,43 +18,27 @@ executables: []
158
18
  extensions: []
159
19
  extra_rdoc_files: []
160
20
  files:
161
- - ".github/workflows/ci.yml"
21
+ - ".github/workflows/cla.yml"
162
22
  - ".gitignore"
23
+ - ".ruby-version"
163
24
  - CHANGELOG.md
164
25
  - Gemfile
165
26
  - LICENSE
166
27
  - README.md
167
28
  - Rakefile
168
29
  - dev.yml
169
- - gemfiles/rails-5.2.gemfile
170
- - gemfiles/rails-6.0.gemfile
171
- - gemfiles/rails-6.1.gemfile
172
- - gemfiles/rails-edge.gemfile
173
30
  - lib/measured-rails.rb
174
- - lib/measured/rails/active_record.rb
175
- - lib/measured/rails/base.rb
176
- - lib/measured/rails/railtie.rb
177
- - lib/measured/rails/units/length.rb
178
- - lib/measured/rails/units/volume.rb
179
- - lib/measured/rails/units/weight.rb
180
- - lib/measured/rails/validations.rb
181
- - lib/measured/rails/version.rb
182
31
  - measured-rails.gemspec
183
32
  - shipit.rubygems.yml
184
- - test/active_record_test.rb
185
- - test/support/models/thing.rb
186
- - test/support/models/thing_with_custom_unit_accessor.rb
187
- - test/support/models/thing_with_custom_value_accessor.rb
188
- - test/support/models/validated_thing.rb
189
- - test/support/schema.rb
190
- - test/test_helper.rb
191
- - test/validation_test.rb
192
33
  homepage: https://github.com/Shopify/measured-rails
193
34
  licenses:
194
35
  - MIT
195
36
  metadata:
196
37
  allowed_push_host: https://rubygems.org
197
- post_install_message:
38
+ post_install_message: |
39
+ Since version 3.0.0, the functionality of the `measured-rails` gem has been
40
+ merged into the `measured` gem. This gem will no longer be maintained. Please
41
+ remove the gem from your `Gemfile` and replace it with just the `measured` gem.
198
42
  rdoc_options: []
199
43
  require_paths:
200
44
  - lib
@@ -202,23 +46,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
202
46
  requirements:
203
47
  - - ">="
204
48
  - !ruby/object:Gem::Version
205
- version: '0'
49
+ version: '3.0'
206
50
  required_rubygems_version: !ruby/object:Gem::Requirement
207
51
  requirements:
208
52
  - - ">="
209
53
  - !ruby/object:Gem::Version
210
54
  version: '0'
211
55
  requirements: []
212
- rubygems_version: 3.2.20
56
+ rubygems_version: 3.5.9
213
57
  signing_key:
214
58
  specification_version: 4
215
59
  summary: Rails adaptor for measured
216
- test_files:
217
- - test/active_record_test.rb
218
- - test/support/models/thing.rb
219
- - test/support/models/thing_with_custom_unit_accessor.rb
220
- - test/support/models/thing_with_custom_value_accessor.rb
221
- - test/support/models/validated_thing.rb
222
- - test/support/schema.rb
223
- - test/test_helper.rb
224
- - test/validation_test.rb
60
+ test_files: []
@@ -1,38 +0,0 @@
1
- name: CI
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- env:
9
- BUNDLE_GEMFILE: ${{ matrix.gemfile }}
10
- strategy:
11
- fail-fast: false
12
- matrix:
13
- ruby:
14
- - '2.6'
15
- - '2.7'
16
- - '3.0'
17
- gemfile:
18
- - Gemfile
19
- - gemfiles/rails-5.2.gemfile
20
- - gemfiles/rails-6.0.gemfile
21
- - gemfiles/rails-6.1.gemfile
22
- - gemfiles/rails-edge.gemfile
23
- exclude:
24
- - ruby: '2.6'
25
- gemfile: gemfiles/rails-edge.gemfile
26
- - ruby: '3.0'
27
- gemfile: gemfiles/rails-5.2.gemfile
28
- name: Ruby ${{ matrix.ruby }} ${{ matrix.gemfile }}
29
- steps:
30
- - uses: actions/checkout@v1
31
- - name: Set up Ruby ${{ matrix.ruby }}
32
- uses: ruby/setup-ruby@v1
33
- with:
34
- ruby-version: ${{ matrix.ruby }}
35
- bundler-cache: true
36
- - name: Run tests
37
- run: |
38
- bundle exec rake
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 5.2'
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 6.0'
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', '~> 6.1'
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'rails', github: 'rails/rails', branch: 'main'
@@ -1,96 +0,0 @@
1
- # frozen_string_literal: true
2
- module Measured::Rails::ActiveRecord
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def measured(measured_class, *fields)
7
- options = fields.extract_options!
8
- options = {}.merge(options)
9
-
10
- measured_class = measured_class.constantize if measured_class.is_a?(String)
11
- unless measured_class.is_a?(Class) && measured_class.ancestors.include?(Measured::Measurable)
12
- raise Measured::Rails::Error, "Expecting #{ measured_class } to be a subclass of Measured::Measurable"
13
- end
14
-
15
- options[:class] = measured_class
16
-
17
- fields.map(&:to_sym).each do |field|
18
- raise Measured::Rails::Error, "The field #{ field } has already been measured" if measured_fields.key?(field)
19
-
20
- measured_fields[field] = options
21
-
22
- unit_field_name = if options[:unit_field_name]
23
- measured_fields[field][:unit_field_name] = options[:unit_field_name].to_s
24
- else
25
- "#{ field }_unit"
26
- end
27
-
28
- value_field_name = if options[:value_field_name]
29
- measured_fields[field][:value_field_name] = options[:value_field_name].to_s
30
- else
31
- "#{ field }_value"
32
- end
33
-
34
- # Reader to retrieve measured object
35
- define_method(field) do
36
- value = public_send(value_field_name)
37
- unit = public_send(unit_field_name)
38
-
39
- return nil unless value && unit
40
-
41
- instance = instance_variable_get("@measured_#{ field }") if instance_variable_defined?("@measured_#{ field }")
42
- new_instance = begin
43
- measured_class.new(value, unit)
44
- rescue Measured::UnitError
45
- nil
46
- end
47
-
48
- if instance == new_instance
49
- instance
50
- else
51
- instance_variable_set("@measured_#{ field }", new_instance)
52
- end
53
- end
54
-
55
- # Writer to assign measured object
56
- define_method("#{ field }=") do |incoming|
57
- if incoming.is_a?(measured_class)
58
- instance_variable_set("@measured_#{ field }", incoming)
59
- precision = self.column_for_attribute(value_field_name).precision
60
- scale = self.column_for_attribute(value_field_name).scale
61
- rounded_to_scale_value = incoming.value.round(scale)
62
-
63
- max = self.class.measured_fields[field][:max_on_assignment]
64
- if max && rounded_to_scale_value > max
65
- rounded_to_scale_value = max
66
- elsif rounded_to_scale_value.to_i.to_s.length > (precision - scale)
67
- 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."
68
- end
69
-
70
- public_send("#{ value_field_name }=", rounded_to_scale_value)
71
- public_send("#{ unit_field_name }=", incoming.unit.name)
72
- else
73
- instance_variable_set("@measured_#{ field }", nil)
74
- public_send("#{ value_field_name}=", nil)
75
- public_send("#{ unit_field_name }=", nil)
76
- end
77
- end
78
-
79
- # Writer to override unit assignment
80
- redefine_method("#{ unit_field_name }=") do |incoming|
81
- unit_name = measured_class.unit_system.unit_for(incoming).try!(:name)
82
- write_attribute(unit_field_name, unit_name || incoming)
83
- end
84
- end
85
- end
86
-
87
- def measured_fields
88
- @measured_fields ||= {}
89
- end
90
-
91
- end
92
- end
93
-
94
- ActiveSupport.on_load(:active_record) do
95
- ::ActiveRecord::Base.send :include, Measured::Rails::ActiveRecord
96
- end