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