Class: Medidas::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/medidas/unit.rb

Direct Known Subclasses

BaseUnit, DerivedUnit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_unitObject (readonly)

Returns the value of attribute base_unit.



3
4
5
# File 'lib/medidas/unit.rb', line 3

def base_unit
  @base_unit
end

Class Method Details

.registered_unitsObject



5
6
7
# File 'lib/medidas/unit.rb', line 5

def self.registered_units
  @registered_units ||= {}
end

Instance Method Details

#*(number) ⇒ Object



9
10
11
# File 'lib/medidas/unit.rb', line 9

def *(number)
  Measure.for(amount: number, unit: self)
end

#base_unit?(a_unit) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/medidas/unit.rb', line 13

def base_unit?(a_unit)
  a_unit == base_unit
end

#coerce(amount) ⇒ Object

Raises:

  • (TypeError)


25
26
27
28
29
# File 'lib/medidas/unit.rb', line 25

def coerce(amount)
  raise TypeError, "Unit can't be coerced into Integer except for multiplication" unless caller.first.include?("`*'")

  [self, amount]
end

#inspectObject



21
22
23
# File 'lib/medidas/unit.rb', line 21

def inspect
  "#{self.class}(#{@name})"
end

#name_for(amount) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/medidas/unit.rb', line 31

def name_for(amount)
  if amount == 1
    @name
  else
    @plural
  end
end

#to_sObject



17
18
19
# File 'lib/medidas/unit.rb', line 17

def to_s
  @name
end