Class: Antex::Measurable

Inherits:
Object
  • Object
show all
Defined in:
lib/antex/measurable.rb

Overview

Implements an easy to use container for storing measures and retrieving them performing unit conversions.

Measures should be a hash with symbolic keys and numeric values with uniform units.

mea = Measurable.new
mea.measures = { km: 1, mi: 0.621 }

Direct Known Subclasses

SVGBox, SetBox, TexBox

Defined Under Namespace

Classes: InvalidUnit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMeasurable

Returns a new instance of Measurable.



14
15
16
17
# File 'lib/antex/measurable.rb', line 14

def initialize
  @default_unit = nil
  @measures = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)

Once measures are set they can be accessed and converted.

mea.km(:km) # => 1.0
mea.km(:mi) # => 1.610...
mea.mi(:mi) # => 1.0
mea.mi(:km) # => 0.621

You can set a default unit for quicker access.

mea.default_unit = :mi
mea.km # => 1.610...
mea.mi # => 1.0


62
63
64
65
66
# File 'lib/antex/measurable.rb', line 62

def method_missing(method_name, *arguments, &block)
  return super unless @measures.include? method_name

  send :calculate, method_name, *arguments, &block
end

Instance Attribute Details

#default_unitSymbol

Returns the default unit.

Returns:

  • (Symbol)

    the default unit



20
21
22
# File 'lib/antex/measurable.rb', line 20

def default_unit
  @default_unit
end

#measuresHash

Note:

When setting, the default unit will be set to nil.

Returns the stored measures.

Returns:

  • (Hash)

    the stored measures



31
32
33
# File 'lib/antex/measurable.rb', line 31

def measures
  @measures
end