Class: Charisma::Measurement Abstract
- Inherits:
-
Object
- Object
- Charisma::Measurement
- Defined in:
- lib/charisma/measurement.rb,
lib/charisma/measurement/mass.rb,
lib/charisma/measurement/time.rb,
lib/charisma/measurement/speed.rb,
lib/charisma/measurement/length.rb
Overview
An actual measurement class should inherit from this and use #units
to define units.
An abstract class that implements an API for Charisma to deal with measured characteristics.
Defined Under Namespace
Classes: Length, Mass, Speed, Time
Constant Summary collapse
- Velocity =
Velocity is an SI-accepted alias for speed
Speed
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
The quantity of the measured value.
Class Method Summary collapse
-
.unit ⇒ Symbol
Retrive the units used by the measurement.
-
.unit_abbreviation ⇒ String
Retrieve the abbreviation of the units used by the measurement.
-
.units(units) ⇒ Object
Define the units used with this measurement.
Instance Method Summary collapse
-
#as_json ⇒ Object
Provide a hash for later conversion to JSON.
-
#initialize(value) ⇒ Measurement
constructor
Create a new instance of this measurement.
-
#method_missing(*args) ⇒ Object
Handle conversion methods.
-
#to_f ⇒ Fixnum
Return just the quantity of the measurement.
-
#to_hash ⇒ Object
Provide a hash form.
-
#to_s ⇒ String
Show the measured value, along with units.
-
#u ⇒ String
The standard abbreviation for the measurement’s units.
-
#units ⇒ Symbol
The measurement’s units.
Constructor Details
#initialize(value) ⇒ Measurement
Create a new instance of this measurement.
Typically this will be done automatically by Charisma::Curator::Curation
.
19 20 21 |
# File 'lib/charisma/measurement.rb', line 19 def initialize(value) @value = value end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
Handle conversion methods
48 49 50 51 52 53 54 |
# File 'lib/charisma/measurement.rb', line 48 def method_missing(*args) if Conversions.conversions[units.to_sym][args.first] to_f.send(units.to_sym).to(args.first) else super end end |
Instance Attribute Details
#value ⇒ Object (readonly)
The quantity of the measured value
12 13 14 |
# File 'lib/charisma/measurement.rb', line 12 def value @value end |
Class Method Details
.unit ⇒ Symbol
Retrive the units used by the measurement
77 78 79 |
# File 'lib/charisma/measurement.rb', line 77 def unit @units end |
.unit_abbreviation ⇒ String
Retrieve the abbreviation of the units used by the measurement
83 84 85 |
# File 'lib/charisma/measurement.rb', line 83 def unit_abbreviation @units_abbreviation end |
.units(units) ⇒ Object
Define the units used with this measurement.
Used by conforming subclasses.
71 72 73 |
# File 'lib/charisma/measurement.rb', line 71 def units(units) @units, @units_abbreviation = units.to_a.flatten end |
Instance Method Details
#as_json ⇒ Object
Provide a hash for later conversion to JSON
62 63 64 |
# File 'lib/charisma/measurement.rb', line 62 def as_json to_hash end |
#to_f ⇒ Fixnum
Return just the quantity of the measurement
31 32 33 |
# File 'lib/charisma/measurement.rb', line 31 def to_f value.to_f end |
#to_hash ⇒ Object
Provide a hash form
57 58 59 |
# File 'lib/charisma/measurement.rb', line 57 def to_hash { :value => value, :units => units.to_s } end |
#to_s ⇒ String
Show the measured value, along with units
25 26 27 |
# File 'lib/charisma/measurement.rb', line 25 def to_s "#{NumberHelper.delimit value} #{u}" end |
#u ⇒ String
The standard abbreviation for the measurement’s units
43 44 45 |
# File 'lib/charisma/measurement.rb', line 43 def u self.class.unit_abbreviation end |
#units ⇒ Symbol
The measurement’s units
37 38 39 |
# File 'lib/charisma/measurement.rb', line 37 def units self.class.unit end |