unit_measurements-rails 1.4.0 → 1.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8df42aea1bd4eeeb476acbf102accaa55c206886b581df3f70e055accae95d41
4
- data.tar.gz: 8f1d81756ce2844917278e2c16f49665be362a427c3c1af3c00d8cc834b96f2b
3
+ metadata.gz: 02e353f4bcd5f1cad16b7bebee4a1d46dc226ece9768ffc6f08a05bc62448cab
4
+ data.tar.gz: f2d6ab2245a058aef8552669085b3086c682653cb1647f799af3a98c8e4b84c8
5
5
  SHA512:
6
- metadata.gz: e36f8d160d0644c4c66bd5723cdabef25db28ca5cae522dff70cb4299ea59c942d5190d0badc2374456b59db07dca09ac4f7c51bb22f8be261f67c932322cc79
7
- data.tar.gz: 8285500f4713be2467e28cb6a2e39605478babf072935a2a93f6de01dfe3338010fed463a094828f425d76a4c8c48124c09b68f1cdbb9ff9f13f3abbad2e5cb7
6
+ metadata.gz: 30f7dccc1823251bafb0dea416bca202c2870b1031e393c845d8f708ea27862eead58392fb6740455cb918785302fb614834bd0b7c05db8f3abc13211432bda4
7
+ data.tar.gz: 2c74e56f900a0d540359ce69ccf25afa232eea4a1a00ca39b8546877dd00d7cd40b1f9976fe5b6998da8f2f986a9b5b4872ec0112c4feb97dde547fbe263fd99
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [1.6.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.5.0...v1.6.0) - 2023-12-10
2
+
3
+ ### What's new
4
+
5
+ - Added tests for `measured_*` methods.
6
+
7
+ -----------
8
+
9
+ ## [1.5.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.4.0...v1.5.0) - 2023-11-27
10
+
11
+ ### What's new
12
+
13
+ - Raise error when columns for measurement attribute don't exist in DB.
14
+
15
+ -----------
16
+
1
17
  ## [1.4.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.3.0...v1.4.0) - 2023-11-25
2
18
 
3
19
  ### What's updated
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements-rails (1.4.0)
4
+ unit_measurements-rails (1.6.0)
5
5
  activemodel (>= 7)
6
6
  activerecord (>= 7)
7
7
  railties (>= 7)
@@ -163,7 +163,7 @@ GEM
163
163
  simplecov_json_formatter (~> 0.1)
164
164
  simplecov-html (0.12.3)
165
165
  simplecov_json_formatter (0.1.4)
166
- sqlite3 (1.6.8-x86_64-linux)
166
+ sqlite3 (1.6.9-x86_64-linux)
167
167
  thor (1.3.0)
168
168
  timeout (0.4.1)
169
169
  tzinfo (2.0.6)
@@ -184,7 +184,7 @@ DEPENDENCIES
184
184
  rake (~> 13.0)
185
185
  rspec (~> 3.0)
186
186
  simplecov (~> 0.21, >= 0.21.2)
187
- sqlite3 (~> 1.6)
187
+ sqlite3 (~> 1.6.9)
188
188
  unit_measurements-rails!
189
189
 
190
190
  BUNDLED WITH
data/README.md CHANGED
@@ -14,7 +14,7 @@ A Rails adaptor that encapsulate measurements and their units in Ruby on Rails.
14
14
  ## Introduction
15
15
 
16
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
17
+ It provides an `ActiveRecord` adapter for persisting and retrieving measurement quantity along with its unit, simplifying complex
18
18
  measurement handling within your Rails applications.
19
19
 
20
20
  ## Minimum Requirements
@@ -42,8 +42,8 @@ Or otherwise simply install it yourself as:
42
42
 
43
43
  ### ActiveRecord
44
44
 
45
- This gem provides an ActiveRecord integration allowing you to declare measurement attributes with their
46
- corresponding units in your database schema:
45
+ This gem provides an ActiveRecord integration allowing you to declare measurement
46
+ attributes along with their corresponding units in your database schema:
47
47
 
48
48
  ```ruby
49
49
  class CreateCubes < ActiveRecord::Migration[7.0]
@@ -76,6 +76,9 @@ cube.length_unit #=> "ft"
76
76
  cube.length #=> 5.0 ft
77
77
  ```
78
78
 
79
+ Attribute accessor names are expected to have the `_quantity` and `_unit` suffix,
80
+ and be `DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.
81
+
79
82
  You can specify multiple measurement attributes simultaneously:
80
83
 
81
84
  ```ruby
@@ -84,28 +87,36 @@ class Cube < ActiveRecord::Base
84
87
  end
85
88
  ```
86
89
 
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.
90
+ You can customize the quantity and unit accessors of the measurement attribute by
91
+ specifying them in the `quantity_attribute_name` and `unit_attribute_name` options,
92
+ respectively.
92
93
 
93
94
  ```ruby
94
95
  class CubeWithCustomAccessor < ActiveRecord::Base
95
- measured_length :length, unit_attribute_name: :length_uom
96
- measured_length :width, quantity_attribute_name: :width_value
96
+ measured UnitMeasurements::Length, :length, :width, :height, unit_attribute_name: :size_uom
97
+ measured UnitMeasurements::Weight, :weight, quantity_attribute_name: :width_quantity
97
98
  end
98
99
  ```
99
100
 
100
101
  For a more streamlined approach, predefined methods are available for commonly used
101
- types like `length`, `weight`, `area`, `volume`, etc.:
102
+ types:
103
+
104
+ | # | Unit group | Method name |
105
+ | :- | :---------- | :----------- |
106
+ | 1 | Length or distance | measured_length |
107
+ | 2 | Weight or mass | measured_weight |
108
+ | 3 | Time or duration | measured_time |
109
+ | 4 | Temperature | measured_temperature |
110
+ | 5 | Area | measured_area |
111
+ | 6 | Volume | measured_volume |
112
+ | 7 | Density | measured_density |
102
113
 
103
114
  ```ruby
