unit_measurements-rails 0.2.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 +21 -0
- data/Gemfile.lock +2 -2
- data/README.md +76 -0
- data/lib/unit_measurements/rails/active_record.rb +171 -0
- data/lib/unit_measurements/rails/base.rb +19 -0
- data/lib/unit_measurements/rails/railtie.rb +9 -0
- data/lib/unit_measurements/rails/unit_groups/all.rb +8 -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
- data/lib/unit_measurements-rails.rb +2 -0
- metadata +8 -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,24 @@
|
|
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
|
+
|
13
|
+
## [1.0.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.2.0...v1.0.0) - 2023-11-14
|
14
|
+
|
15
|
+
### What's new
|
16
|
+
|
17
|
+
- Added method `measured` to store and retrieve measurement object.
|
18
|
+
- Added RDoc for modules and classes.
|
19
|
+
|
20
|
+
-----------
|
21
|
+
|
1
22
|
## [0.2.0](https://github.com/shivam091/unit_measurements-rails/compare/v0.1.0...v0.2.0) - 2023-11-12
|
2
23
|
|
3
24
|
### What's new
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unit_measurements-rails (
|
4
|
+
unit_measurements-rails (1.1.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.11.0)
|
172
172
|
activesupport (~> 7.0)
|
173
173
|
websocket-driver (0.7.6)
|
174
174
|
websocket-extensions (>= 0.1.0)
|
data/README.md
CHANGED
@@ -37,6 +37,82 @@ 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 CreateTableCubes < ActiveRecord::Migration[7.0]
|
48
|
+
def change
|
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
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
A column can be declared as a measured with its unit group class:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Cube < ActiveRecord::Base
|
66
|
+
measured UnitMeasurements::Length, :height
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
This will allow you to access and assign a measured attribute:
|
71
|
+
|
72
|
+
```ruby
|
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
|
78
|
+
```
|
79
|
+
|
80
|
+
Order of assignment does not matter, and each attribute can be assigned separately
|
81
|
+
and with mass assignment:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
params = {height_quantity: "3", height_unit: "ft"}
|
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
|
115
|
+
```
|
40
116
|
|
41
117
|
## Contributing
|
42
118
|
|
@@ -0,0 +1,171 @@
|
|
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
|
9
|
+
# facilitates defining measurable attributes in models with specific unit
|
10
|
+
# group support.
|
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 measured attributes in
|
16
|
+
# the +ActiveRecord+ model.
|
17
|
+
#
|
18
|
+
# @example Defining single measured attribute:
|
19
|
+
# class Cube < ActiveRecord::Base
|
20
|
+
# measured UnitMeasurements::Length, :height
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# @example Defining multiple measured attributes:
|
24
|
+
# class Package < ActiveRecord::Base
|
25
|
+
# measured UnitMeasurements::Weight, :item_weight, :total_weight
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @param [Class|String] unit_group
|
29
|
+
# The unit group class or its name as a string.
|
30
|
+
# @param [Array<String|Symbol>] measured_attrs
|
31
|
+
# An array of the names of measured attributes.
|
32
|
+
# @return [void]
|
33
|
+
#
|
34
|
+
# @raise [BaseError]
|
35
|
+
# If +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
36
|
+
#
|
37
|
+
# @see BaseError
|
38
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
39
|
+
# @since 1.0.0
|
40
|
+
def measured(unit_group, *measured_attrs)
|
41
|
+
unit_group = unit_group.constantize if unit_group.is_a?(String)
|
42
|
+
|
43
|
+
validate_unit_group!(unit_group)
|
44
|
+
|
45
|
+
measured_attrs.map(&:to_s).each do |measured_attr|
|
46
|
+
quantity_attr = "#{measured_attr}_quantity"
|
47
|
+
unit_attr = "#{measured_attr}_unit"
|
48
|
+
|
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
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
# @private
|
59
|
+
# Validates whether +unit_group+ is a subclass of +UnitMeasurements::Measurement+.
|
60
|
+
#
|
61
|
+
# @param [Class] unit_group The unit group class to be validated.
|
62
|
+
#
|
63
|
+
# @raise [BaseError]
|
64
|
+
# if +unit_group+ is not a subclass of +UnitMeasurements::Measurement+.
|
65
|
+
#
|
66
|
+
# @return [void]
|
67
|
+
#
|
68
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
69
|
+
# @since 1.0.0
|
70
|
+
def validate_unit_group!(unit_group)
|
71
|
+
unless unit_group.is_a?(Class) && unit_group.ancestors.include?(Measurement)
|
72
|
+
raise BaseError, "Expecting `#{unit_group}` to be a subclass of UnitMeasurements::Measurement"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# @private
|
77
|
+
# Defines the method to read the measured attribute.
|
78
|
+
#
|
79
|
+
# @param [String] measured_attr The name of the measured attribute.
|
80
|
+
# @param [String] quantity_attr The name of the quantity attribute.
|
81
|
+
# @param [String] unit_attr The name of the unit attribute.
|
82
|
+
# @param [Class] unit_group The unit group class for the measurement.
|
83
|
+
#
|
84
|
+
# @return [void]
|
85
|
+
#
|
86
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
87
|
+
# @since 1.0.0
|
88
|
+
def define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
89
|
+
define_method(measured_attr) do
|
90
|
+
quantity, unit = public_send(quantity_attr), public_send(unit_attr)
|
91
|
+
|
92
|
+
begin
|
93
|
+
unit_group.new(quantity, unit)
|
94
|
+
rescue BlankQuantityError, BlankUnitError, ParseError, UnitError
|
95
|
+
nil
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# @private
|
101
|
+
# Defines the method to write the measured attribute.
|
102
|
+
#
|
103
|
+
# @param [String] measured_attr The name of the measured attribute.
|
104
|
+
# @param [String] quantity_attr The name of the quantity attribute.
|
105
|
+
# @param [String] unit_attr The name of the unit attribute.
|
106
|
+
# @param [Class] unit_group The unit group class for the measurement.
|
107
|
+
#
|
108
|
+
# @return [void]
|
109
|
+
#
|
110
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
111
|
+
# @since 1.0.0
|
112
|
+
def define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
|
113
|
+
define_method("#{measured_attr}=") do |measurement|
|
114
|
+
if measurement.is_a?(unit_group)
|
115
|
+
public_send("#{quantity_attr}=", measurement.quantity)
|
116
|
+
public_send("#{unit_attr}=", measurement.unit.name)
|
117
|
+
else
|
118
|
+
public_send("#{quantity_attr}=", nil)
|
119
|
+
public_send("#{unit_attr}=", nil)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# @private
|
125
|
+
# Redefines the writer method to set the quantity attribute.
|
126
|
+
#
|
127
|
+
# @param quantity_attr [String] The name of the quantity attribute.
|
128
|
+
#
|
129
|
+
# @return [void]
|
130
|
+
#
|
131
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
132
|
+
# @since 1.0.0
|
133
|
+
def redefine_quantity_writer(quantity_attr)
|
134
|
+
redefine_method("#{quantity_attr}=") do |quantity|
|
135
|
+
quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
|
136
|
+
quantity = if quantity
|
137
|
+
db_column_props = self.column_for_attribute(quantity_attr)
|
138
|
+
precision, scale = db_column_props.precision, db_column_props.scale
|
139
|
+
|
140
|
+
quantity.round(scale)
|
141
|
+
else
|
142
|
+
nil
|
143
|
+
end.tap { |value| write_attribute(quantity_attr, value) }
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# @private
|
148
|
+
# Redefines the writer method to set the unit attribute.
|
149
|
+
#
|
150
|
+
# @param unit_attr [String] The name of the unit attribute.
|
151
|
+
# @param unit_group [Class] The unit group class for the measurement.
|
152
|
+
#
|
153
|
+
# @return [void]
|
154
|
+
#
|
155
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
156
|
+
# @since 1.0.0
|
157
|
+
def redefine_unit_writer(unit_attr, unit_group)
|
158
|
+
redefine_method("#{unit_attr}=") do |unit|
|
159
|
+
unit_name = unit_group.unit_group.unit_for(unit).try!(:name)
|
160
|
+
write_attribute(unit_attr, (unit_name || unit))
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord`
|
168
|
+
# module.
|
169
|
+
ActiveSupport.on_load(:active_record) do
|
170
|
+
::ActiveRecord::Base.send :extend, UnitMeasurements::Rails::ActiveRecord
|
171
|
+
end
|
@@ -11,9 +11,28 @@ require "active_model"
|
|
11
11
|
require "active_model/validations"
|
12
12
|
|
13
13
|
module UnitMeasurements
|
14
|
+
# The +UnitMeasurements::Rails+ integrates the
|
15
|
+
# {unit_measurements}[https://github.com/shivam091/unit_measurements]
|
16
|
+
# gem into Ruby on Rails applications.
|
17
|
+
#
|
18
|
+
# It integrates +unit_measurements+ into Ruby on Rails applications, encompassing
|
19
|
+
# modules and classes for ActiveRecord support, Railties, and custom errors.
|
20
|
+
#
|
21
|
+
# @see ActiveRecord
|
22
|
+
# @see Railtie
|
23
|
+
# @see BaseError
|
24
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
25
|
+
# @since 0.2.0
|
14
26
|
module Rails
|
27
|
+
# This class serves as the base for custom errors within the +UnitMeasurements::Rails+
|
28
|
+
# module.
|
29
|
+
#
|
30
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
31
|
+
# @since 0.2.0
|
15
32
|
class BaseError < StandardError; end
|
16
33
|
end
|
17
34
|
end
|
18
35
|
|
36
|
+
require "unit_measurements/rails/active_record"
|
37
|
+
|
19
38
|
require "unit_measurements/rails/railtie" if defined?(Rails)
|
@@ -4,7 +4,16 @@
|
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
6
|
module Rails
|
7
|
+
# The +Railtie+ class integrates +unit_measurements+ with the Rails framework.
|
8
|
+
#
|
9
|
+
# This Railtie is designed to be automatically loaded by Rails when the
|
10
|
+
# application starts. It facilitates configuration and customization of the
|
11
|
+
# behavior of +unit_measurements+ within a Rails application.
|
12
|
+
#
|
13
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
14
|
+
# @since 0.1.0
|
7
15
|
class Railtie < ::Rails::Railtie
|
16
|
+
# (optional: add custom configuration, initialization, or other hooks)
|
8
17
|
end
|
9
18
|
end
|
10
19
|
end
|
@@ -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:
|
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
|
@@ -165,8 +165,14 @@ files:
|
|
165
165
|
- gemfiles/rails-7.1.gemfile
|
166
166
|
- gemfiles/rails-edge.gemfile
|
167
167
|
- lib/unit_measurements-rails.rb
|
168
|
+
- lib/unit_measurements/rails/active_record.rb
|
168
169
|
- lib/unit_measurements/rails/base.rb
|
169
170
|
- lib/unit_measurements/rails/railtie.rb
|
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
|
170
176
|
- lib/unit_measurements/rails/version.rb
|
171
177
|
- unit_measurements-rails.gemspec
|
172
178
|
homepage: https://github.com/shivam091/unit_measurements-rails
|