Class: Measured::Measurable
- Inherits:
-
Numeric
- Object
- Numeric
- Measured::Measurable
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
|
# 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)
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
str.gsub(/\.0*\Z/, "")
end.freeze
end
|
Instance Attribute Details
#unit ⇒ Object
Returns the value of attribute unit.
7
8
9
|
# File 'lib/measured/measurable.rb', line 7
def unit
@unit
end
|
#value ⇒ Object
Returns the value of attribute value.
7
8
9
|
# File 'lib/measured/measurable.rb', line 7
def value
@value
end
|
Class Method Details
.name ⇒ Object
86
87
88
|
# File 'lib/measured/measurable.rb', line 86
def name
to_s.split("::").last.underscore.humanize.downcase
end
|
.unit_system ⇒ Object
78
79
80
|
# File 'lib/measured/measurable.rb', line 78
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
67
68
69
70
71
72
73
|
# File 'lib/measured/measurable.rb', line 67
def <=>(other)
if other.is_a?(self.class)
value <=> other.convert_to(unit).value
else
nil
end
end
|
#convert_to(new_unit) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/measured/measurable.rb', line 37
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
|
46
47
48
49
50
51
52
|
# File 'lib/measured/measurable.rb', line 46
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
|
#humanize ⇒ Object
58
59
60
61
|
# File 'lib/measured/measurable.rb', line 58
def humanize
unit_string = value == 1 ? unit.name : ActiveSupport::Inflector.pluralize(unit.name)
"#{@value_string} #{unit_string}"
end
|
#inspect ⇒ Object
63
64
65
|
# File 'lib/measured/measurable.rb', line 63
def inspect
"#<#{self.class}: #{@value_string} #{unit.inspect}>"
end
|
#to_s ⇒ Object
54
55
56
|
# File 'lib/measured/measurable.rb', line 54
def to_s
"#{@value_string} #{unit.name}"
end
|