Class: Charisma::Measurement Abstract

Inherits:
Object
  • Object
show all
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

This class is abstract.

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.

Direct Known Subclasses

Length, Mass, Speed, Time

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Measurement

Create a new instance of this measurement.

Typically this will be done automatically by Charisma::Curator::Curation.

Parameters:

  • value (Fixnum)

    The quantity of the measured value

See Also:



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

#valueObject (readonly)

The quantity of the measured value



12
13
14
# File 'lib/charisma/measurement.rb', line 12

def value
  @value
end

Class Method Details

.unitSymbol

Retrive the units used by the measurement

Returns:

  • (Symbol)


77
78
79
# File 'lib/charisma/measurement.rb', line 77

def unit
  @units
end

.unit_abbreviationString

Retrieve the abbreviation of the units used by the measurement

Returns:

  • (String)


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.

Parameters:

  • units (Hash)

    The units, given in the form :plural_unit_name => 'abbrev'



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_jsonObject

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_fFixnum

Return just the quantity of the measurement

Returns:

  • (Fixnum)


31
32
33
# File 'lib/charisma/measurement.rb', line 31

def to_f
  value.to_f
end

#to_hashObject

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_sString

Show the measured value, along with units

Returns:

  • (String)


25
26
27
# File 'lib/charisma/measurement.rb', line 25

def to_s
  "#{NumberHelper.delimit value} #{u}"
end

#uString

The standard abbreviation for the measurement’s units

Returns:

  • (String)


43
44
45
# File 'lib/charisma/measurement.rb', line 43

def u
  self.class.unit_abbreviation
end

#unitsSymbol

The measurement’s units

Returns:

  • (Symbol)



37
38
39
# File 'lib/charisma/measurement.rb', line 37

def units
  self.class.unit
end