unit_measurements-rails 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +10 -3
- data/CHANGELOG.md +18 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +136 -15
- data/README.md +45 -1
- data/Rakefile +2 -0
- data/gemfiles/rails-7.0.gemfile +9 -0
- data/gemfiles/rails-7.1.gemfile +9 -0
- data/gemfiles/rails-edge.gemfile +9 -0
- data/lib/unit_measurements/rails/active_record.rb +159 -0
- data/lib/unit_measurements/rails/base.rb +37 -0
- data/lib/unit_measurements/rails/railtie.rb +20 -0
- data/lib/unit_measurements/rails/unit_groups/all.rb +3 -0
- data/lib/unit_measurements/rails/version.rb +1 -1
- data/lib/{unit_measurements/rails.rb → unit_measurements-rails.rb} +2 -6
- data/unit_measurements-rails.gemspec +7 -1
- metadata +66 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cddde8ae9d387f5c1710542cc3152c2621aae6b2f6c2622a0f0b993ebe315e55
|
4
|
+
data.tar.gz: d68ebeb0acb2dc387fec180d750626f27c345f21a6e65b8181e2de23d946a2af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 872c3102f9608149bc7d7983a67684bcbcaac6387a32efdefb1aeeb2214510f25ab00c617e97e2d99675c861d4f29abca2ee88e85f77f5d72abc941064e7c298
|
7
|
+
data.tar.gz: 04b8d2bdd7122990917ffdea05355d2fa7a23e57a91925ddc2b3088745d4a4b5af9042ed45f714d27558894ded5a43196e071f2ca373f5a563e791c97abe45d8
|
data/.github/workflows/main.yml
CHANGED
@@ -8,19 +8,26 @@ on:
|
|
8
8
|
- main
|
9
9
|
jobs:
|
10
10
|
build:
|
11
|
-
name: Ruby ${{ matrix.ruby }}
|
12
11
|
runs-on: ubuntu-latest
|
13
12
|
env:
|
14
13
|
RAILS_ENV: test
|
15
14
|
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
15
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
16
16
|
strategy:
|
17
|
+
fail-fast: false
|
17
18
|
matrix:
|
18
19
|
ruby:
|
19
|
-
- "3.2
|
20
|
+
- "3.2"
|
21
|
+
gemfile:
|
22
|
+
- Gemfile
|
23
|
+
- gemfiles/rails-7.0.gemfile
|
24
|
+
- gemfiles/rails-7.1.gemfile
|
25
|
+
- gemfiles/rails-edge.gemfile
|
26
|
+
name: Ruby ${{ matrix.ruby }} ${{ matrix.gemfile }}
|
20
27
|
steps:
|
21
28
|
- name: Checkout Repository
|
22
29
|
uses: actions/checkout@v3
|
23
|
-
- name: Set up Ruby
|
30
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
24
31
|
uses: ruby/setup-ruby@v1
|
25
32
|
with:
|
26
33
|
ruby-version: ${{ matrix.ruby }}
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
## [1.0.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.2.0...v1.0.0) - 2023-11-14
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added method `measured` to store and retrieve measurement object.
|
6
|
+
- Added RDoc for modules and classes.
|
7
|
+
|
8
|
+
-----------
|
9
|
+
|
10
|
+
## [0.2.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.1.0...v0.2.0) - 2023-11-12
|
11
|
+
|
12
|
+
### What's new
|
13
|
+
|
14
|
+
- Added gem skeleton and updated dependencies.
|
15
|
+
- Added test schema to prepare the database.
|
16
|
+
|
17
|
+
-----------
|
18
|
+
|
1
19
|
## 0.1.0 - 2023-11-08
|
2
20
|
|
3
21
|
### Initial release
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,35 +1,148 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unit_measurements-rails (
|
4
|
+
unit_measurements-rails (1.0.0)
|
5
|
+
activemodel (>= 7)
|
6
|
+
activerecord (>= 7)
|
7
|
+
railties (>= 7)
|
5
8
|
unit_measurements (~> 5)
|
6
9
|
|
7
10
|
GEM
|
8
11
|
remote: https://rubygems.org/
|
9
12
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
actioncable (7.0.8)
|
14
|
+
actionpack (= 7.0.8)
|
15
|
+
activesupport (= 7.0.8)
|
16
|
+
nio4r (~> 2.0)
|
17
|
+
websocket-driver (>= 0.6.1)
|
18
|
+
actionmailbox (7.0.8)
|
19
|
+
actionpack (= 7.0.8)
|
20
|
+
activejob (= 7.0.8)
|
21
|
+
activerecord (= 7.0.8)
|
22
|
+
activestorage (= 7.0.8)
|
23
|
+
activesupport (= 7.0.8)
|
24
|
+
mail (>= 2.7.1)
|
25
|
+
net-imap
|
26
|
+
net-pop
|
27
|
+
net-smtp
|
28
|
+
actionmailer (7.0.8)
|
29
|
+
actionpack (= 7.0.8)
|
30
|
+
actionview (= 7.0.8)
|
31
|
+
activejob (= 7.0.8)
|
32
|
+
activesupport (= 7.0.8)
|
33
|
+
mail (~> 2.5, >= 2.5.4)
|
34
|
+
net-imap
|
35
|
+
net-pop
|
36
|
+
net-smtp
|
37
|
+
rails-dom-testing (~> 2.0)
|
38
|
+
actionpack (7.0.8)
|
39
|
+
actionview (= 7.0.8)
|
40
|
+
activesupport (= 7.0.8)
|
41
|
+
rack (~> 2.0, >= 2.2.4)
|
42
|
+
rack-test (>= 0.6.3)
|
43
|
+
rails-dom-testing (~> 2.0)
|
44
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
45
|
+
actiontext (7.0.8)
|
46
|
+
actionpack (= 7.0.8)
|
47
|
+
activerecord (= 7.0.8)
|
48
|
+
activestorage (= 7.0.8)
|
49
|
+
activesupport (= 7.0.8)
|
50
|
+
globalid (>= 0.6.0)
|
51
|
+
nokogiri (>= 1.8.5)
|
52
|
+
actionview (7.0.8)
|
53
|
+
activesupport (= 7.0.8)
|
54
|
+
builder (~> 3.1)
|
55
|
+
erubi (~> 1.4)
|
56
|
+
rails-dom-testing (~> 2.0)
|
57
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
58
|
+
activejob (7.0.8)
|
59
|
+
activesupport (= 7.0.8)
|
60
|
+
globalid (>= 0.3.6)
|
61
|
+
activemodel (7.0.8)
|
62
|
+
activesupport (= 7.0.8)
|
63
|
+
activerecord (7.0.8)
|
64
|
+
activemodel (= 7.0.8)
|
65
|
+
activesupport (= 7.0.8)
|
66
|
+
activestorage (7.0.8)
|
67
|
+
actionpack (= 7.0.8)
|
68
|
+
activejob (= 7.0.8)
|
69
|
+
activerecord (= 7.0.8)
|
70
|
+
activesupport (= 7.0.8)
|
71
|
+
marcel (~> 1.0)
|
72
|
+
mini_mime (>= 1.1.0)
|
73
|
+
activesupport (7.0.8)
|
13
74
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
-
connection_pool (>= 2.2.5)
|
15
|
-
drb
|
16
75
|
i18n (>= 1.6, < 2)
|
17
76
|
minitest (>= 5.1)
|
18
|
-
mutex_m
|
19
77
|
tzinfo (~> 2.0)
|
20
|
-
|
21
|
-
bigdecimal (3.1.4)
|
78
|
+
builder (3.2.4)
|
22
79
|
byebug (11.1.3)
|
23
80
|
concurrent-ruby (1.2.2)
|
24
|
-
|
81
|
+
crass (1.0.6)
|
82
|
+
date (3.3.4)
|
25
83
|
diff-lcs (1.5.0)
|
26
84
|
docile (1.4.0)
|
27
|
-
|
28
|
-
|
85
|
+
erubi (1.12.0)
|
86
|
+
globalid (1.2.1)
|
87
|
+
activesupport (>= 6.1)
|
29
88
|
i18n (1.14.1)
|
30
89
|
concurrent-ruby (~> 1.0)
|
90
|
+
loofah (2.21.4)
|
91
|
+
crass (~> 1.0.2)
|
92
|
+
nokogiri (>= 1.12.0)
|
93
|
+
mail (2.8.1)
|
94
|
+
mini_mime (>= 0.1.1)
|
95
|
+
net-imap
|
96
|
+
net-pop
|
97
|
+
net-smtp
|
98
|
+
marcel (1.0.2)
|
99
|
+
method_source (1.0.0)
|
100
|
+
mini_mime (1.1.5)
|
31
101
|
minitest (5.20.0)
|
32
|
-
|
102
|
+
net-imap (0.4.4)
|
103
|
+
date
|
104
|
+
net-protocol
|
105
|
+
net-pop (0.1.2)
|
106
|
+
net-protocol
|
107
|
+
net-protocol (0.2.2)
|
108
|
+
timeout
|
109
|
+
net-smtp (0.4.0)
|
110
|
+
net-protocol
|
111
|
+
nio4r (2.5.9)
|
112
|
+
nokogiri (1.15.4-x86_64-linux)
|
113
|
+
racc (~> 1.4)
|
114
|
+
racc (1.7.3)
|
115
|
+
rack (2.2.8)
|
116
|
+
rack-test (2.1.0)
|
117
|
+
rack (>= 1.3)
|
118
|
+
rails (7.0.8)
|
119
|
+
actioncable (= 7.0.8)
|
120
|
+
actionmailbox (= 7.0.8)
|
121
|
+
actionmailer (= 7.0.8)
|
122
|
+
actionpack (= 7.0.8)
|
123
|
+
actiontext (= 7.0.8)
|
124
|
+
actionview (= 7.0.8)
|
125
|
+
activejob (= 7.0.8)
|
126
|
+
activemodel (= 7.0.8)
|
127
|
+
activerecord (= 7.0.8)
|
128
|
+
activestorage (= 7.0.8)
|
129
|
+
activesupport (= 7.0.8)
|
130
|
+
bundler (>= 1.15.0)
|
131
|
+
railties (= 7.0.8)
|
132
|
+
rails-dom-testing (2.2.0)
|
133
|
+
activesupport (>= 5.0.0)
|
134
|
+
minitest
|
135
|
+
nokogiri (>= 1.6)
|
136
|
+
rails-html-sanitizer (1.6.0)
|
137
|
+
loofah (~> 2.21)
|
138
|
+
nokogiri (~> 1.14)
|
139
|
+
railties (7.0.8)
|
140
|
+
actionpack (= 7.0.8)
|
141
|
+
activesupport (= 7.0.8)
|
142
|
+
method_source
|
143
|
+
rake (>= 12.2)
|
144
|
+
thor (~> 1.0)
|
145
|
+
zeitwerk (~> 2.5)
|
33
146
|
rake (13.1.0)
|
34
147
|
rspec (3.12.0)
|
35
148
|
rspec-core (~> 3.12.0)
|
@@ -44,26 +157,34 @@ GEM
|
|
44
157
|
diff-lcs (>= 1.2.0, < 2.0)
|
45
158
|
rspec-support (~> 3.12.0)
|
46
159
|
rspec-support (3.12.1)
|
47
|
-
ruby2_keywords (0.0.5)
|
48
160
|
simplecov (0.22.0)
|
49
161
|
docile (~> 1.1)
|
50
162
|
simplecov-html (~> 0.11)
|
51
163
|
simplecov_json_formatter (~> 0.1)
|
52
164
|
simplecov-html (0.12.3)
|
53
165
|
simplecov_json_formatter (0.1.4)
|
166
|
+
sqlite3 (1.6.8-x86_64-linux)
|
167
|
+
thor (1.3.0)
|
168
|
+
timeout (0.4.1)
|
54
169
|
tzinfo (2.0.6)
|
55
170
|
concurrent-ruby (~> 1.0)
|
56
|
-
unit_measurements (5.
|
171
|
+
unit_measurements (5.11.0)
|
57
172
|
activesupport (~> 7.0)
|
173
|
+
websocket-driver (0.7.6)
|
174
|
+
websocket-extensions (>= 0.1.0)
|
175
|
+
websocket-extensions (0.1.5)
|
176
|
+
zeitwerk (2.6.12)
|
58
177
|
|
59
178
|
PLATFORMS
|
60
179
|
x86_64-linux
|
61
180
|
|
62
181
|
DEPENDENCIES
|
63
182
|
byebug (~> 11)
|
183
|
+
rails
|
64
184
|
rake (~> 13.0)
|
65
185
|
rspec (~> 3.0)
|
66
186
|
simplecov (~> 0.21, >= 0.21.2)
|
187
|
+
sqlite3 (~> 1.6)
|
67
188
|
unit_measurements-rails!
|
68
189
|
|
69
190
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ It provides ActiveRecord adapter for persisting and retrieving measurements with
|
|
18
18
|
|
19
19
|
## Minimum Requirements
|
20
20
|
|
21
|
-
* Ruby 3.2
|
21
|
+
* Ruby 3.2+ (https://www.ruby-lang.org/en/downloads/branches/)
|
22
22
|
* Rails 7.0.0+ (https://rubygems.org/gems/rails/versions)
|
23
23
|
|
24
24
|
## Installation
|
@@ -37,6 +37,50 @@ Or otherwise simply install it yourself as:
|
|
37
37
|
|
38
38
|
`$ gem install unit_measurements-rails`
|
39
39
|
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
### ActiveRecord
|
43
|
+
|
44
|
+
Attribute names are expected to have the `_quantity` and `_unit` suffix, and be `DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class AddHeightToThings < ActiveRecord::Migration[7.0]
|
48
|
+
def change
|
49
|
+
add_column :things, :height_quantity, :decimal, precision: 10, scale: 2
|
50
|
+
add_column :things, :height_unit, :string, limit: 12
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
A column can be declared as a measurement with its unit group class:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Thing < ActiveRecord::Base
|
59
|
+
measured UnitMeasurements::Length, :height
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
This will allow you to access and assign a measurement object:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
thing = Thing.new
|
67
|
+
thing.height = UnitMeasurements::Length.new(5, "ft")
|
68
|
+
thing.height_quantity
|
69
|
+
#=> 0.5e1
|
70
|
+
thing.height_unit
|
71
|
+
#=> "ft"
|
72
|
+
thing.height
|
73
|
+
#=> 5.0 ft
|
74
|
+
```
|
75
|
+
|
76
|
+
Order of assignment does not matter, and each attribute can be assigned separately and with mass assignment:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
params = {height_quantity: "3", height_unit: "ft"}
|
80
|
+
thing = Thing.new(params)
|
81
|
+
thing.height
|
82
|
+
#=> 3.0 ft
|
83
|
+
```
|
40
84
|
|
41
85
|
## Contributing
|
42
86
|
|
data/Rakefile
CHANGED
@@ -0,0 +1,159 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
module Rails
|
7
|
+
# The +UnitMeasurements::Rails::ActiveRecord+ module enhances ActiveRecord
|
8
|
+
# models by providing a convenient way to handle unit measurements. It allows
|
9
|
+
# you to define measurement attributes in your models with support for
|
10
|
+
# specific unit groups.
|
11
|
+
#
|
12
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
13
|
+
# @since 1.0.0
|
14
|
+
module ActiveRecord
|
15
|
+
# Defines a reader and writer methods for the measurement attribute in the
|
16
|
+
# +ActiveRecord+ model.
|
17
|
+
#
|
18
|
+
# @example Defining measured attributes:
|
19
|
+
# class Thing < ActiveRecord::Base
|
20
|
+
# measured UnitMeasurements::Length, :height
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# @param [Class|String] unit_group
|
24
|
+
# The unit group class or its name as a string.
|
25
|
+
# @param [String|Symbol] measurement_attr
|
26
|
+
# The name of the measurement attribute.
|
27
|
+
# @return [void]
|
28
|
+
#
|
29
|
+
# @raise [BaseError]
|
30
|
+
# if unit_group is not a subclass of UnitMeasurements::Measurement.
|
31
|
+
#
|
32
|
+
# @see BaseError
|
33
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
34
|
+
# @since 1.0.0
|
35
|
+
def measured(unit_group, measurement_attr)
|
36
|
+
unit_group = unit_group.constantize if unit_group.is_a?(String)
|
37
|
+
|
38
|
+
validate_unit_group!(unit_group)
|
39
|
+
|
40
|
+
quantity_attr = "#{measurement_attr}_quantity"
|
41
|
+
unit_attr = "#{measurement_attr}_unit"
|
42
|
+
|
43
|
+
define_measurement_reader(measurement_attr, quantity_attr, unit_attr, unit_group)
|
44
|
+
define_measurement_writer(measurement_attr, quantity_attr, unit_attr, unit_group)
|
45
|
+
redefine_quantity_writer(quantity_attr)
|
46
|
+
redefine_unit_writer(unit_attr, unit_group)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# Validates whether the +unit_group+ is a subclass of +UnitMeasurements::Measurement+.
|
52
|
+
#
|
53
|
+
# @param [Class] unit_group The unit group class to be validated.
|
54
|
+
#
|
55
|
+
# @raise [BaseError]
|
56
|
+
# if unit_group is not a subclass of UnitMeasurements::Measurement.
|
57
|
+
#
|
58
|
+
# @return [void]
|
59
|
+
#
|
60
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
61
|
+
# @since 1.0.0
|
62
|
+
def validate_unit_group!(unit_group)
|
63
|
+
unless unit_group.is_a?(Class) && unit_group.ancestors.include?(Measurement)
|
64
|
+
raise BaseError, "Expecting `#{unit_group}` to be a subclass of UnitMeasurements::Measurement"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Defines the method to read measurement attribute.
|
69
|
+
#
|
70
|
+
# @param [String|Symbol] measurement_attr The name of the measurement attribute.
|
71
|
+
# @param [String] quantity_attr The name of the quantity attribute.
|
72
|
+
# @param [String] unit_attr The name of the unit attribute.
|
73
|
+
# @param [Class] unit_group The unit group class for the measurement.
|
74
|
+
#
|
75
|
+
# @return [void]
|
76
|
+
#
|
77
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
78
|
+
# @since 1.0.0
|
79
|
+
def define_measurement_reader(measurement_attr, quantity_attr, unit_attr, unit_group)
|
80
|
+
define_method(measurement_attr) do
|
81
|
+
quantity, unit = public_send(quantity_attr), public_send(unit_attr)
|
82
|
+
|
83
|
+
begin
|
84
|
+
unit_group.new(quantity, unit)
|
85
|
+
rescue BlankQuantityError, BlankUnitError, ParseError, UnitError
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Defines the method to write measurement attribute.
|
92
|
+
#
|
93
|
+
# @param [String|Symbol] measurement_attr The name of the measurement attribute.
|
94
|
+
# @param [String] quantity_attr The name of the quantity attribute.
|
95
|
+
# @param [String] unit_attr The name of the unit attribute.
|
96
|
+
# @param [Class] unit_group The unit group class for the measurement.
|
97
|
+
#
|
98
|
+
# @return [void]
|
99
|
+
#
|
100
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
101
|
+
# @since 1.0.0
|
102
|
+
def define_measurement_writer(measurement_attr, quantity_attr, unit_attr, unit_group)
|
103
|
+
define_method("#{measurement_attr}=") do |measurement|
|
104
|
+
if measurement.is_a?(unit_group)
|
105
|
+
public_send("#{quantity_attr}=", measurement.quantity)
|
106
|
+
public_send("#{unit_attr}=", measurement.unit.name)
|
107
|
+
else
|
108
|
+
public_send("#{quantity_attr}=", nil)
|
109
|
+
public_send("#{unit_attr}=", nil)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Redefines the writer method to set quantity attribute.
|
115
|
+
#
|
116
|
+
# @param quantity_attr [String] The name of the quantity attribute.
|
117
|
+
#
|
118
|
+
# @return [void]
|
119
|
+
#
|
120
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
121
|
+
# @since 1.0.0
|
122
|
+
def redefine_quantity_writer(quantity_attr)
|
123
|
+
redefine_method("#{quantity_attr}=") do |quantity|
|
124
|
+
quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
|
125
|
+
quantity = if quantity
|
126
|
+
db_column_props = self.column_for_attribute(quantity_attr)
|
127
|
+
precision, scale = db_column_props.precision, db_column_props.scale
|
128
|
+
|
129
|
+
quantity.round(scale)
|
130
|
+
else
|
131
|
+
nil
|
132
|
+
end
|
133
|
+
|
134
|
+
write_attribute(quantity_attr, quantity)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Redefines the writer method to set unit attribute.
|
139
|
+
#
|
140
|
+
# @param unit_attr [String] The name of the unit attribute.
|
141
|
+
# @param unit_group [Class] The unit group class for the measurement.
|
142
|
+
#
|
143
|
+
# @return [void]
|
144
|
+
#
|
145
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
146
|
+
# @since 1.0.0
|
147
|
+
def redefine_unit_writer(unit_attr, unit_group)
|
148
|
+
redefine_method("#{unit_attr}=") do |unit|
|
149
|
+
unit_name = unit_group.unit_group.unit_for(unit).try!(:name)
|
150
|
+
write_attribute(unit_attr, (unit_name || unit))
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
ActiveSupport.on_load(:active_record) do
|
158
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord
|
159
|
+
end
|
@@ -4,3 +4,40 @@
|
|
4
4
|
|
5
5
|
require "unit_measurements/rails/version"
|
6
6
|
require "unit_measurements"
|
7
|
+
|
8
|
+
require "active_support/all"
|
9
|
+
require "active_record"
|
10
|
+
require "active_model"
|
11
|
+
require "active_model/validations"
|
12
|
+
|
13
|
+
# The +UnitMeasurements::Rails+ module provides functionality related to handling
|
14
|
+
# unit measurements within the Rails framework.
|
15
|
+
#
|
16
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
17
|
+
# @since 0.1.0
|
18
|
+
module UnitMeasurements
|
19
|
+
# The +Rails+ module within +UnitMeasurements+ is dedicated to integrating
|
20
|
+
# {unit_measurements}[https://github.com/shivam091/unit_measurements] into
|
21
|
+
# Ruby on Rails applications.
|
22
|
+
#
|
23
|
+
# It includes modules and classes for ActiveRecord support, Railties, and custom
|
24
|
+
# errors.
|
25
|
+
#
|
26
|
+
# @see ActiveRecord
|
27
|
+
# @see Railtie
|
28
|
+
# @see BaseError
|
29
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
30
|
+
# @since 0.2.0
|
31
|
+
module Rails
|
32
|
+
# This is the base class for custom errors in the +UnitMeasurements::Rails+
|
33
|
+
# module.
|
34
|
+
#
|
35
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
36
|
+
# @since 0.2.0
|
37
|
+
class BaseError < StandardError; end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
require "unit_measurements/rails/active_record"
|
42
|
+
|
43
|
+
require "unit_measurements/rails/railtie" if defined?(Rails)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
module Rails
|
7
|
+
# The +Railtie+ class for integrating +unit_measurements+ with the +Rails+
|
8
|
+
# framework.
|
9
|
+
#
|
10
|
+
# This Railtie is designed to be automatically loaded by Rails when the
|
11
|
+
# application starts. It can be used to configure and customize the behavior
|
12
|
+
# of +unit_measurements+ in a Rails application.
|
13
|
+
#
|
14
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
15
|
+
# @since 0.1.0
|
16
|
+
class Railtie < ::Rails::Railtie
|
17
|
+
# (optional: add custom configuration, initialization, or other hooks)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -2,10 +2,6 @@
|
|
2
2
|
# -*- frozen_string_literal: true -*-
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
|
-
module UnitMeasurements
|
6
|
-
module Rails
|
7
|
-
class BaseError < StandardError; end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
5
|
require "unit_measurements/rails/base"
|
6
|
+
|
7
|
+
require "unit_measurements/rails/unit_groups/all"
|
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.summary = "Rails adaptor for unit_measurements"
|
18
18
|
spec.homepage = "https://github.com/shivam091/unit_measurements-rails"
|
19
19
|
spec.license = "MIT"
|
20
|
-
spec.required_ruby_version = ">= 3.2.2"
|
21
20
|
|
22
21
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
23
22
|
|
@@ -34,10 +33,17 @@ Gem::Specification.new do |spec|
|
|
34
33
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
35
34
|
spec.require_paths = ["lib"]
|
36
35
|
|
36
|
+
spec.add_runtime_dependency "railties", ">= 7"
|
37
|
+
spec.add_runtime_dependency "activemodel", ">= 7"
|
38
|
+
spec.add_runtime_dependency "activerecord", ">= 7"
|
39
|
+
|
37
40
|
spec.add_runtime_dependency "unit_measurements", "~> 5"
|
38
41
|
|
39
42
|
spec.add_development_dependency "rake", "~> 13.0"
|
40
43
|
spec.add_development_dependency "rspec", "~> 3.0"
|
41
44
|
spec.add_development_dependency "simplecov", "~> 0.21", ">= 0.21.2"
|
42
45
|
spec.add_development_dependency "byebug", "~> 11"
|
46
|
+
spec.add_development_dependency "sqlite3", "~> 1.6"
|
47
|
+
|
48
|
+
spec.required_ruby_version = ">= 3.2"
|
43
49
|
end
|
metadata
CHANGED
@@ -1,15 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unit_measurements-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harshal LADHE
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '7'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '7'
|
13
55
|
- !ruby/object:Gem::Dependency
|
14
56
|
name: unit_measurements
|
15
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,6 +128,20 @@ dependencies:
|
|
86
128
|
- - "~>"
|
87
129
|
- !ruby/object:Gem::Version
|
88
130
|
version: '11'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: sqlite3
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '1.6'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - "~>"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '1.6'
|
89
145
|
description: A Rails adaptor that encapsulate measurements and their units in Ruby
|
90
146
|
on Rails.
|
91
147
|
email:
|
@@ -105,8 +161,14 @@ files:
|
|
105
161
|
- LICENSE.md
|
106
162
|
- README.md
|
107
163
|
- Rakefile
|
108
|
-
-
|
164
|
+
- gemfiles/rails-7.0.gemfile
|
165
|
+
- gemfiles/rails-7.1.gemfile
|
166
|
+
- gemfiles/rails-edge.gemfile
|
167
|
+
- lib/unit_measurements-rails.rb
|
168
|
+
- lib/unit_measurements/rails/active_record.rb
|
109
169
|
- lib/unit_measurements/rails/base.rb
|
170
|
+
- lib/unit_measurements/rails/railtie.rb
|
171
|
+
- lib/unit_measurements/rails/unit_groups/all.rb
|
110
172
|
- lib/unit_measurements/rails/version.rb
|
111
173
|
- unit_measurements-rails.gemspec
|
112
174
|
homepage: https://github.com/shivam091/unit_measurements-rails
|
@@ -127,7 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
189
|
requirements:
|
128
190
|
- - ">="
|
129
191
|
- !ruby/object:Gem::Version
|
130
|
-
version: 3.2
|
192
|
+
version: '3.2'
|
131
193
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
194
|
requirements:
|
133
195
|
- - ">="
|