Class: Measured::Measurable

Inherits:
Numeric
  • Object
show all
Extended by:
Forwardable
Includes:
Arithmetic
Defined in:
lib/measured/measurable.rb

Constant Summary collapse

DEFAULT_FORMAT_STRING =
'%.2<value>f %<unit>s'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arithmetic

#+, #-, #-@, #coerce, #scale, #to_i

Constructor Details

#initialize(value, unit) ⇒ Measurable

Returns a new instance of Measurable.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/measured/measurable.rb', line 8

def initialize(value, unit)
  raise Measured::UnitError, "Unit value cannot be blank" if value.blank?

  @unit = unit_from_unit_or_name!(unit)
  @value = case value
  when Float
    BigDecimal(value, Float::DIG)
  when BigDecimal, Rational
    value
  when Integer
    Rational(value)
  else
    BigDecimal(value)
  end
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



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

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.nameObject



75
76
77
# File 'lib/measured/measurable.rb', line 75

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

.parse(string) ⇒ Object



79
80
81
# File 'lib/measured/measurable.rb', line 79

def parse(string)
  new(*Measured::Parser.parse_string(string))
end

.unit_systemObject



67
68
69
# File 'lib/measured/measurable.rb', line 67

def unit_system
  raise "`Measurable` does not have a `unit_system` object. You cannot directly subclass `Measurable`. Instead, build a new unit system by calling `Measured.build`."
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  if other.is_a?(self.class)
    value <=> other.convert_to(unit).value
  else
    nil
  end
end

#convert_to(new_unit) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/measured/measurable.rb', line 24

def convert_to(new_unit)
  new_unit = unit_from_unit_or_name!(new_unit)
  return self if new_unit == unit

  new_value = unit.unit_system.convert(value, from: unit, to: new_unit)

  self.class.new(new_value, new_unit)
end

#format(format_string = nil) ⇒ Object



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

def format(format_string=nil)
  kwargs = {
    value: self.value,
    unit: self.unit,
  }
  (format_string || DEFAULT_FORMAT_STRING) % kwargs
end

#humanizeObject



45
46
47
48
49
50
# File 'lib/measured/measurable.rb', line 45

def humanize
  @humanize ||= begin
    unit_string = value == 1 ? unit.name : ActiveSupport::Inflector.pluralize(unit.name)
    "#{value_string} #{unit_string}"
  end
end

#inspectObject



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

def inspect
  @inspect ||= "#<#{self.class}: #{value_string} #{unit.inspect}>"
end

#to_sObject



41
42
43
# File 'lib/measured/measurable.rb', line 41

def to_s
  @to_s ||= "#{value_string} #{unit.name}"
end