Class: Alchemist::CompoundNumericConversion
- Inherits:
-
Object
- Object
- Alchemist::CompoundNumericConversion
- Defined in:
- lib/alchemist.rb
Instance Attribute Summary collapse
-
#denominators ⇒ Object
Returns the value of attribute denominators.
-
#numerators ⇒ Object
Returns the value of attribute numerators.
Instance Method Summary collapse
- #*(value) ⇒ Object
- #consolidate ⇒ Object
-
#initialize(numerator) ⇒ CompoundNumericConversion
constructor
A new instance of CompoundNumericConversion.
- #method_missing(method, *attrs, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(numerator) ⇒ CompoundNumericConversion
Returns a new instance of CompoundNumericConversion.
360 361 362 363 364 |
# File 'lib/alchemist.rb', line 360 def initialize(numerator) @coefficient = 1 #* numerator.to_f @numerators = [numerator] @denominators = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *attrs, &block) ⇒ Object
402 403 404 405 406 407 |
# File 'lib/alchemist.rb', line 402 def method_missing(method, *attrs, &block) if Conversions[method] @denominators << 1.send(method) consolidate end end |
Instance Attribute Details
#denominators ⇒ Object
Returns the value of attribute denominators.
359 360 361 |
# File 'lib/alchemist.rb', line 359 def denominators @denominators end |
#numerators ⇒ Object
Returns the value of attribute numerators.
359 360 361 |
# File 'lib/alchemist.rb', line 359 def numerators @numerators end |
Instance Method Details
#*(value) ⇒ Object
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/alchemist.rb', line 365 def *(value) case value when Numeric @coefficient *= value self when Alchemist::NumericConversion @numerators << value return consolidate end end |
#consolidate ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/alchemist.rb', line 376 def consolidate @numerators.each_with_index do |numerator, n| @denominators.each_with_index do |denominator, d| next if numerator.is_a?(Numeric) next if denominator.is_a?(Numeric) if (Conversions[numerator.unit_name] & Conversions[denominator.unit_name]).length > 0 value = numerator / denominator @numerators.delete_at(n) @denominators.delete_at(d) @coefficient *= value end end end if @denominators.length == 0 && @numerators.length == 1 @numerators[0] * @coefficient elsif @denominators.length == 0 && @numerators.length == 0 @coefficient else self end end |
#to_s ⇒ Object
398 399 400 |
# File 'lib/alchemist.rb', line 398 def to_s end |