104
- class Package < ActiveRecord::Base
115
+ class CubeWithPredefinedMethods < ActiveRecord::Base
105
116
  measured_length :size
106
- measured_area :carpet_area
107
- measured_volume :total_volume
108
- measured_weight :item_weight, :package_weight
117
+ measured_volume :volume
118
+ measured_weight :weight
119
+ measured_density :density
109
120
  end
110
121
  ```
111
122
 
@@ -121,4 +132,4 @@ Contributions to this project are welcomed! To contribute:
121
132
 
122
133
  ## License
123
134
 
124
- Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
135
+ Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -140,6 +140,8 @@ module UnitMeasurements
140
140
  # @since 1.0.0
141
141
  def define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
142
142
  define_method(measured_attr) do
143
+ column_exists?(quantity_attr) && column_exists?(unit_attr)
144
+
143
145
  quantity, unit = public_send(quantity_attr), public_send(unit_attr)
144
146
 
145
147
  begin
@@ -165,6 +167,8 @@ module UnitMeasurements
165
167
  # @since 1.0.0
166
168
  def define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
167
169
  define_method("#{measured_attr}=") do |measurement|
170
+ column_exists?(quantity_attr) && column_exists?(unit_attr)
171
+
168
172
  if measurement.is_a?(unit_group)
169
173
  public_send("#{quantity_attr}=", measurement.quantity)
170
174
  public_send("#{unit_attr}=", measurement.unit.name)
@@ -187,6 +191,8 @@ module UnitMeasurements
187
191
  # @since 1.0.0
188
192
  def redefine_quantity_writer(quantity_attr)
189
193
  redefine_method("#{quantity_attr}=") do |quantity|
194
+ column_exists?(quantity_attr)
195
+
190
196
  quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
191
197
  if quantity
192
198
  db_column_props = self.column_for_attribute(quantity_attr)
@@ -212,6 +218,8 @@ module UnitMeasurements
212
218
  # @since 1.0.0
213
219
  def redefine_unit_writer(unit_attr, unit_group)
214
220
  redefine_method("#{unit_attr}=") do |unit|
221
+ column_exists?(unit_attr)
222
+
215
223
  unit_name = unit_group.unit_for(unit).try!(:name)
216
224
  write_attribute(unit_attr, (unit_name || unit))
217
225
  end
@@ -33,6 +33,7 @@ module UnitMeasurements
33
33
  end
34
34
  end
35
35
 
36
+ require "unit_measurements/rails/helpers"
36
37
  require "unit_measurements/rails/active_record"
37
38
 
38
39
  require "unit_measurements/rails/railtie" if defined?(Rails)
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ module Rails
7
+ # The +UnitMeasurements::Rails::Helpers+ module provides helper methods for
8
+ # handling +ActiveRecord+ models in the context of unit measurements.
9
+ #
10
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
11
+ # @since 1.5.0
12
+ module Helpers
13
+ # Checks the existence of a +column_name+ within the database table
14
+ # associated with the +ActiveRecord+ model.
15
+ #
16
+ # @param [String] column_name The name of the column to check for existence.
17
+ #
18
+ # @raise [ArgumentError]
19
+ # If the specified column does not exist in the database table.
20
+ #
21
+ # @example Check if the +height_quantity+ column exists in the database table:
22
+ # column_exists?("height_quantity")
23
+ #
24
+ # @return [void]
25
+ #
26
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
27
+ # @since 1.5.0
28
+ def column_exists?(column_name)
29
+ unless self.class.column_names.include?(column_name)
30
+ raise ArgumentError, "Column '#{column_name}' does not exist in the database for table '#{self.class.table_name}'."
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # ActiveSupport hook to include ActiveRecord with the `UnitMeasurements::Rails::Helpers`
38
+ # module.
39
+ ActiveSupport.on_load(:active_record) do
40
+ ::ActiveRecord::Base.send(:include, UnitMeasurements::Rails::Helpers)
41
+ end
@@ -5,6 +5,6 @@
5
5
  module UnitMeasurements
6
6
  module Rails
7
7
  # Current stable version.
8
- VERSION = "1.4.0"
8
+ VERSION = "1.6.0"
9
9
  end
10
10
  end
@@ -43,7 +43,7 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency "rspec", "~> 3.0"
44
44
  spec.add_development_dependency "simplecov", "~> 0.21", ">= 0.21.2"
45
45
  spec.add_development_dependency "byebug", "~> 11"
46
- spec.add_development_dependency "sqlite3", "~> 1.6"
46
+ spec.add_development_dependency "sqlite3", "~> 1.6.9"
47
47
 
48
48
  spec.required_ruby_version = ">= 3.2"
49
49
  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.0
4
+ version: 1.6.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-25 00:00:00.000000000 Z
11
+ date: 2023-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -134,14 +134,14 @@ dependencies:
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: '1.6'
137
+ version: 1.6.9
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '1.6'
144
+ version: 1.6.9
145
145
  description: A Rails adaptor that encapsulate measurements and their units in Ruby
146
146
  on Rails.
147
147
  email:
@@ -167,6 +167,7 @@ files:
167
167
  - lib/unit_measurements-rails.rb
168
168
  - lib/unit_measurements/rails/active_record.rb
169
169
  - lib/unit_measurements/rails/base.rb
170
+ - lib/unit_measurements/rails/helpers.rb
170
171
  - lib/unit_measurements/rails/railtie.rb
171
172
  - lib/unit_measurements/rails/unit_groups/all.rb
172
173
  - lib/unit_measurements/rails/unit_groups/area.rb