-
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 #68 from shivam091/5.15.0
5.15.0
- Loading branch information
Showing
10 changed files
with
210 additions
and
44 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.14.0) | ||
unit_measurements (5.15.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,87 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module UnitMeasurements | ||
# This module provides functionality to define conversion 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. These methods allow easy conversion | ||
# between different units within a given unit group. | ||
# | ||
# This module is included in the +Measurement+ class to allow defining conversion | ||
# methods for specified units. | ||
# | ||
# @see Measurement | ||
# | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.15.0 | ||
module ConversionMethods | ||
# @scope class | ||
# Defines conversion methods for specified +units+ within the unit group. | ||
# If +units+ are empty, it defaults to defining methods for all units within | ||
# the unit group. | ||
# | ||
# @example Define conversion methods for metres, centimetres, and millimetres: | ||
# UnitMeasurements::Length.define_conversion_methods("metres", :cm, :mm) | ||
# | ||
# @example Define conversion methods for all units within the unit group: | ||
# UnitMeasurements::Length.define_conversion_methods | ||
# | ||
# @param [Array<String|Symbol>, optional] units | ||
# An array of units' names for which conversion methods need to be defined. | ||
# If empty, methods will be defined for all units within the unit group. | ||
# | ||
# @return [Array<Unit>] | ||
# An array of units for which the conversion methods were defined. | ||
# | ||
# @note | ||
# This method defines a conversion methods specifically for units that contain | ||
# alphabetic characters in their names. | ||
# | ||
# @see .define_conversion_method_for | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.15.0 | ||
def define_conversion_methods(*units) | ||
unit_group = self | ||
units = units.empty? ? unit_group.units : units | ||
|
||
units.inject([]) do |units, unit| | ||
units << define_conversion_method_for(unit, unit_group) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @private | ||
# @scope class | ||
# Defines conversion methods for a specific +unit+ within a +unit_group+. | ||
# These methods are defined dynamically using +define_method+. | ||
# | ||
# @param [String|Symbol|Unit] unit | ||
# The unit (or its name) for which the conversion methods need to be defined. | ||
# @param [UnitGroup] unit_group The unit group to which the unit belongs. | ||
# | ||
# @return [Unit] | ||
# The unit instance for which the conversion methods were defined. | ||
# | ||
# @see .define_conversion_methods | ||
# @author {Harshal V. Ladhe}[https://shivam091.github.io/] | ||
# @since 5.15.0 | ||
def define_conversion_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]+$/ | ||
|
||
define_method("in_#{method_name}") do |use_cache: false| | ||
convert_to(unit, use_cache: use_cache) | ||
end | ||
alias_method "to_#{method_name}", "in_#{method_name}" | ||
alias_method "as_#{method_name}", "in_#{method_name}" | ||
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
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.14.0" | ||
VERSION = "5.15.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,41 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
# spec/unit_measurements/extras/conversion_methods_spec.rb | ||
|
||
RSpec.describe UnitMeasurements::ConversionMethods do | ||
let(:unit_group) { UnitMeasurements::Length } | ||
let(:measurement) { unit_group.new(1, "ft") } | ||
|
||
context "when units are specified" do | ||
it "defines methods for specified units" do | ||
unit_group.define_conversion_methods("m", "cm") | ||
|
||
expect(unit_group.method_defined?("to_m")).to be_truthy | ||
expect(unit_group.method_defined?("to_cm")).to be_truthy | ||
|
||
expect(measurement).to respond_to(:to_m) | ||
expect(measurement).to respond_to(:in_m) | ||
expect(measurement).to respond_to(:as_m) | ||
|
||
expect(measurement.to_m.to_s).to eq("0.3048 m") | ||
end | ||
end | ||
|
||
context "when units are specified" do | ||
|
||
it "defines methods for all units within the unit group" do | ||
unit_group.define_conversion_methods | ||
|
||
expect(unit_group.method_defined?("to_ft")).to be_truthy | ||
expect(unit_group.method_defined?("to_in")).to be_truthy | ||
|
||
expect(measurement).to respond_to(:to_in) | ||
expect(measurement).to respond_to(:to_inch) | ||
expect(measurement).to respond_to(:to_inches) | ||
|
||
expect(measurement.to_in.to_s).to eq("12.0 in") | ||
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