Class: AIXM::D

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Concerns::HashEquality, Comparable
Defined in:
lib/aixm/d.rb

Overview

Dimension, distance or length

Examples:

AIXM.d(123, :m)

Constant Summary collapse

UNITS =
{
  ft: { km: 0.0003048, m: 0.3048, nm: 0.000164578833554 },
  km: { ft: 3280.839895, m: 1000, nm: 0.539956803 },
  m: { ft: 3.280839895, km: 0.001, nm: 0.000539956803 },
  nm: { ft: 6076.11548554, km: 1.852, m: 1852 }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HashEquality

#eql?, #hash

Constructor Details

#initialize(dim, unit) ⇒ D

See the overview for examples.



44
45
46
# File 'lib/aixm/d.rb', line 44

def initialize(dim, unit)
  self.dim, self.unit = dim, unit
end

Instance Attribute Details

#dimFloat #dim=(value) ⇒ Object

Dimension



33
34
35
# File 'lib/aixm/d.rb', line 33

def dim
  @dim
end

#unitSymbol #unit=(value) ⇒ Object

Unit



41
42
43
# File 'lib/aixm/d.rb', line 41

def unit
  @unit
end

Instance Method Details

#<=>(other) ⇒ Object

See Also:

  • Object#<=>


84
85
86
# File 'lib/aixm/d.rb', line 84

def <=>(other)
  dim <=> other.send(:"to_#{unit}").dim
end

#==(other) ⇒ Object

See Also:

  • Object#==


89
90
91
# File 'lib/aixm/d.rb', line 89

def ==(other)
  self.class === other  && (self <=> other).zero?
end

#inspectString



49
50
51
# File 'lib/aixm/d.rb', line 49

def inspect
  %Q(#<#{self.class} #{to_s}>)
end

#to_ftAIXM::D

Convert dimension



76
77
78
79
80
81
# File 'lib/aixm/d.rb', line 76

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_kmAIXM::D

Convert dimension



76
77
78
79
80
81
# File 'lib/aixm/d.rb', line 76

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_mAIXM::D

Convert dimension



76
77
78
79
80
81
# File 'lib/aixm/d.rb', line 76

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_nmAIXM::D

Convert dimension



76
77
78
79
80
81
# File 'lib/aixm/d.rb', line 76

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_sString



54
55
56
# File 'lib/aixm/d.rb', line 54

def to_s
  [dim, unit].join(' '.freeze)
end

#zero?Boolean

Whether dimension is zero.



25
# File 'lib/aixm/d.rb', line 25

def_delegator :@dim, :zero?