Class: Measured::Measurable

Inherits:
Object
  • Object
show all
Includes:
Comparable, Arithmetic
Defined in:
lib/measured/measurable.rb

Direct Known Subclasses

CaseSensitiveMeasurable, Length, Weight

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arithmetic

#*, #+, #-, #-@, #/, #coerce

Constructor Details

#initialize(value, unit) ⇒ Measurable

Returns a new instance of Measurable.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/measured/measurable.rb', line 9

def initialize(value, unit)
  raise Measured::UnitError, "Unit cannot be blank" if unit.blank?
  raise Measured::UnitError, "Unit #{ unit } does not exist" unless self.class.conversion.unit_or_alias?(unit)

  @value = case value
  when NilClass
    raise Measured::UnitError, "Unit value cannot be nil"
  when Float
    BigDecimal(value, Float::DIG+1)
  when BigDecimal
    value
  else
    if value.blank?
      raise Measured::UnitError, "Unit value cannot be blank"
    else
      BigDecimal(value)
    end
  end

  @unit = self.class.conversion.to_unit_name(unit)
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



5
6
7
# File 'lib/measured/measurable.rb', line 5

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/measured/measurable.rb', line 5

def value
  @value
end

Class Method Details

.conversionObject



81
82
83
# File 'lib/measured/measurable.rb', line 81

def conversion
  @conversion ||= Measured::Conversion.new
end

.nameObject



97
98
99
# File 'lib/measured/measurable.rb', line 97

def name
  to_s.split("::").last.underscore.humanize.downcase
end

.unitsObject



85
86
87
# File 'lib/measured/measurable.rb', line 85

def units
  conversion.unit_names
end

.units_with_aliasesObject



93
94
95
# File 'lib/measured/measurable.rb', line 93

def units_with_aliases
  conversion.unit_names_with_aliases
end

.valid_unit?(unit) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/measured/measurable.rb', line 89

def valid_unit?(unit)
  conversion.unit_or_alias?(unit)
end

Instance Method Details

#<=>(other) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/measured/measurable.rb', line 57

def <=>(other)
  if other.is_a?(self.class)
    other_converted = other.convert_to(unit)
    value <=> other_converted.value
  elsif other == 0
    other_converted = self.class.new(0, unit)
    value <=> other_converted.value
  end
end

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



67
68
69
70
71
72
73
74
75
# File 'lib/measured/measurable.rb', line 67

def ==(other)
  if other.is_a?(self.class)
    other_converted = other.convert_to(unit)
    value == other_converted.value
  elsif other == 0
    other_converted = self.class.new(0, unit)
    value == other_converted.value
  end
end

#convert_to(new_unit) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/measured/measurable.rb', line 31

def convert_to(new_unit)
  new_unit_name = self.class.conversion.to_unit_name(new_unit)
  return self if new_unit_name == self.class.conversion.to_unit_name(unit)

  value = self.class.conversion.convert(@value, from: @unit, to: new_unit_name)

  self.class.new(value, new_unit)
end

#convert_to!(new_unit) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/measured/measurable.rb', line 40

def convert_to!(new_unit)
  converted = convert_to(new_unit)

  @value = converted.value
  @unit = converted.unit

  self
end

#inspectObject



53
54
55
# File 'lib/measured/measurable.rb', line 53

def inspect
  "#<#{ self.class }: #{ value } #{ unit }>"
end

#to_sObject



49
50
51
# File 'lib/measured/measurable.rb', line 49

def to_s
  [value.to_f.to_s.gsub(/\.0\Z/, ""), unit].join(" ")
end