Module: SY::SignedMagnitude

Defined in:
lib/sy/signed_magnitude.rb

Overview

Qualities specific to relative magnitudes (mixin).

Instance Method Summary collapse

Instance Method Details

#+(m2) ⇒ Object

Addition.

TODO: Figure out which module comes on the top in Quantity@Magnitude, whether Magnitude or SignedMagnitude, and therefore, whether it is necessary to adjust this method.



34
35
36
37
38
39
40
# File 'lib/sy/signed_magnitude.rb', line 34

def + m2
  return magnitude( amount + m2.amount ) if quantity == m2.quantity
  return quantity.absolute.magnitude( amount + m2.amount ) if
    quantity.absolute == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 + compat_2
end

#-(m2) ⇒ Object

Subtraction.

TODO: ditto



45
46
47
48
49
50
51
# File 'lib/sy/signed_magnitude.rb', line 45

def - m2
  return magnitude( amount - m2.amount ) if m2.quantity == quantity.relative
  return quantity.relative.magnitude( amount - m2.amount ) if
    quantity == m2.quantity
  compat_1, compat_2 = m2.coerce self
  return compat_1 - compat_2
end

#initialize(of: nil, amount: nil) ⇒ Object

Relative magnitude constructor takes :quantity (alias :of) argument and :amount argument. Amount is allowed to be negative.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sy/signed_magnitude.rb', line 8

def initialize( of: nil, amount: nil )
  puts "Constructing AbsoluteMagnitude of #{of}, amount: #{amount}" if SY::DEBUG
  fail ArgumentError, "Quantity (:of) argument missing!" if of.nil?
  @quantity = of
  @amount = case amount
            when Numeric then
              puts "This amount is a Numeric, using it directly" if SY::DEBUG
              amount
            when nil then
              puts "This amount is 'nil', using 1 instead" if SY::DEBUG
              1
            else
              begin
                puts "Amount #{amount} will be reframed to #{@quantity}" if SY::DEBUG
                amount.( @quantity ).amount
              rescue NameError, NoMethodError
                puts "fail, amount #{amount} will be used directly" if SY::DEBUG
                amount
              end
            end
end