Class: Quantified::Attribute

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/vendor/quantified/lib/quantified/attribute.rb

Direct Known Subclasses

Length, Mass

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, unit) ⇒ Attribute

Returns a new instance of Attribute

Raises:

  • (ArgumentError)

7
8
9
10
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 7

def initialize(amount, unit)
  raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric)
  @amount, @unit = amount, unit.to_sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 46

def method_missing(meth, *args)
  if args.size == 1 && self.class == (other = args.first).class
    other_amount_in_self_units = self.class.convert(other.amount, other.unit, unit)
    self.class.new(amount.send(meth, other_amount_in_self_units), unit)
  else
    amount.send(meth, *args)
  end
end

Class Attribute Details

.add_numeric_methodsObject


109
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 109

def add_numeric_methods;     @add_numeric_methods ||= false; end

.current_systemObject

Returns the value of attribute current_system


112
113
114
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 112

def current_system
  @current_system
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount


5
6
7
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 5

def amount
  @amount
end

#unitObject (readonly)

Returns the value of attribute unit


5
6
7
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 5

def unit
  @unit
end

Class Method Details

.add_numeric_methods?Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 95

def self.add_numeric_methods?
  add_numeric_methods
end

.conversion_rate(from, to) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 55

def self.conversion_rate(from, to)
  return nil unless conversions[from] and conversions[to]
  conversions[from][to] ||=
  (1.0 / conversions[to][from] if conversions[to][from]) || begin
    shared_conversions = conversions[from].keys & conversions[to].keys
    if shared_conversions.any?
      primitive = shared_conversions.first
      conversions[from][primitive] * (1.0 / conversions[to][primitive])
    else
      conversions[from].each do |conversion_unit, multiple|
        if conversions[to].include?(conversion_unit)
          return multiple * conversion_rate(conversion) * (1.0 / conversions[to][conversion_unit])
        end
      end
      from_primitive = (conversions[from].keys & primitives).first
      to_primitive = (conversions[to].keys & primitives).first
      if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
        return conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / conversions[to][to_primitive])
      end
      raise StandardError, "No conversion path from #{from} to #{to}"
    end
  end
end

.conversionsObject


111
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 111

def conversions;             @conversions ||= {};            end

.non_primitivesObject


87
88
89
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 87

def self.non_primitives
  conversions.keys
end

.numeric_methods(*args) ⇒ Object


99
100
101
102
103
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 99

def self.numeric_methods(*args)
  args.each do |arg|
    add_numeric_method_for(arg.to_sym)
  end
end

.primitivesObject


108
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 108

def primitives;              @primitives ||= [];             end

.systemsObject


91
92
93
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 91

def self.systems
  systems_to_units.keys
end

.systems_to_unitsObject


114
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 114

def systems_to_units;        @systems_to_units ||= {};       end

.units(system = nil) ⇒ Object


79
80
81
82
83
84
85
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 79

def self.units(system = nil)
  if system
    systems_to_units[system.to_sym].dup
  else
    primitives | conversions.keys
  end
end

.units_to_systemsObject


115
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 115

def units_to_systems;        @units_to_systems ||= {};       end

Instance Method Details

#<=>(other) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 30

def <=>(other)
  if self.class == other.class
    self.class.convert(amount, unit, other.unit) <=> other.amount
  else
    amount <=> other
  end
end

#==(other) ⇒ Object


20
21
22
23
24
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 20

def ==(other)
  (BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit) || BigDecimal.new(self.class.convert(amount, unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s)
rescue NoMethodError
  amount == other
end

#coerce(other) ⇒ Object


42
43
44
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 42

def coerce(other)
  [other, amount]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 26

def eql?(other)
  self.class == other.class && BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit
end

#inspectObject


16
17
18
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 16

def inspect
  "#<#{self.class.name}: #{amount} #{unit}>"
end

#systemObject


38
39
40
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 38

def system
  self.class.units_to_systems[unit]
end

#to_sObject


12
13
14
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 12

def to_s
  "#{amount} #{unit}"
end