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

Overloads:

  • #dimFloat

    Returns:

    • (Float)
  • #dim=(value) ⇒ Object

    Parameters:

    • value (Float)


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

def dim
  @dim
end

#unitSymbol #unit=(value) ⇒ Object

Unit

Overloads:

  • #unitSymbol

    Returns any of UNITS.

    Returns:

    • (Symbol)

      any of UNITS

  • #unit=(value) ⇒ Object

    Parameters:

    • value (Symbol)

      any of UNITS



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

Returns:

  • (String)


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

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

#to_ftAIXM::D

Convert dimension

Returns:

  • (AIXM::D)

    converted 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

Returns:

  • (AIXM::D)

    converted 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

Returns:

  • (AIXM::D)

    converted 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

Returns:

  • (AIXM::D)

    converted 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

Returns human readable representation (e.g. “123.0 m”).

Returns:

  • (String)

    human readable representation (e.g. “123.0 m”)



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

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

#zero?Boolean

Whether dimension is zero.

Returns:

  • (Boolean)


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

def_delegator :@dim, :zero?