-
Notifications
You must be signed in to change notification settings - Fork 2
5.14.0 #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
5.14.0 #67
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
unit_measurements (5.13.0) | ||
unit_measurements (5.14.0) | ||
activesupport (~> 7.0) | ||
|
||
GEM | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module UnitMeasurements | ||
# This module provides methods to define +Numeric+ extension methods for a list | ||
# of units within a unit group. If units are empty, it defaults to defining | ||
# methods for all units in the unit group. | ||
# | ||
# This module is included in the +Measurement+ class to allow defining numeric | ||
# extension methods for specified units. | ||
# | ||
# @see Measurement | ||
# | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.14.0 | ||
module NumericMethods | ||
# @scope class | ||
# Defines +Numeric+ extension methods for specified units within the unit | ||
# group. If units are empty, it defaults to defining methods for all units | ||
# within the unit group. | ||
# | ||
# @param [Array<String|Symbol>] units | ||
# An array of units' names for which numeric methods need to be defined. | ||
# If empty, methods will be defined for all units in the unit group. | ||
# | ||
# @return [Array<Unit>] An array of units for which methods are defined. | ||
# | ||
# @example Define numeric methods for metres, centimetres, and millimetres: | ||
# UnitMeasurements::Length.define_numeric_methods("metres", :cm, :mm) | ||
# | ||
# @example Define numeric methods for all units in the unit group: | ||
# UnitMeasurements::Length.define_numeric_methods | ||
# | ||
# @see #define_numeric_method_for | ||
# | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.14.0 | ||
def define_numeric_methods(*units) | ||
unit_group = self | ||
units = units.empty? ? unit_group.units : units | ||
|
||
units.inject([]) do |units, unit| | ||
units << define_numeric_method_for(unit, unit_group) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @private | ||
# @scope class | ||
# This method defines a numeric method for a specific unit within a unit group. | ||
# The method is defined dynamically using +define_method+ and associates the | ||
# unit with the numeric value. | ||
# | ||
# @param [String|Symbol|Unit] unit | ||
# The unit for which the numeric method is defined. | ||
# @param [UnitGroup] unit_group The unit group to which the unit belongs. | ||
# | ||
# @return [Unit] The unit instance for which the method was defined. | ||
# | ||
# @see #define_numeric_methods | ||
# | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.14.0 | ||
def define_numeric_method_for(unit, unit_group) | ||
unit = unit.is_a?(Unit) ? unit : unit_group.unit_for!(unit) | ||
|
||
unit.names.each do |method_name| | ||
# Check if the name contains alphabetic characters | ||
next unless method_name =~ /^[a-zA-Z]+$/ | ||
|
||
Numeric.define_method(method_name) do | ||
unit_group.new(self, unit) | ||
end | ||
end | ||
|
||
unit | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
|
||
module UnitMeasurements | ||
# Current stable version. | ||
VERSION = "5.13.0" | ||
VERSION = "5.14.0" | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
# spec/unit_measurements/extras/numeric_methods_spec.rb | ||
|
||
RSpec.describe UnitMeasurements::NumericMethods do | ||
let(:unit_group) { UnitMeasurements::Length } | ||
let(:m) { unit_group.unit_for!(:m) } | ||
let(:cm) { unit_group.unit_for!(:cm) } | ||
|
||
context "when units are specified" do | ||
it "defines extension methods for specified units" do | ||
unit_group.define_numeric_methods("m", "cm") | ||
|
||
expect(Numeric.method_defined?("m")).to be_truthy | ||
expect(Numeric.method_defined?("cm")).to be_truthy | ||
|
||
expect(1.cm).to be_instance_of(unit_group) | ||
end | ||
end | ||
|
||
context "when units are specified" do | ||
it "defines extension methods for all units within the unit group" do | ||
unit_group.define_numeric_methods | ||
|
||
expect(Numeric.method_defined?("ft")).to be_truthy | ||
expect(Numeric.method_defined?("in")).to be_truthy | ||
|
||
expect(1.ft).to be_instance_of(unit_group) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.