Module: Quantify::ExtendedMethods
- Included in:
- Unit, Unit::Base
- Defined in:
- lib/quantify/quantify.rb
Instance Method Summary collapse
-
#method_missing(method, *args, &block) ⇒ Object
Provides syntactic sugar for accessing units via the #for method.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Provides syntactic sugar for accessing units via the #for method. Specify:
Unit.degree_celsius
rather than Unit.for :degree_celsius
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/quantify/quantify.rb', line 16 def method_missing(method, *args, &block) if method.to_s =~ /((si|non_si|compound)_)?(non_(prefixed)_)?((base|derived|benchmark)_)?units(_by_(name|symbol|label))?/ if $2 || $4 || $6 conditions = [] conditions << "unit.is_#{$2}_unit?" if $2 conditions << "!unit.is_prefixed_unit?" if $4 conditions << "unit.is_#{$6}_unit?" if $6 units = Unit.units.select { |unit| instance_eval(conditions.join(" and ")) } else units = Unit.units end return_format = ( $8 ? $8.to_sym : nil ) units.map(&return_format).to_a elsif unit = Unit.for(method) return unit else super end end |