Class: Quantified::Attribute

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

Direct Known Subclasses

Length, Mass

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



42
43
44
45
46
47
48
49
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 42

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, self.unit)
    self.class.new(amount.send(meth, other_amount_in_self_units), self.unit)
  else
    amount.send(meth, *args)
  end
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_methodsObject



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

def add_numeric_methods;     @add_numeric_methods ||= false; end

.add_numeric_methods=(v) ⇒ Object



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

def add_numeric_methods=(v); @add_numeric_methods = v;       end

.add_numeric_methods?Boolean

Returns:

  • (Boolean)


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

def self.add_numeric_methods?
  self.add_numeric_methods
end

.conversion_rate(from, to) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 51

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

.conversionsObject



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

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

.current_systemObject



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

def current_system;          @current_system;                end

.current_system=(v) ⇒ Object



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

def current_system=(v);      @current_system = v;            end

.non_primitivesObject



83
84
85
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 83

def self.non_primitives
  self.conversions.keys
end

.numeric_methods(*args) ⇒ Object



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

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

.primitivesObject



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

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

.systemsObject



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

def self.systems
  self.systems_to_units.keys
end

.systems_to_unitsObject



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

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

.units(system = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/vendor/quantified/lib/quantified/attribute.rb', line 75

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

.units_to_systemsObject



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

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(self.amount, self.unit, other.unit) <=> other.amount
  else
    self.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(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.unit == other.unit) || BigDecimal.new(self.class.convert(self.amount, self.unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s)
rescue NoMethodError
  self.amount == other
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(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.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