unit_measurements-rails 1.1.0 → 1.3.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 +20 -1
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/unit_measurements/rails/active_record.rb +61 -7
- data/lib/unit_measurements/rails/unit_groups/all.rb +5 -2
- data/lib/unit_measurements/rails/unit_groups/area.rb +9 -5
- data/lib/unit_measurements/rails/unit_groups/density.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/length.rb +9 -5
- data/lib/unit_measurements/rails/unit_groups/temperature.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/time.rb +50 -0
- data/lib/unit_measurements/rails/unit_groups/volume.rb +9 -5
- data/lib/unit_measurements/rails/unit_groups/weight.rb +9 -5
- data/lib/unit_measurements/rails/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf20e075bf5db7bd8a33f60eb2e14493aa4ed985defd4a3f49b020498a66ec6b
|
4
|
+
data.tar.gz: df3f2e54ed849cab6b0b6e7221e57bc74c11480053136aba222f6f91b3c43410
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a59d3f2cd7a3d7f716aa68ba88ff3c19ae29aade038c6aa1258a5eeef77c6fc44634e3c66167b8e9e499865c51b15861f0122e3cb6b226d1414daa8cb18af14
|
7
|
+
data.tar.gz: 3c722fab2b67d36ae090aef75a01222e5bbecf937c9a2322c03832a4960e0304975d9a347eef5178ea94cc5ae184f5055f97436e37a5702a1ded9b5cbebd5cc2
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
## [1.3.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.2.0...v1.3.0) - 2023-11-22
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Raise `BaseError` if already measured attribute.
|
6
|
+
- Added wrapper for defining `density` measured attributes.
|
7
|
+
- Added wrapper for defining `temperature` measured attributes.
|
8
|
+
- Added wrapper for defining `time` measured attributes.
|
9
|
+
|
10
|
+
-----------
|
11
|
+
|
12
|
+
## [1.2.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.1.0...v1.2.0) - 2023-11-20
|
13
|
+
|
14
|
+
### What's new
|
15
|
+
|
16
|
+
- Added method `.measured_fields` to provide ability to maintain measured attribute names and their options.
|
17
|
+
|
18
|
+
-----------
|
19
|
+
|
1
20
|
## [1.1.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.0.0...v1.1.0) - 2023-11-16
|
2
21
|
|
3
22
|
### What's new
|
@@ -14,7 +33,7 @@
|
|
14
33
|
|
15
34
|
### What's new
|
16
35
|
|
17
|
-
- Added method
|
36
|
+
- Added method `.measured` to store and retrieve measurement object.
|
18
37
|
- Added RDoc for modules and classes.
|
19
38
|
|
20
39
|
-----------
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -41,7 +41,11 @@ 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
51
|
class CreateTableCubes < ActiveRecord::Migration[7.0]
|
@@ -94,6 +98,18 @@ class Land < ActiveRecord::Base
|
|
94
98
|
end
|
95
99
|
```
|
96
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
|
+
|
97
113
|
There are some simpler methods for predefined types:
|
98
114
|
|
99
115
|
```ruby
|
@@ -2,6 +2,15 @@
|
|
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
|
@@ -12,6 +21,7 @@ module UnitMeasurements
|
|
12
21
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
13
22
|
# @since 1.0.0
|
14
23
|
module ActiveRecord
|
24
|
+
# @!scope class
|
15
25
|
# Defines a _reader_ and _writer_ methods for the measured attributes in
|
16
26
|
# the +ActiveRecord+ model.
|
17
27
|
#
|
@@ -29,6 +39,10 @@ module UnitMeasurements
|
|
29
39
|
# The unit group class or its name as a string.
|
30
40
|
# @param [Array<String|Symbol>] measured_attrs
|
31
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
|
+
#
|
32
46
|
# @return [void]
|
33
47
|
#
|
34
48
|
# @raise [BaseError]
|
@@ -37,14 +51,21 @@ module UnitMeasurements
|
|
37
51
|
# @see BaseError
|
38
52
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
39
53
|
# @since 1.0.0
|
40
|
-
def measured(unit_group, *measured_attrs)
|
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)
|
41
58
|
unit_group = unit_group.constantize if unit_group.is_a?(String)
|
42
59
|
|
43
|
-
|
60
|
+
options[:unit_group] = unit_group
|
44
61
|
|
45
62
|
measured_attrs.map(&:to_s).each do |measured_attr|
|
46
|
-
|
47
|
-
|
63
|
+
raise BaseError, "The field '#{measured_attr}' has already been measured." if measured_attributes.key?(measured_attr)
|
64
|
+
|
65
|
+
quantity_attr = options[:quantity_attribute_name]&.to_s || "#{measured_attr}_quantity"
|
66
|
+
unit_attr = options[:unit_attribute_name]&.to_s || "#{measured_attr}_unit"
|
67
|
+
|
68
|
+
measured_attributes[measured_attr] = options.merge(quantity_attribute_name: quantity_attr, unit_attribute_name: unit_attr)
|
48
69
|
|
49
70
|
define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
50
71
|
define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
@@ -53,8 +74,37 @@ module UnitMeasurements
|
|
53
74
|
end
|
54
75
|
end
|
55
76
|
|
77
|
+
# @!scope class
|
78
|
+
# Returns a hash containing information about the measured attributes and
|
79
|
+
# their options.
|
80
|
+
#
|
81
|
+
# @return [Hash{String => Hash{Symbol => String|Class}}]
|
82
|
+
# A hash where keys represent the names of the measured attributes, and
|
83
|
+
# values are hashes containing the options for each measured attribute.
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
# {
|
87
|
+
# "height" => {
|
88
|
+
# unit_group: UnitMeasurements::Length,
|
89
|
+
# quantity_attribute_name: "height_quantity",
|
90
|
+
# unit_attribute_name: "height_unit"
|
91
|
+
# },
|
92
|
+
# "weight" => {
|
93
|
+
# unit_group: UnitMeasurements::Length,
|
94
|
+
# quantity_attribute_name: "weight_quantity",
|
95
|
+
# unit_attribute_name: "weight_unit"
|
96
|
+
# }
|
97
|
+
# }
|
98
|
+
#
|
99
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
100
|
+
# @since 1.2.0
|
101
|
+
def measured_attributes
|
102
|
+
@measured_attributes ||= {}
|
103
|
+
end
|
104
|
+
|
56
105
|
private
|
57
106
|
|
107
|
+
# @!scope class
|
58
108
|
# @private
|
59
109
|
# Validates whether +unit_group+ is a subclass of +UnitMeasurements::Measurement+.
|
60
110
|
#
|
@@ -73,6 +123,7 @@ module UnitMeasurements
|
|
73
123
|
end
|
74
124
|
end
|
75
125
|
|
126
|
+
# @!scope class
|
76
127
|
# @private
|
77
128
|
# Defines the method to read the measured attribute.
|
78
129
|
#
|
@@ -97,6 +148,7 @@ module UnitMeasurements
|
|
97
148
|
end
|
98
149
|
end
|
99
150
|
|
151
|
+
# @!scope class
|
100
152
|
# @private
|
101
153
|
# Defines the method to write the measured attribute.
|
102
154
|
#
|
@@ -121,6 +173,7 @@ module UnitMeasurements
|
|
121
173
|
end
|
122
174
|
end
|
123
175
|
|
176
|
+
# @!scope class
|
124
177
|
# @private
|
125
178
|
# Redefines the writer method to set the quantity attribute.
|
126
179
|
#
|
@@ -133,7 +186,7 @@ module UnitMeasurements
|
|
133
186
|
def redefine_quantity_writer(quantity_attr)
|
134
187
|
redefine_method("#{quantity_attr}=") do |quantity|
|
135
188
|
quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
|
136
|
-
|
189
|
+
if quantity
|
137
190
|
db_column_props = self.column_for_attribute(quantity_attr)
|
138
191
|
precision, scale = db_column_props.precision, db_column_props.scale
|
139
192
|
|
@@ -144,6 +197,7 @@ module UnitMeasurements
|
|
144
197
|
end
|
145
198
|
end
|
146
199
|
|
200
|
+
# @!scope class
|
147
201
|
# @private
|
148
202
|
# Redefines the writer method to set the unit attribute.
|
149
203
|
#
|
@@ -156,7 +210,7 @@ module UnitMeasurements
|
|
156
210
|
# @since 1.0.0
|
157
211
|
def redefine_unit_writer(unit_attr, unit_group)
|
158
212
|
redefine_method("#{unit_attr}=") do |unit|
|
159
|
-
unit_name = unit_group.
|
213
|
+
unit_name = unit_group.unit_for(unit).try!(:name)
|
160
214
|
write_attribute(unit_attr, (unit_name || unit))
|
161
215
|
end
|
162
216
|
end
|
@@ -167,5 +221,5 @@ end
|
|
167
221
|
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord`
|
168
222
|
# module.
|
169
223
|
ActiveSupport.on_load(:active_record) do
|
170
|
-
::ActiveRecord::Base.send
|
224
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord)
|
171
225
|
end
|
@@ -2,7 +2,10 @@
|
|
2
2
|
# -*- frozen_string_literal: true -*-
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
|
+
require_relative "area"
|
6
|
+
require_relative "density"
|
5
7
|
require_relative "length"
|
6
|
-
require_relative "
|
8
|
+
require_relative "temperature"
|
9
|
+
require_relative "time"
|
7
10
|
require_relative "volume"
|
8
|
-
require_relative "
|
11
|
+
require_relative "weight"
|
@@ -11,14 +11,18 @@
|
|
11
11
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
12
|
# @since 1.1.0
|
13
13
|
module UnitMeasurements::Rails::ActiveRecord::Area
|
14
|
+
# @!scope class
|
14
15
|
# Defines _area-measured_ attributes in the +ActiveRecord+ model.
|
15
16
|
#
|
16
17
|
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
18
|
# definition of area-measured attributes by accepting an array of attribute
|
18
|
-
# names.
|
19
|
+
# names along with their options.
|
19
20
|
#
|
20
21
|
# @param [Array<String|Symbol>] measured_attrs
|
21
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.
|
22
26
|
#
|
23
27
|
# @example Define single area-measured attribute:
|
24
28
|
# class Land < ActiveRecord::Base
|
@@ -32,15 +36,15 @@ module UnitMeasurements::Rails::ActiveRecord::Area
|
|
32
36
|
#
|
33
37
|
# @return [void]
|
34
38
|
#
|
35
|
-
# @see
|
39
|
+
# @see .measured
|
36
40
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
41
|
# @since 1.1.0
|
38
|
-
def measured_area(*measured_attrs)
|
39
|
-
measured(UnitMeasurements::Area, *measured_attrs)
|
42
|
+
def measured_area(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Area, *measured_attrs, **options)
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
47
|
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Area` module.
|
44
48
|
ActiveSupport.on_load(:active_record) do
|
45
|
-
::ActiveRecord::Base.send
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Area)
|
46
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::Density+ module provides a convenient
|
6
|
+
# way to define density-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# density-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.3.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Density
|
14
|
+
# @!scope class
|
15
|
+
# Defines _density-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of density-measured attributes by accepting an array of attribute
|
19
|
+
# names along with their options.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of density-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 density-measured attribute:
|
28
|
+
# class Substance < ActiveRecord::Base
|
29
|
+
# measured_density :total_density
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple density-measured attributes:
|
33
|
+
# class Substance < ActiveRecord::Base
|
34
|
+
# measured_density :internal_density, :external_density
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.3.0
|
42
|
+
def measured_density(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Density, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Density` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Density)
|
50
|
+
end
|
@@ -11,14 +11,18 @@
|
|
11
11
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
12
|
# @since 1.1.0
|
13
13
|
module UnitMeasurements::Rails::ActiveRecord::Length
|
14
|
+
# @!scope class
|
14
15
|
# Defines _length-measured_ attributes in the +ActiveRecord+ model.
|
15
16
|
#
|
16
17
|
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
18
|
# definition of length-measured attributes by accepting an array of attribute
|
18
|
-
# names.
|
19
|
+
# names along with their options.
|
19
20
|
#
|
20
21
|
# @param [Array<String|Symbol>] measured_attrs
|
21
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.
|
22
26
|
#
|
23
27
|
# @example Define single length-measured attribute:
|
24
28
|
# class Cube < ActiveRecord::Base
|
@@ -32,15 +36,15 @@ module UnitMeasurements::Rails::ActiveRecord::Length
|
|
32
36
|
#
|
33
37
|
# @return [void]
|
34
38
|
#
|
35
|
-
# @see
|
39
|
+
# @see .measured
|
36
40
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
41
|
# @since 1.1.0
|
38
|
-
def measured_length(*measured_attrs)
|
39
|
-
measured(UnitMeasurements::Length, *measured_attrs)
|
42
|
+
def measured_length(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Length, *measured_attrs, **options)
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
47
|
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Length` module.
|
44
48
|
ActiveSupport.on_load(:active_record) do
|
45
|
-
::ActiveRecord::Base.send
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Length)
|
46
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::Temperature+ module provides a convenient
|
6
|
+
# way to define temperature-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# temperature-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.3.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Temperature
|
14
|
+
# @!scope class
|
15
|
+
# Defines _temperature-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of temperature-measured attributes by accepting an array of attribute
|
19
|
+
# names along with their options.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of temperature-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 temperature-measured attribute:
|
28
|
+
# class WeatherReport < ActiveRecord::Base
|
29
|
+
# measured_temperature :average_temperature
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple temperature-measured attributes:
|
33
|
+
# class WeatherReport < ActiveRecord::Base
|
34
|
+
# measured_temperature :day_temperature, :night_temperature
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.3.0
|
42
|
+
def measured_temperature(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Temperature, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Temperature` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Temperature)
|
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::Time+ module provides a convenient
|
6
|
+
# way to define time-measured attributes in +ActiveRecord+ models.
|
7
|
+
#
|
8
|
+
# It acts as a wrapper for the +measured+ method, simplifying the definition of
|
9
|
+
# time-measured attributes without directly invoking the +measured+ method.
|
10
|
+
#
|
11
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
|
+
# @since 1.3.0
|
13
|
+
module UnitMeasurements::Rails::ActiveRecord::Time
|
14
|
+
# @!scope class
|
15
|
+
# Defines _time-measured_ attributes in the +ActiveRecord+ model.
|
16
|
+
#
|
17
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
18
|
+
# definition of time-measured attributes by accepting an array of attribute
|
19
|
+
# names along with their options.
|
20
|
+
#
|
21
|
+
# @param [Array<String|Symbol>] measured_attrs
|
22
|
+
# An array of the names of time-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 time-measured attribute:
|
28
|
+
# class ProjectTimeline < ActiveRecord::Base
|
29
|
+
# measured_time :duration
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example Define multiple time-measured attributes:
|
33
|
+
# class ProjectTimeline < ActiveRecord::Base
|
34
|
+
# measured_time :setup_time, :processing_time
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @return [void]
|
38
|
+
#
|
39
|
+
# @see .measured
|
40
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
41
|
+
# @since 1.3.0
|
42
|
+
def measured_time(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Time, *measured_attrs, **options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Time` module.
|
48
|
+
ActiveSupport.on_load(:active_record) do
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Time)
|
50
|
+
end
|
@@ -11,14 +11,18 @@
|
|
11
11
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
12
|
# @since 1.1.0
|
13
13
|
module UnitMeasurements::Rails::ActiveRecord::Volume
|
14
|
+
# @!scope class
|
14
15
|
# Defines _volume-measured_ attributes in the +ActiveRecord+ model.
|
15
16
|
#
|
16
17
|
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
18
|
# definition of volume-measured attributes by accepting an array of attribute
|
18
|
-
# names.
|
19
|
+
# names along with their options.
|
19
20
|
#
|
20
21
|
# @param [Array<String|Symbol>] measured_attrs
|
21
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.
|
22
26
|
#
|
23
27
|
# @example Define single volume-measured attribute:
|
24
28
|
# class Container < ActiveRecord::Base
|
@@ -32,15 +36,15 @@ module UnitMeasurements::Rails::ActiveRecord::Volume
|
|
32
36
|
#
|
33
37
|
# @return [void]
|
34
38
|
#
|
35
|
-
# @see
|
39
|
+
# @see .measured
|
36
40
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
41
|
# @since 1.1.0
|
38
|
-
def measured_volume(*measured_attrs)
|
39
|
-
measured(UnitMeasurements::Volume, *measured_attrs)
|
42
|
+
def measured_volume(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Volume, *measured_attrs, **options)
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
47
|
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Volume` module.
|
44
48
|
ActiveSupport.on_load(:active_record) do
|
45
|
-
::ActiveRecord::Base.send
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Volume)
|
46
50
|
end
|
@@ -11,14 +11,18 @@
|
|
11
11
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
12
12
|
# @since 1.1.0
|
13
13
|
module UnitMeasurements::Rails::ActiveRecord::Weight
|
14
|
+
# @!scope class
|
14
15
|
# Defines _weight-measured_ attributes in the +ActiveRecord+ model.
|
15
16
|
#
|
16
17
|
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
18
|
# definition of weight-measured attributes by accepting an array of attribute
|
18
|
-
# names.
|
19
|
+
# names along with their options.
|
19
20
|
#
|
20
21
|
# @param [Array<String|Symbol>] measured_attrs
|
21
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.
|
22
26
|
#
|
23
27
|
# @example Define single weight-measured attribute:
|
24
28
|
# class Package < ActiveRecord::Base
|
@@ -32,15 +36,15 @@ module UnitMeasurements::Rails::ActiveRecord::Weight
|
|
32
36
|
#
|
33
37
|
# @return [void]
|
34
38
|
#
|
35
|
-
# @see
|
39
|
+
# @see .measured
|
36
40
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
41
|
# @since 1.1.0
|
38
|
-
def measured_weight(*measured_attrs)
|
39
|
-
measured(UnitMeasurements::Weight, *measured_attrs)
|
42
|
+
def measured_weight(*measured_attrs, **options)
|
43
|
+
measured(UnitMeasurements::Weight, *measured_attrs, **options)
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
47
|
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Weight` module.
|
44
48
|
ActiveSupport.on_load(:active_record) do
|
45
|
-
::ActiveRecord::Base.send
|
49
|
+
::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Weight)
|
46
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.3.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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -170,7 +170,10 @@ files:
|
|
170
170
|
- lib/unit_measurements/rails/railtie.rb
|
171
171
|
- lib/unit_measurements/rails/unit_groups/all.rb
|
172
172
|
- lib/unit_measurements/rails/unit_groups/area.rb
|
173
|
+
- lib/unit_measurements/rails/unit_groups/density.rb
|
173
174
|
- lib/unit_measurements/rails/unit_groups/length.rb
|
175
|
+
- lib/unit_measurements/rails/unit_groups/temperature.rb
|
176
|
+
- lib/unit_measurements/rails/unit_groups/time.rb
|
174
177
|
- lib/unit_measurements/rails/unit_groups/volume.rb
|
175
178
|
- lib/unit_measurements/rails/unit_groups/weight.rb
|
176
179
|
- lib/unit_measurements/rails/version.rb
|