unit_measurements-rails 1.3.0 → 1.4.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/CHANGELOG.md +8 -0
- data/Gemfile.lock +2 -2
- data/README.md +34 -53
- data/lib/unit_measurements/rails/active_record.rb +20 -18
- data/lib/unit_measurements/rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8df42aea1bd4eeeb476acbf102accaa55c206886b581df3f70e055accae95d41
|
4
|
+
data.tar.gz: 8f1d81756ce2844917278e2c16f49665be362a427c3c1af3c00d8cc834b96f2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e36f8d160d0644c4c66bd5723cdabef25db28ca5cae522dff70cb4299ea59c942d5190d0badc2374456b59db07dca09ac4f7c51bb22f8be261f67c932322cc79
|
7
|
+
data.tar.gz: 8285500f4713be2467e28cb6a2e39605478babf072935a2a93f6de01dfe3338010fed463a094828f425d76a4c8c48124c09b68f1cdbb9ff9f13f3abbad2e5cb7
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## [1.4.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.3.0...v1.4.0) - 2023-11-25
|
2
|
+
|
3
|
+
### What's updated
|
4
|
+
|
5
|
+
- Updated RDoc and rearranged files.
|
6
|
+
|
7
|
+
-----------
|
8
|
+
|
1
9
|
## [1.3.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.2.0...v1.3.0) - 2023-11-22
|
2
10
|
|
3
11
|
### What's new
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unit_measurements-rails (1.
|
4
|
+
unit_measurements-rails (1.4.0)
|
5
5
|
activemodel (>= 7)
|
6
6
|
activerecord (>= 7)
|
7
7
|
railties (>= 7)
|
@@ -168,7 +168,7 @@ GEM
|
|
168
168
|
timeout (0.4.1)
|
169
169
|
tzinfo (2.0.6)
|
170
170
|
concurrent-ruby (~> 1.0)
|
171
|
-
unit_measurements (5.
|
171
|
+
unit_measurements (5.12.0)
|
172
172
|
activesupport (~> 7.0)
|
173
173
|
websocket-driver (0.7.6)
|
174
174
|
websocket-extensions (>= 0.1.0)
|
data/README.md
CHANGED
@@ -13,17 +13,18 @@ A Rails adaptor that encapsulate measurements and their units in Ruby on Rails.
|
|
13
13
|
|
14
14
|
## Introduction
|
15
15
|
|
16
|
-
This gem is
|
17
|
-
It provides ActiveRecord adapter for persisting and retrieving measurements with their units
|
16
|
+
This gem is designed as a Rails integration for the [unit_measurements](https://github.com/shivam091/unit_measurements) gem.
|
17
|
+
It provides an `ActiveRecord` adapter for persisting and retrieving measurements along with their units, simplifying complex
|
18
|
+
measurement handling within your Rails applications.
|
18
19
|
|
19
20
|
## Minimum Requirements
|
20
21
|
|
21
|
-
* Ruby 3.2+ (https://www.ruby-lang.org/en/downloads/branches/)
|
22
|
-
* Rails 7.0.0+ (https://rubygems.org/gems/rails/versions)
|
22
|
+
* Ruby 3.2+ ([Download Ruby](https://www.ruby-lang.org/en/downloads/branches/))
|
23
|
+
* Rails 7.0.0+ ([RubyGems - Rails Versions](https://rubygems.org/gems/rails/versions))
|
23
24
|
|
24
25
|
## Installation
|
25
26
|
|
26
|
-
|
27
|
+
To use `unit_measurements-rails` in your Rails application, add the following line to your Gemfile:
|
27
28
|
|
28
29
|
```ruby
|
29
30
|
gem "unit_measurements-rails"
|
@@ -41,102 +42,82 @@ Or otherwise simply install it yourself as:
|
|
41
42
|
|
42
43
|
### ActiveRecord
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
Customizing the accessors used to hold quantity and unit is supported, see below
|
48
|
-
for details.
|
45
|
+
This gem provides an ActiveRecord integration allowing you to declare measurement attributes with their
|
46
|
+
corresponding units in your database schema:
|
49
47
|
|
50
48
|
```ruby
|
51
|
-
class
|
49
|
+
class CreateCubes < ActiveRecord::Migration[7.0]
|
52
50
|
def change
|
53
51
|
create_table :cubes do |t|
|
54
52
|
t.decimal :length_quantity, precision: 10, scale: 2
|
55
53
|
t.string :length_unit, limit: 12
|
56
|
-
t.decimal :width_quantity, precision: 10, scale: 2
|
57
|
-
t.string :width_unit, limit: 12
|
58
|
-
t.decimal :height_quantity, precision: 10, scale: 2
|
59
|
-
t.string :height_unit, limit: 12
|
60
54
|
t.timestamps
|
61
55
|
end
|
62
56
|
end
|
63
57
|
end
|
64
58
|
```
|
65
59
|
|
66
|
-
|
60
|
+
Next, declare an atribute as measurement using `measured` with its associated unit
|
61
|
+
group class:
|
67
62
|
|
68
63
|
```ruby
|
69
64
|
class Cube < ActiveRecord::Base
|
70
|
-
measured UnitMeasurements::Length, :
|
65
|
+
measured UnitMeasurements::Length, :length
|
71
66
|
end
|
72
67
|
```
|
73
68
|
|
74
|
-
This
|
69
|
+
This setup allows you to access and assign measurement attributes conveniently:
|
75
70
|
|
76
71
|
```ruby
|
77
72
|
cube = Cube.new
|
78
|
-
cube.
|
79
|
-
cube.
|
80
|
-
cube.
|
81
|
-
cube.
|
73
|
+
cube.length = UnitMeasurements::Length.new(5, "ft")
|
74
|
+
cube.length_quantity #=> 0.5e1
|
75
|
+
cube.length_unit #=> "ft"
|
76
|
+
cube.length #=> 5.0 ft
|
82
77
|
```
|
83
78
|
|
84
|
-
|
85
|
-
and with mass assignment:
|
79
|
+
You can specify multiple measurement attributes simultaneously:
|
86
80
|
|
87
81
|
```ruby
|
88
|
-
|
89
|
-
|
90
|
-
cube.height #=> 3.0 ft
|
91
|
-
```
|
92
|
-
|
93
|
-
You can specify multiple measured attributes at a time:
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
class Land < ActiveRecord::Base
|
97
|
-
measured UnitMeasurements::Area, :carpet_area, :buildup_area
|
82
|
+
class Cube < ActiveRecord::Base
|
83
|
+
measured UnitMeasurements::Length, :length, :width
|
98
84
|
end
|
99
85
|
```
|
100
86
|
|
101
|
-
|
102
|
-
|
103
|
-
|
87
|
+
Attribute names are expected to have the `_quantity` and `_unit` suffix, and be
|
88
|
+
`DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.
|
89
|
+
|
90
|
+
You can customize the model's quantity and unit accessors by specifying them in the
|
91
|
+
`quantity_attribute_name` and `unit_attribute_name` options, respectively.
|
104
92
|
|
105
93
|
```ruby
|
106
94
|
class CubeWithCustomAccessor < ActiveRecord::Base
|
107
95
|
measured_length :length, unit_attribute_name: :length_uom
|
108
96
|
measured_length :width, quantity_attribute_name: :width_value
|
109
|
-
measured_length :height, quantity_attribute_name: :height_value, unit_attribute_name: :height_uom
|
110
97
|
end
|
111
98
|
```
|
112
99
|
|
113
|
-
|
100
|
+
For a more streamlined approach, predefined methods are available for commonly used
|
101
|
+
types like `length`, `weight`, `area`, `volume`, etc.:
|
114
102
|
|
115
103
|
```ruby
|
116
104
|
class Package < ActiveRecord::Base
|
117
105
|
measured_length :size
|
118
|
-
measured_weight :item_weight, :package_weight
|
119
106
|
measured_area :carpet_area
|
120
107
|
measured_volume :total_volume
|
108
|
+
measured_weight :item_weight, :package_weight
|
121
109
|
end
|
122
110
|
```
|
123
111
|
|
124
|
-
This will allow you to access and assign a measurement object:
|
125
|
-
|
126
|
-
```ruby
|
127
|
-
package = Package.new
|
128
|
-
package.item_weight = UnitMeasurements::Weight.new(10, "g")
|
129
|
-
package.item_weight_unit #=> "g"
|
130
|
-
package.item_weight_value #=> 10
|
131
|
-
```
|
132
|
-
|
133
112
|
## Contributing
|
134
113
|
|
135
|
-
|
136
|
-
|
114
|
+
Contributions to this project are welcomed! To contribute:
|
115
|
+
|
116
|
+
1. Fork this repository
|
117
|
+
2. Create a new branch (`git checkout -b my-new-feature`)
|
137
118
|
3. Commit your changes (`git commit -am "Add some feature"`)
|
138
|
-
4. Push to
|
139
|
-
5. Create new Pull Request
|
119
|
+
4. Push the changes to your branch (`git push origin my-new-feature`)
|
120
|
+
5. Create new **Pull Request**
|
140
121
|
|
141
122
|
## License
|
142
123
|
|
@@ -15,22 +15,22 @@ module UnitMeasurements
|
|
15
15
|
module Rails
|
16
16
|
# The +UnitMeasurements::Rails::ActiveRecord+ module enhances ActiveRecord
|
17
17
|
# models by providing a convenient way to handle unit measurements. It
|
18
|
-
# facilitates defining
|
18
|
+
# facilitates defining measurement attributes in models with specific unit
|
19
19
|
# group support.
|
20
20
|
#
|
21
21
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
22
22
|
# @since 1.0.0
|
23
23
|
module ActiveRecord
|
24
24
|
# @!scope class
|
25
|
-
# Defines a _reader_ and _writer_ methods for the
|
25
|
+
# Defines a _reader_ and _writer_ methods for the measurement attributes in
|
26
26
|
# the +ActiveRecord+ model.
|
27
27
|
#
|
28
|
-
# @example Defining single
|
28
|
+
# @example Defining single measurement attribute:
|
29
29
|
# class Cube < ActiveRecord::Base
|
30
30
|
# measured UnitMeasurements::Length, :height
|
31
31
|
# end
|
32
32
|
#
|
33
|
-
# @example Defining multiple
|
33
|
+
# @example Defining multiple measurement attributes:
|
34
34
|
# class Package < ActiveRecord::Base
|
35
35
|
# measured UnitMeasurements::Weight, :item_weight, :total_weight
|
36
36
|
# end
|
@@ -38,7 +38,7 @@ module UnitMeasurements
|
|
38
38
|
# @param [Class|String] unit_group
|
39
39
|
# The unit group class or its name as a string.
|
40
40
|
# @param [Array<String|Symbol>] measured_attrs
|
41
|
-
# An array of the names of
|
41
|
+
# An array of the names of measurement attributes.
|
42
42
|
# @param [Hash] options A customizable set of options
|
43
43
|
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
44
44
|
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
@@ -46,7 +46,8 @@ module UnitMeasurements
|
|
46
46
|
# @return [void]
|
47
47
|
#
|
48
48
|
# @raise [BaseError]
|
49
|
-
# If +unit_group+ is not a subclass of +UnitMeasurements::Measurement
|
49
|
+
# If +unit_group+ is not a subclass of +UnitMeasurements::Measurement+ or
|
50
|
+
# the attribute has already been measured.
|
50
51
|
#
|
51
52
|
# @see BaseError
|
52
53
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
@@ -60,7 +61,7 @@ module UnitMeasurements
|
|
60
61
|
options[:unit_group] = unit_group
|
61
62
|
|
62
63
|
measured_attrs.map(&:to_s).each do |measured_attr|
|
63
|
-
raise BaseError, "The
|
64
|
+
raise BaseError, "The attribute '#{measured_attr}' has already been measured." if measured_attributes.key?(measured_attr)
|
64
65
|
|
65
66
|
quantity_attr = options[:quantity_attribute_name]&.to_s || "#{measured_attr}_quantity"
|
66
67
|
unit_attr = options[:unit_attribute_name]&.to_s || "#{measured_attr}_unit"
|
@@ -75,12 +76,13 @@ module UnitMeasurements
|
|
75
76
|
end
|
76
77
|
|
77
78
|
# @!scope class
|
78
|
-
# Returns a hash containing information about the
|
79
|
-
# their options.
|
79
|
+
# Returns a hash containing information about the measurement attributes
|
80
|
+
# and their options.
|
80
81
|
#
|
81
82
|
# @return [Hash{String => Hash{Symbol => String|Class}}]
|
82
|
-
# A hash where keys represent the names of the
|
83
|
-
# values are hashes containing the options for each
|
83
|
+
# A hash where keys represent the names of the measurement attributes,
|
84
|
+
# and values are hashes containing the options for each measurement
|
85
|
+
# attribute.
|
84
86
|
#
|
85
87
|
# @example
|
86
88
|
# {
|
@@ -111,7 +113,7 @@ module UnitMeasurements
|
|
111
113
|
# @param [Class] unit_group The unit group class to be validated.
|
112
114
|
#
|
113
115
|
# @raise [BaseError]
|
114
|
-
# if
|
116
|
+
# if unit group is not a subclass of UnitMeasurements::Measurement.
|
115
117
|
#
|
116
118
|
# @return [void]
|
117
119
|
#
|
@@ -125,9 +127,9 @@ module UnitMeasurements
|
|
125
127
|
|
126
128
|
# @!scope class
|
127
129
|
# @private
|
128
|
-
# Defines the method to
|
130
|
+
# Defines the method to _read_ the measurement attribute.
|
129
131
|
#
|
130
|
-
# @param [String] measured_attr The name of the
|
132
|
+
# @param [String] measured_attr The name of the measurement attribute.
|
131
133
|
# @param [String] quantity_attr The name of the quantity attribute.
|
132
134
|
# @param [String] unit_attr The name of the unit attribute.
|
133
135
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -150,9 +152,9 @@ module UnitMeasurements
|
|
150
152
|
|
151
153
|
# @!scope class
|
152
154
|
# @private
|
153
|
-
# Defines the method to
|
155
|
+
# Defines the method to _write_ the measurement attribute.
|
154
156
|
#
|
155
|
-
# @param [String] measured_attr The name of the
|
157
|
+
# @param [String] measured_attr The name of the measurement attribute.
|
156
158
|
# @param [String] quantity_attr The name of the quantity attribute.
|
157
159
|
# @param [String] unit_attr The name of the unit attribute.
|
158
160
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -175,7 +177,7 @@ module UnitMeasurements
|
|
175
177
|
|
176
178
|
# @!scope class
|
177
179
|
# @private
|
178
|
-
# Redefines the
|
180
|
+
# Redefines the _writer_ method to set the quantity attribute.
|
179
181
|
#
|
180
182
|
# @param quantity_attr [String] The name of the quantity attribute.
|
181
183
|
#
|
@@ -199,7 +201,7 @@ module UnitMeasurements
|
|
199
201
|
|
200
202
|
# @!scope class
|
201
203
|
# @private
|
202
|
-
# Redefines the
|
204
|
+
# Redefines the _writer_ method to set the unit attribute.
|
203
205
|
#
|
204
206
|
# @param unit_attr [String] The name of the unit attribute.
|
205
207
|
# @param unit_group [Class] The unit group class for the measurement.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unit_measurements-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|