unit_measurements-rails 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -1
- data/Gemfile.lock +1 -1
- data/README.md +67 -19
- data/lib/unit_measurements/rails/active_record.rb +100 -36
- data/lib/unit_measurements/rails/base.rb +6 -11
- data/lib/unit_measurements/rails/railtie.rb +3 -4
- data/lib/unit_measurements/rails/unit_groups/all.rb +5 -0
- data/lib/unit_measurements/rails/unit_groups/area.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/length.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/volume.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/weight.rb +50 -0
- data/lib/unit_measurements/rails/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5315caeda4fd8354ecf8f0ee7f50778725827ce96a176f0c5164270a81e08fca
|
4
|
+
data.tar.gz: d5e6ad2b8f041e6fe0a43cd9f2f52b9339a9383cc99ee124155b8ef18c1dcc43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e6a3b59d61493150ff20625a1a800864094e91d2378f7c3a9b12a43fd4319283aadc6d64d893e42566d910095e7bd9b2151b52b14b3f3f9a91c7b011ae60276
|
7
|
+
data.tar.gz: d3e835f26dbaff425180d40e309dadb8d474001829d80e89998d020d847091e985fcde5f16b8e41937ffd4e4ae8caca71c156d04b680bd1cb527a703592e9cfd
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,28 @@
|
|
1
|
+
## [1.2.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.1.0...v1.2.0) - 2023-11-20
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added method `.measured_fields` to provide ability to maintain measured attribute names and their options.
|
6
|
+
|
7
|
+
-----------
|
8
|
+
|
9
|
+
## [1.1.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.0.0...v1.1.0) - 2023-11-16
|
10
|
+
|
11
|
+
### What's new
|
12
|
+
|
13
|
+
- Added ability to define _multiple_ measured attributes at a time.
|
14
|
+
- Added wrapper for defining `length` measured attributes.
|
15
|
+
- Added wrapper for defining `weight` measured attributes.
|
16
|
+
- Added wrapper for defining `volume` measured attributes.
|
17
|
+
- Added wrapper for defining `area` measured attributes.
|
18
|
+
|
19
|
+
-----------
|
20
|
+
|
1
21
|
## [1.0.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.2.0...v1.0.0) - 2023-11-14
|
2
22
|
|
3
23
|
### What's new
|
4
24
|
|
5
|
-
- Added method
|
25
|
+
- Added method `.measured` to store and retrieve measurement object.
|
6
26
|
- Added RDoc for modules and classes.
|
7
27
|
|
8
28
|
-----------
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -41,45 +41,93 @@ Or otherwise simply install it yourself as:
|
|
41
41
|
|
42
42
|
### ActiveRecord
|
43
43
|
|
44
|
-
Attribute names are expected to have the `_quantity` and `_unit` suffix, and be
|
44
|
+
Attribute names are expected to have the `_quantity` and `_unit` suffix, and be
|
45
|
+
`DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.
|
46
|
+
|
47
|
+
Customizing the accessors used to hold quantity and unit is supported, see below
|
48
|
+
for details.
|
45
49
|
|
46
50
|
```ruby
|
47
|
-
class
|
51
|
+
class CreateTableCubes < ActiveRecord::Migration[7.0]
|
48
52
|
def change
|
49
|
-
|
50
|
-
|
53
|
+
create_table :cubes do |t|
|
54
|
+
t.decimal :length_quantity, precision: 10, scale: 2
|
55
|
+
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
|
+
t.timestamps
|
61
|
+
end
|
51
62
|
end
|
52
63
|
end
|
53
64
|
```
|
54
65
|
|
55
|
-
A column can be declared as a
|
66
|
+
A column can be declared as a measured with its unit group class:
|
56
67
|
|
57
68
|
```ruby
|
58
|
-
class
|
69
|
+
class Cube < ActiveRecord::Base
|
59
70
|
measured UnitMeasurements::Length, :height
|
60
71
|
end
|
61
72
|
```
|
62
73
|
|
63
|
-
This will allow you to access and assign a
|
74
|
+
This will allow you to access and assign a measured attribute:
|
64
75
|
|
65
76
|
```ruby
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
#=>
|
70
|
-
|
71
|
-
#=> "ft"
|
72
|
-
thing.height
|
73
|
-
#=> 5.0 ft
|
77
|
+
cube = Cube.new
|
78
|
+
cube.height = UnitMeasurements::Length.new(5, "ft")
|
79
|
+
cube.height_quantity #=> 0.5e1
|
80
|
+
cube.height_unit #=> "ft"
|
81
|
+
cube.height #=> 5.0 ft
|
74
82
|
```
|
75
83
|
|
76
|
-
Order of assignment does not matter, and each attribute can be assigned separately
|
84
|
+
Order of assignment does not matter, and each attribute can be assigned separately
|
85
|
+
and with mass assignment:
|
77
86
|
|
78
87
|
```ruby
|
79
88
|
params = {height_quantity: "3", height_unit: "ft"}
|
80
|
-
|
81
|
-
|
82
|
-
|
89
|
+
cube = Cube.new(params)
|
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
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
You can optionally customize the model's quantity and unit accessors by specifying
|
102
|
+
them in the `quantity_attribute_name` and `unit_attribute_name` option, respectively,
|
103
|
+
as follows:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class CubeWithCustomAccessor < ActiveRecord::Base
|
107
|
+
measured_length :length, unit_attribute_name: :length_uom
|
108
|
+
measured_length :width, quantity_attribute_name: :width_value
|
109
|
+
measured_length :height, quantity_attribute_name: :height_value, unit_attribute_name: :height_uom
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
There are some simpler methods for predefined types:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
class Package < ActiveRecord::Base
|
117
|
+
measured_length :size
|
118
|
+
measured_weight :item_weight, :package_weight
|
119
|
+
measured_area :carpet_area
|
120
|
+
measured_volume :total_volume
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
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
|
83
131
|
```
|
84
132
|
|
85
133
|
## Contributing
|
@@ -2,58 +2,114 @@
|
|
2
2
|
# -*- frozen_string_literal: true -*-
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
|
+
# The +UnitMeasurements+ module provides functionality for handling unit
|
6
|
+
# measurements. It includes various classes and modules for persisting and
|
7
|
+
# retrieving measurements with their units.
|
8
|
+
#
|
9
|
+
# The module also offers support for integrating with Rails ActiveRecord models
|
10
|
+
# for handling unit measurements conveniently.
|
11
|
+
#
|
12
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
13
|
+
# @since 1.0.0
|
5
14
|
module UnitMeasurements
|
6
15
|
module Rails
|
7
16
|
# The +UnitMeasurements::Rails::ActiveRecord+ module enhances ActiveRecord
|
8
|
-
# models by providing a convenient way to handle unit measurements. It
|
9
|
-
#
|
10
|
-
#
|
17
|
+
# models by providing a convenient way to handle unit measurements. It
|
18
|
+
# facilitates defining measurable attributes in models with specific unit
|
19
|
+
# group support.
|
11
20
|
#
|
12
21
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
13
22
|
# @since 1.0.0
|
14
23
|
module ActiveRecord
|
15
|
-
#
|
16
|
-
#
|
24
|
+
# @!scope class
|
25
|
+
# Defines a _reader_ and _writer_ methods for the measured attributes in
|
26
|
+
# the +ActiveRecord+ model.
|
17
27
|
#
|
18
|
-
# @example Defining measured
|
19
|
-
# class
|
28
|
+
# @example Defining single measured attribute:
|
29
|
+
# class Cube < ActiveRecord::Base
|
20
30
|
# measured UnitMeasurements::Length, :height
|
21
31
|
# end
|
22
32
|
#
|
33
|
+
# @example Defining multiple measured attributes:
|
34
|
+
# class Package < ActiveRecord::Base
|
35
|
+
# measured UnitMeasurements::Weight, :item_weight, :total_weight
|
36
|
+
# end
|
37
|
+
#
|
23
38
|
# @param [Class|String] unit_group
|
24
39
|
# The unit group class or its name as a string.
|
25
|
-
# @param [String|Symbol]
|
26
|
-
#
|
40
|
+
# @param [Array<String|Symbol>] measured_attrs
|
41
|
+
# An array of the names of measured attributes.
|
42
|
+
# @param [Hash] options A customizable set of options
|
43
|
+
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
44
|
+
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
45
|
+
#
|
27
46
|
# @return [void]
|
28
47
|
#
|
29
48
|
# @raise [BaseError]
|
30
|
-
#
|
49
|
+
# If +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
31
50
|
#
|
32
51
|
# @see BaseError
|
33
52
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
34
53
|
# @since 1.0.0
|
35
|
-
def measured(unit_group,
|
54
|
+
def measured(unit_group, *measured_attrs, **options)
|
55
|
+
validate_unit_group!(unit_group)
|
56
|
+
|
57
|
+
options = options.reverse_merge(quantity_attribute_name: nil, unit_attribute_name: nil)
|
36
58
|
unit_group = unit_group.constantize if unit_group.is_a?(String)
|
37
59
|
|
38
|
-
|
60
|
+
options[:unit_group] = unit_group
|
61
|
+
|
62
|
+
measured_attrs.map(&:to_s).each do |measured_attr|
|
63
|
+
quantity_attr = options[:quantity_attribute_name]&.to_s || "#{measured_attr}_quantity"
|
64
|
+
unit_attr = options[:unit_attribute_name]&.to_s || "#{measured_attr}_unit"
|
39
65
|
|
40
|
-
|
41
|
-
unit_attr = "#{measurement_attr}_unit"
|
66
|
+
measured_attributes[measured_attr] = options.merge(quantity_attribute_name: quantity_attr, unit_attribute_name: unit_attr)
|
42
67
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
68
|
+
define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
69
|
+
define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
70
|
+
redefine_quantity_writer(quantity_attr)
|
71
|
+
redefine_unit_writer(unit_attr, unit_group)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# @!scope class
|
76
|
+
# Returns a hash containing information about the measured attributes and
|
77
|
+
# their options.
|
78
|
+
#
|
79
|
+
# @return [Hash{String => Hash{Symbol => String|Class}}]
|
80
|
+
# A hash where keys represent the names of the measured attributes, and
|
81
|
+
# values are hashes containing the options for each measured attribute.
|
82
|
+
#
|
83
|
+
# @example
|
84
|
+
# {
|
85
|
+
# "height" => {
|
86
|
+
# unit_group: UnitMeasurements::Length,
|
87
|
+
# quantity_attribute_name: "height_quantity",
|
88
|
+
# unit_attribute_name: "height_unit"
|
89
|
+
# },
|
90
|
+
# "weight" => {
|
91
|
+
# unit_group: UnitMeasurements::Length,
|
92
|
+
# quantity_attribute_name: "weight_quantity",
|
93
|
+
# unit_attribute_name: "weight_unit"
|
94
|
+
# }
|
95
|
+
# }
|
96
|
+
#
|
97
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
98
|
+
# @since 1.2.0
|
99
|
+
def measured_attributes
|
100
|
+
@measured_attributes ||= {}
|
47
101
|
end
|
48
102
|
|
49
103
|
private
|
50
104
|
|
51
|
-
#
|
105
|
+
# @!scope class
|
106
|
+
# @private
|
107
|
+
# Validates whether +unit_group+ is a subclass of +UnitMeasurements::Measurement+.
|
52
108
|
#
|
53
109
|
# @param [Class] unit_group The unit group class to be validated.
|
54
110
|
#
|
55
111
|
# @raise [BaseError]
|
56
|
-
# if unit_group is not a subclass of UnitMeasurements::Measurement
|
112
|
+
# if +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
57
113
|
#
|
58
114
|
# @return [void]
|
59
115
|
#
|
@@ -65,9 +121,11 @@ module UnitMeasurements
|
|
65
121
|
end
|
66
122
|
end
|
67
123
|
|
68
|
-
#
|
124
|
+
# @!scope class
|
125
|
+
# @private
|
126
|
+
# Defines the method to read the measured attribute.
|
69
127
|
#
|
70
|
-
# @param [String
|
128
|
+
# @param [String] measured_attr The name of the measured attribute.
|
71
129
|
# @param [String] quantity_attr The name of the quantity attribute.
|
72
130
|
# @param [String] unit_attr The name of the unit attribute.
|
73
131
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -76,8 +134,8 @@ module UnitMeasurements
|
|
76
134
|
#
|
77
135
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
78
136
|
# @since 1.0.0
|
79
|
-
def
|
80
|
-
define_method(
|
137
|
+
def define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
138
|
+
define_method(measured_attr) do
|
81
139
|
quantity, unit = public_send(quantity_attr), public_send(unit_attr)
|
82
140
|
|
83
141
|
begin
|
@@ -88,9 +146,11 @@ module UnitMeasurements
|
|
88
146
|
end
|
89
147
|
end
|
90
148
|
|
91
|
-
#
|
149
|
+
# @!scope class
|
150
|
+
# @private
|
151
|
+
# Defines the method to write the measured attribute.
|
92
152
|
#
|
93
|
-
# @param [String
|
153
|
+
# @param [String] measured_attr The name of the measured attribute.
|
94
154
|
# @param [String] quantity_attr The name of the quantity attribute.
|
95
155
|
# @param [String] unit_attr The name of the unit attribute.
|
96
156
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -99,8 +159,8 @@ module UnitMeasurements
|
|
99
159
|
#
|
100
160
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
101
161
|
# @since 1.0.0
|
102
|
-
def
|
103
|
-
define_method("#{
|
162
|
+
def define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
163
|
+
define_method("#{measured_attr}=") do |measurement|
|
104
164
|
if measurement.is_a?(unit_group)
|
105
165
|
public_send("#{quantity_attr}=", measurement.quantity)
|
106
166
|
public_send("#{unit_attr}=", measurement.unit.name)
|
@@ -111,7 +171,9 @@ module UnitMeasurements
|
|
111
171
|
end
|
112
172
|
end
|
113
173
|
|
114
|
-
#
|
174
|
+
# @!scope class
|
175
|
+
# @private
|
176
|
+
# Redefines the writer method to set the quantity attribute.
|
115
177
|
#
|
116
178
|
# @param quantity_attr [String] The name of the quantity attribute.
|
117
179
|
#
|
@@ -122,20 +184,20 @@ module UnitMeasurements
|
|
122
184
|
def redefine_quantity_writer(quantity_attr)
|
123
185
|
redefine_method("#{quantity_attr}=") do |quantity|
|
124
186
|
quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
|
125
|
-
|
187
|
+
if quantity
|
126
188
|
db_column_props = self.column_for_attribute(quantity_attr)
|
127
189
|
precision, scale = db_column_props.precision, db_column_props.scale
|
128
190
|
|
129
191
|
quantity.round(scale)
|
130
192
|
else
|
131
193
|
nil
|
132
|
-
end
|
133
|
-
|
134
|
-
write_attribute(quantity_attr, quantity)
|
194
|
+
end.tap { |value| write_attribute(quantity_attr, value) }
|
135
195
|
end
|
136
196
|
end
|
137
197
|
|
138
|
-
#
|
198
|
+
# @!scope class
|
199
|
+
# @private
|
200
|
+
# Redefines the writer method to set the unit attribute.
|
139
201
|
#
|
140
202
|
# @param unit_attr [String] The name of the unit attribute.
|
141
203
|
# @param unit_group [Class] The unit group class for the measurement.
|
@@ -146,7 +208,7 @@ module UnitMeasurements
|
|
146
208
|
# @since 1.0.0
|
147
209
|
def redefine_unit_writer(unit_attr, unit_group)
|
148
210
|
redefine_method("#{unit_attr}=") do |unit|
|
149
|
-
unit_name = unit_group.
|
211
|
+
unit_name = unit_group.unit_for(unit).try!(:name)
|
150
212
|
write_attribute(unit_attr, (unit_name || unit))
|
151
213
|
end
|
152
214
|
end
|
@@ -154,6 +216,8 @@ module UnitMeasurements
|
|
154
216
|
end
|
155
217
|
end
|
156
218
|
|
219
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord`
|
220
|
+
# module.
|
157
221
|
ActiveSupport.on_load(:active_record) do
|
158
|
-
::ActiveRecord::Base.send
|
222
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord)
|
159
223
|
end
|
@@ -10,18 +10,13 @@ require "active_record"
|
|
10
10
|
require "active_model"
|
11
11
|
require "active_model/validations"
|
12
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
13
|
module UnitMeasurements
|
19
|
-
# The +Rails+
|
20
|
-
# {unit_measurements}[https://github.com/shivam091/unit_measurements]
|
21
|
-
# Ruby on Rails applications.
|
14
|
+
# The +UnitMeasurements::Rails+ integrates the
|
15
|
+
# {unit_measurements}[https://github.com/shivam091/unit_measurements]
|
16
|
+
# gem into Ruby on Rails applications.
|
22
17
|
#
|
23
|
-
# It
|
24
|
-
# errors.
|
18
|
+
# It integrates +unit_measurements+ into Ruby on Rails applications, encompassing
|
19
|
+
# modules and classes for ActiveRecord support, Railties, and custom errors.
|
25
20
|
#
|
26
21
|
# @see ActiveRecord
|
27
22
|
# @see Railtie
|
@@ -29,7 +24,7 @@ module UnitMeasurements
|
|
29
24
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
30
25
|
# @since 0.2.0
|
31
26
|
module Rails
|
32
|
-
# This
|
27
|
+
# This class serves as the base for custom errors within the +UnitMeasurements::Rails+
|
33
28
|
# module.
|
34
29
|
#
|
35
30
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
@@ -4,12 +4,11 @@
|
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
6
|
module Rails
|
7
|
-
# The +Railtie+ class
|
8
|
-
# framework.
|
7
|
+
# The +Railtie+ class integrates +unit_measurements+ with the Rails framework.
|
9
8
|
#
|
10
9
|
# This Railtie is designed to be automatically loaded by Rails when the
|
11
|
-
# application starts. It
|
12
|
-
# of +unit_measurements+
|
10
|
+
# application starts. It facilitates configuration and customization of the
|
11
|
+
# behavior of +unit_measurements+ within a Rails application.
|
13
12
|
#
|
14
13
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
15
14
|
# @since 0.1.0
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
# The +UnitMeasurements::Rails::ActiveRecord::Area+ module provides a convenient
|
6
|
+
# way to define area-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# area-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.1.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Area
|
14
|
+
# @!scope class
|
15
|
+
# Defines _area-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of area-measured attributes by accepting an array of attribute
|
19
|
+
# names.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of area-measured attributes.
|
23
|
+
# @param [Hash] options A customizable set of options
|
24
|
+
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
25
|
+
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
26
|
+
#
|
27
|
+
# @example Define single area-measured attribute:
|
28
|
+
# class Land < ActiveRecord::Base
|
29
|
+
# measured_area :total_area
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple area-measured attributes:
|
33
|
+
# class Land < ActiveRecord::Base
|
34
|
+
# measured_area :carpet_area, :buildup_area
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.1.0
|
42
|
+
def measured_area(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Area, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Area` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Area)
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
# The +UnitMeasurements::Rails::ActiveRecord::Length+ module provides a convenient
|
6
|
+
# way to define length-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# length-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.1.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Length
|
14
|
+
# @!scope class
|
15
|
+
# Defines _length-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of length-measured attributes by accepting an array of attribute
|
19
|
+
# names.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of length-measured attributes.
|
23
|
+
# @param [Hash] options A customizable set of options
|
24
|
+
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
25
|
+
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
26
|
+
#
|
27
|
+
# @example Define single length-measured attribute:
|
28
|
+
# class Cube < ActiveRecord::Base
|
29
|
+
# measured_length :length
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple length-measured attributes:
|
33
|
+
# class Cube < ActiveRecord::Base
|
34
|
+
# measured_length :height, :width
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.1.0
|
42
|
+
def measured_length(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Length, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Length` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Length)
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
# The +UnitMeasurements::Rails::ActiveRecord::Volume+ module provides a convenient
|
6
|
+
# way to define volume-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# volume-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.1.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Volume
|
14
|
+
# @!scope class
|
15
|
+
# Defines _volume-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of volume-measured attributes by accepting an array of attribute
|
19
|
+
# names.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of volume-measured attributes.
|
23
|
+
# @param [Hash] options A customizable set of options
|
24
|
+
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
25
|
+
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
26
|
+
#
|
27
|
+
# @example Define single volume-measured attribute:
|
28
|
+
# class Container < ActiveRecord::Base
|
29
|
+
# measured_volume :total_volume
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple volume-measured attributes:
|
33
|
+
# class Container < ActiveRecord::Base
|
34
|
+
# measured_volume :internal_volume, :external_volume
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.1.0
|
42
|
+
def measured_volume(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Volume, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Volume` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Volume)
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
# The +UnitMeasurements::Rails::ActiveRecord::Weight+ module provides a convenient
|
6
|
+
# way to define weight-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# weight-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.1.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Weight
|
14
|
+
# @!scope class
|
15
|
+
# Defines _weight-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of weight-measured attributes by accepting an array of attribute
|
19
|
+
# names.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of weight-measured attributes.
|
23
|
+
# @param [Hash] options A customizable set of options
|
24
|
+
# @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute.
|
25
|
+
# @option options [String|Symbol] :unit_attribute_name The name of the unit attribute.
|
26
|
+
#
|
27
|
+
# @example Define single weight-measured attribute:
|
28
|
+
# class Package < ActiveRecord::Base
|
29
|
+
# measured_weight :total_weight
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple weight-measured attributes:
|
33
|
+
# class Package < ActiveRecord::Base
|
34
|
+
# measured_weight :item_weight, :package_weight
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.1.0
|
42
|
+
def measured_weight(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Weight, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Weight` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Weight)
|
50
|
+
end
|
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.2.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-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -169,6 +169,10 @@ files:
|
|
169
169
|
- lib/unit_measurements/rails/base.rb
|
170
170
|
- lib/unit_measurements/rails/railtie.rb
|
171
171
|
- lib/unit_measurements/rails/unit_groups/all.rb
|
172
|
+
- lib/unit_measurements/rails/unit_groups/area.rb
|
173
|
+
- lib/unit_measurements/rails/unit_groups/length.rb
|
174
|
+
- lib/unit_measurements/rails/unit_groups/volume.rb
|
175
|
+
- lib/unit_measurements/rails/unit_groups/weight.rb
|
172
176
|
- lib/unit_measurements/rails/version.rb
|
173
177
|
- unit_measurements-rails.gemspec
|
174
178
|
homepage: https://github.com/shivam091/unit_measurements-rails
|