unit_measurements-rails 1.0.0 → 1.1.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 +12 -0
- data/Gemfile.lock +1 -1
- data/README.md +50 -18
- data/lib/unit_measurements/rails/active_record.rb +44 -32
- 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 +46 -0
- data/lib/unit_measurements/rails/unit_groups/length.rb +46 -0
- data/lib/unit_measurements/rails/unit_groups/volume.rb +46 -0
- data/lib/unit_measurements/rails/unit_groups/weight.rb +46 -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: 5f1843c7f2b52c98319dc2157d86f1028638fcc375e3104551ea79f2954560a4
|
4
|
+
data.tar.gz: 271f1f7c5103ad50e3eddf357cc1456aba6e7219eebeca8768ab7fc5b9bcae9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c824399cc26f750e0e5981f39f4ea14ef50e2c1cb6d6c8dbfabf402bea3e06248ed4e48a0787ce015b223c9aa8b52949b15c6527db655bbb47c7b0db2da76957
|
7
|
+
data.tar.gz: 236a331b4812c7598f997f27203f5b1bfaadd8f0f8a9ac667d2d1e454a8f5f46395eb9dfb7590f81443b0a1ae0b3fe83ead51d5d838eba9de70203f39ede554f
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## [1.1.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.0.0...v1.1.0) - 2023-11-16
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added ability to define _multiple_ measured attributes at a time.
|
6
|
+
- Added wrapper for defining `length` measured attributes.
|
7
|
+
- Added wrapper for defining `weight` measured attributes.
|
8
|
+
- Added wrapper for defining `volume` measured attributes.
|
9
|
+
- Added wrapper for defining `area` measured attributes.
|
10
|
+
|
11
|
+
-----------
|
12
|
+
|
1
13
|
## [1.0.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.2.0...v1.0.0) - 2023-11-14
|
2
14
|
|
3
15
|
### What's new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -44,42 +44,74 @@ Or otherwise simply install it yourself as:
|
|
44
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
45
|
|
46
46
|
```ruby
|
47
|
-
class
|
47
|
+
class CreateTableCubes < ActiveRecord::Migration[7.0]
|
48
48
|
def change
|
49
|
-
|
50
|
-
|
49
|
+
create_table :cubes do |t|
|
50
|
+
t.decimal :length_quantity, precision: 10, scale: 2
|
51
|
+
t.string :length_unit, limit: 12
|
52
|
+
t.decimal :width_quantity, precision: 10, scale: 2
|
53
|
+
t.string :width_unit, limit: 12
|
54
|
+
t.decimal :height_quantity, precision: 10, scale: 2
|
55
|
+
t.string :height_unit, limit: 12
|
56
|
+
t.timestamps
|
57
|
+
end
|
51
58
|
end
|
52
59
|
end
|
53
60
|
```
|
54
61
|
|
55
|
-
A column can be declared as a
|
62
|
+
A column can be declared as a measured with its unit group class:
|
56
63
|
|
57
64
|
```ruby
|
58
|
-
class
|
65
|
+
class Cube < ActiveRecord::Base
|
59
66
|
measured UnitMeasurements::Length, :height
|
60
67
|
end
|
61
68
|
```
|
62
69
|
|
63
|
-
This will allow you to access and assign a
|
70
|
+
This will allow you to access and assign a measured attribute:
|
64
71
|
|
65
72
|
```ruby
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
#=>
|
70
|
-
|
71
|
-
#=> "ft"
|
72
|
-
thing.height
|
73
|
-
#=> 5.0 ft
|
73
|
+
cube = Cube.new
|
74
|
+
cube.height = UnitMeasurements::Length.new(5, "ft")
|
75
|
+
cube.height_quantity #=> 0.5e1
|
76
|
+
cube.height_unit #=> "ft"
|
77
|
+
cube.height #=> 5.0 ft
|
74
78
|
```
|
75
79
|
|
76
|
-
Order of assignment does not matter, and each attribute can be assigned separately
|
80
|
+
Order of assignment does not matter, and each attribute can be assigned separately
|
81
|
+
and with mass assignment:
|
77
82
|
|
78
83
|
```ruby
|
79
84
|
params = {height_quantity: "3", height_unit: "ft"}
|
80
|
-
|
81
|
-
|
82
|
-
|
85
|
+
cube = Cube.new(params)
|
86
|
+
cube.height #=> 3.0 ft
|
87
|
+
```
|
88
|
+
|
89
|
+
You can specify multiple measured attributes at a time:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class Land < ActiveRecord::Base
|
93
|
+
measured UnitMeasurements::Area, :carpet_area, :buildup_area
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
There are some simpler methods for predefined types:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Package < ActiveRecord::Base
|
101
|
+
measured_length :size
|
102
|
+
measured_weight :item_weight, :package_weight
|
103
|
+
measured_area :carpet_area
|
104
|
+
measured_volume :total_volume
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
This will allow you to access and assign a measurement object:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
package = Package.new
|
112
|
+
package.item_weight = UnitMeasurements::Weight.new(10, "g")
|
113
|
+
package.item_weight_unit #=> "g"
|
114
|
+
package.item_weight_value #=> 10
|
83
115
|
```
|
84
116
|
|
85
117
|
## Contributing
|
@@ -5,55 +5,63 @@
|
|
5
5
|
module UnitMeasurements
|
6
6
|
module Rails
|
7
7
|
# The +UnitMeasurements::Rails::ActiveRecord+ module enhances ActiveRecord
|
8
|
-
# models by providing a convenient way to handle unit measurements. It
|
9
|
-
#
|
10
|
-
#
|
8
|
+
# models by providing a convenient way to handle unit measurements. It
|
9
|
+
# facilitates defining measurable attributes in models with specific unit
|
10
|
+
# group support.
|
11
11
|
#
|
12
12
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
13
13
|
# @since 1.0.0
|
14
14
|
module ActiveRecord
|
15
|
-
# Defines a
|
16
|
-
# +ActiveRecord+ model.
|
15
|
+
# Defines a _reader_ and _writer_ methods for the measured attributes in
|
16
|
+
# the +ActiveRecord+ model.
|
17
17
|
#
|
18
|
-
# @example Defining measured
|
19
|
-
# class
|
18
|
+
# @example Defining single measured attribute:
|
19
|
+
# class Cube < ActiveRecord::Base
|
20
20
|
# measured UnitMeasurements::Length, :height
|
21
21
|
# end
|
22
22
|
#
|
23
|
+
# @example Defining multiple measured attributes:
|
24
|
+
# class Package < ActiveRecord::Base
|
25
|
+
# measured UnitMeasurements::Weight, :item_weight, :total_weight
|
26
|
+
# end
|
27
|
+
#
|
23
28
|
# @param [Class|String] unit_group
|
24
29
|
# The unit group class or its name as a string.
|
25
|
-
# @param [String|Symbol]
|
26
|
-
#
|
30
|
+
# @param [Array<String|Symbol>] measured_attrs
|
31
|
+
# An array of the names of measured attributes.
|
27
32
|
# @return [void]
|
28
33
|
#
|
29
34
|
# @raise [BaseError]
|
30
|
-
#
|
35
|
+
# If +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
31
36
|
#
|
32
37
|
# @see BaseError
|
33
38
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
34
39
|
# @since 1.0.0
|
35
|
-
def measured(unit_group,
|
40
|
+
def measured(unit_group, *measured_attrs)
|
36
41
|
unit_group = unit_group.constantize if unit_group.is_a?(String)
|
37
42
|
|
38
43
|
validate_unit_group!(unit_group)
|
39
44
|
|
40
|
-
|
41
|
-
|
45
|
+
measured_attrs.map(&:to_s).each do |measured_attr|
|
46
|
+
quantity_attr = "#{measured_attr}_quantity"
|
47
|
+
unit_attr = "#{measured_attr}_unit"
|
42
48
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
50
|
+
define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
51
|
+
redefine_quantity_writer(quantity_attr)
|
52
|
+
redefine_unit_writer(unit_attr, unit_group)
|
53
|
+
end
|
47
54
|
end
|
48
55
|
|
49
56
|
private
|
50
57
|
|
51
|
-
#
|
58
|
+
# @private
|
59
|
+
# Validates whether +unit_group+ is a subclass of +UnitMeasurements::Measurement+.
|
52
60
|
#
|
53
61
|
# @param [Class] unit_group The unit group class to be validated.
|
54
62
|
#
|
55
63
|
# @raise [BaseError]
|
56
|
-
# if unit_group is not a subclass of UnitMeasurements::Measurement
|
64
|
+
# if +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
57
65
|
#
|
58
66
|
# @return [void]
|
59
67
|
#
|
@@ -65,9 +73,10 @@ module UnitMeasurements
|
|
65
73
|
end
|
66
74
|
end
|
67
75
|
|
68
|
-
#
|
76
|
+
# @private
|
77
|
+
# Defines the method to read the measured attribute.
|
69
78
|
#
|
70
|
-
# @param [String
|
79
|
+
# @param [String] measured_attr The name of the measured attribute.
|
71
80
|
# @param [String] quantity_attr The name of the quantity attribute.
|
72
81
|
# @param [String] unit_attr The name of the unit attribute.
|
73
82
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -76,8 +85,8 @@ module UnitMeasurements
|
|
76
85
|
#
|
77
86
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
78
87
|
# @since 1.0.0
|
79
|
-
def
|
80
|
-
define_method(
|
88
|
+
def define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
89
|
+
define_method(measured_attr) do
|
81
90
|
quantity, unit = public_send(quantity_attr), public_send(unit_attr)
|
82
91
|
|
83
92
|
begin
|
@@ -88,9 +97,10 @@ module UnitMeasurements
|
|
88
97
|
end
|
89
98
|
end
|
90
99
|
|
91
|
-
#
|
100
|
+
# @private
|
101
|
+
# Defines the method to write the measured attribute.
|
92
102
|
#
|
93
|
-
# @param [String
|
103
|
+
# @param [String] measured_attr The name of the measured attribute.
|
94
104
|
# @param [String] quantity_attr The name of the quantity attribute.
|
95
105
|
# @param [String] unit_attr The name of the unit attribute.
|
96
106
|
# @param [Class] unit_group The unit group class for the measurement.
|
@@ -99,8 +109,8 @@ module UnitMeasurements
|
|
99
109
|
#
|
100
110
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
101
111
|
# @since 1.0.0
|
102
|
-
def
|
103
|
-
define_method("#{
|
112
|
+
def define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
113
|
+
define_method("#{measured_attr}=") do |measurement|
|
104
114
|
if measurement.is_a?(unit_group)
|
105
115
|
public_send("#{quantity_attr}=", measurement.quantity)
|
106
116
|
public_send("#{unit_attr}=", measurement.unit.name)
|
@@ -111,7 +121,8 @@ module UnitMeasurements
|
|
111
121
|
end
|
112
122
|
end
|
113
123
|
|
114
|
-
#
|
124
|
+
# @private
|
125
|
+
# Redefines the writer method to set the quantity attribute.
|
115
126
|
#
|
116
127
|
# @param quantity_attr [String] The name of the quantity attribute.
|
117
128
|
#
|
@@ -129,13 +140,12 @@ module UnitMeasurements
|
|
129
140
|
quantity.round(scale)
|
130
141
|
else
|
131
142
|
nil
|
132
|
-
end
|
133
|
-
|
134
|
-
write_attribute(quantity_attr, quantity)
|
143
|
+
end.tap { |value| write_attribute(quantity_attr, value) }
|
135
144
|
end
|
136
145
|
end
|
137
146
|
|
138
|
-
#
|
147
|
+
# @private
|
148
|
+
# Redefines the writer method to set the unit attribute.
|
139
149
|
#
|
140
150
|
# @param unit_attr [String] The name of the unit attribute.
|
141
151
|
# @param unit_group [Class] The unit group class for the measurement.
|
@@ -154,6 +164,8 @@ module UnitMeasurements
|
|
154
164
|
end
|
155
165
|
end
|
156
166
|
|
167
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord`
|
168
|
+
# module.
|
157
169
|
ActiveSupport.on_load(:active_record) do
|
158
170
|
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord
|
159
171
|
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,46 @@
|
|
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
|
+
# Defines _area-measured_ attributes in the +ActiveRecord+ model.
|
15
|
+
#
|
16
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
|
+
# definition of area-measured attributes by accepting an array of attribute
|
18
|
+
# names.
|
19
|
+
#
|
20
|
+
# @param [Array<String|Symbol>] measured_attrs
|
21
|
+
# An array of the names of area-measured attributes.
|
22
|
+
#
|
23
|
+
# @example Define single area-measured attribute:
|
24
|
+
# class Land < ActiveRecord::Base
|
25
|
+
# measured_area :total_area
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example Define multiple area-measured attributes:
|
29
|
+
# class Land < ActiveRecord::Base
|
30
|
+
# measured_area :carpet_area, :buildup_area
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# @return [void]
|
34
|
+
#
|
35
|
+
# @see #measured
|
36
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
|
+
# @since 1.1.0
|
38
|
+
def measured_area(*measured_attrs)
|
39
|
+
measured(UnitMeasurements::Area, *measured_attrs)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Area` module.
|
44
|
+
ActiveSupport.on_load(:active_record) do
|
45
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord::Area
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
# Defines _length-measured_ attributes in the +ActiveRecord+ model.
|
15
|
+
#
|
16
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
|
+
# definition of length-measured attributes by accepting an array of attribute
|
18
|
+
# names.
|
19
|
+
#
|
20
|
+
# @param [Array<String|Symbol>] measured_attrs
|
21
|
+
# An array of the names of length-measured attributes.
|
22
|
+
#
|
23
|
+
# @example Define single length-measured attribute:
|
24
|
+
# class Cube < ActiveRecord::Base
|
25
|
+
# measured_length :length
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example Define multiple length-measured attributes:
|
29
|
+
# class Cube < ActiveRecord::Base
|
30
|
+
# measured_length :height, :width
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# @return [void]
|
34
|
+
#
|
35
|
+
# @see #measured
|
36
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
|
+
# @since 1.1.0
|
38
|
+
def measured_length(*measured_attrs)
|
39
|
+
measured(UnitMeasurements::Length, *measured_attrs)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Length` module.
|
44
|
+
ActiveSupport.on_load(:active_record) do
|
45
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord::Length
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
# Defines _volume-measured_ attributes in the +ActiveRecord+ model.
|
15
|
+
#
|
16
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
|
+
# definition of volume-measured attributes by accepting an array of attribute
|
18
|
+
# names.
|
19
|
+
#
|
20
|
+
# @param [Array<String|Symbol>] measured_attrs
|
21
|
+
# An array of the names of volume-measured attributes.
|
22
|
+
#
|
23
|
+
# @example Define single volume-measured attribute:
|
24
|
+
# class Container < ActiveRecord::Base
|
25
|
+
# measured_volume :total_volume
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example Define multiple volume-measured attributes:
|
29
|
+
# class Container < ActiveRecord::Base
|
30
|
+
# measured_volume :internal_volume, :external_volume
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# @return [void]
|
34
|
+
#
|
35
|
+
# @see #measured
|
36
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
|
+
# @since 1.1.0
|
38
|
+
def measured_volume(*measured_attrs)
|
39
|
+
measured(UnitMeasurements::Volume, *measured_attrs)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Volume` module.
|
44
|
+
ActiveSupport.on_load(:active_record) do
|
45
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord::Volume
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
# Defines _weight-measured_ attributes in the +ActiveRecord+ model.
|
15
|
+
#
|
16
|
+
# This method serves as a wrapper around the +measured+ method and allows easy
|
17
|
+
# definition of weight-measured attributes by accepting an array of attribute
|
18
|
+
# names.
|
19
|
+
#
|
20
|
+
# @param [Array<String|Symbol>] measured_attrs
|
21
|
+
# An array of the names of weight-measured attributes.
|
22
|
+
#
|
23
|
+
# @example Define single weight-measured attribute:
|
24
|
+
# class Package < ActiveRecord::Base
|
25
|
+
# measured_weight :total_weight
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example Define multiple weight-measured attributes:
|
29
|
+
# class Package < ActiveRecord::Base
|
30
|
+
# measured_weight :item_weight, :package_weight
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# @return [void]
|
34
|
+
#
|
35
|
+
# @see #measured
|
36
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
37
|
+
# @since 1.1.0
|
38
|
+
def measured_weight(*measured_attrs)
|
39
|
+
measured(UnitMeasurements::Weight, *measured_attrs)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Weight` module.
|
44
|
+
ActiveSupport.on_load(:active_record) do
|
45
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord::Weight
|
46
|
+
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.1.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-17 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
|