Class: GeneralUnits::Base::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/general_units/units/base/unit.rb

Direct Known Subclasses

Length::Unit, Volume::Unit, Weight::Unit

Constant Summary collapse

METRIC_SYSTEMS =
[:metric, :english, :american]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name, short, fractional, system = :metric) ⇒ Unit

Returns a new instance of Unit.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/general_units/units/base/unit.rb', line 8

def initialize(code, name, short, fractional, system = :metric)
  @code = code
  @name = name.to_s
  @short = short.to_s
  @fractional = fractional.to_d
  @system = system.to_sym
  METRIC_SYSTEMS.each do |s|
    class_eval do
      define_method "#{s}?" do
        self.system == s
      end
    end
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/general_units/units/base/unit.rb', line 6

def code
  @code
end

#fractionalObject (readonly)

Returns the value of attribute fractional.



6
7
8
# File 'lib/general_units/units/base/unit.rb', line 6

def fractional
  @fractional
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/general_units/units/base/unit.rb', line 6

def name
  @name
end

#shortObject (readonly)

Returns the value of attribute short.



6
7
8
# File 'lib/general_units/units/base/unit.rb', line 6

def short
  @short
end

#systemObject (readonly)

Returns the value of attribute system.



6
7
8
# File 'lib/general_units/units/base/unit.rb', line 6

def system
  @system
end

Instance Method Details

#inspectObject



27
28
29
# File 'lib/general_units/units/base/unit.rb', line 27

def inspect
  code
end

#to_sObject



23
24
25
# File 'lib/general_units/units/base/unit.rb', line 23

def to_s
  name
end