Class: Stick::Units::BaseUnit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, converter = Units::Converter.current) ⇒ BaseUnit

Returns a new instance of BaseUnit.

Raises:

  • (::ArgumentError)


142
143
144
145
146
147
# File 'lib/stick/units/base.rb', line 142

def initialize(name, converter = Units::Converter.current)
  name = name.to_sym
  raise ::ArgumentError, "unit #{name.to_s.dump} not registered with #{converter}" if not converter.registered? name
  @name = name
  @converter = converter
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



140
141
142
# File 'lib/stick/units/base.rb', line 140

def converter
  @converter
end

#nameObject (readonly)

Returns the value of attribute name.



140
141
142
# File 'lib/stick/units/base.rb', line 140

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



153
154
155
# File 'lib/stick/units/base.rb', line 153

def ==(other)
  other.is_a?(Units::BaseUnit) && other.name == @name && other.converter == @converter
end

#conversionObject



149
150
151
# File 'lib/stick/units/base.rb', line 149

def conversion
  @converter.send(:conversions, @name)
end

#hashObject



159
160
161
# File 'lib/stick/units/base.rb', line 159

def hash
  @name.hash ^ @converter.hash
end

#to_sObject Also known as: inspect



163
164
165
166
167
168
169
# File 'lib/stick/units/base.rb', line 163

def to_s
  if Units::Converter.current.includes?(converter)
    @name.to_s
  else
    "#{@converter}:#{@name}"
  end
end