-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from shivam091/5.14.0
5.14.0
- Loading branch information
Showing
8 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |