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, #nonzero?, #scale, #to_i

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
30
31
32
33
34
35
36
37
# File 'lib/measured/measurable.rb', line 9

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)
  when String
    /\d+\/\d+/.match?(value) ? Rational(value) : BigDecimal(value)
  else
    BigDecimal(value)
  end

  @value_string = begin
    str = case value
    when Rational
      value.denominator == 1 ? value.numerator.to_s : value.to_f.to_s
    when BigDecimal
      value.to_s("F")
    else
      value.to_f.to_s
    end
    /\.0*\Z/.match?(str) ? str.gsub(/\.0*\Z/, "") : str
  end.freeze
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



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

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.nameObject



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

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

.parse(string) ⇒ Object



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

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

.unit_systemObject



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

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



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

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

#convert_to(new_unit) ⇒ Object



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

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, with_conversion_string: true) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/measured/measurable.rb', line 48

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

#humanizeObject



60
61
62
63
# File 'lib/measured/measurable.rb', line 60

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

#inspectObject



65
66
67
# File 'lib/measured/measurable.rb', line 65

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

#to_sObject



56
57
58
# File 'lib/measured/measurable.rb', line 56

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