Class: Charisma::Measurement Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/charisma/measurement.rb,
lib/charisma/measurement/time.rb,
lib/charisma/measurement/mass.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

Velocity =

Velocity is an SI-accepted alias for speed

Speed

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Measurement) initialize(value)

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:



15
16
17
# File 'lib/charisma/measurement.rb', line 15

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(*args)

Handle conversion methods



44
45
46
47
48
49
50
# File 'lib/charisma/measurement.rb', line 44

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

- (Object) value (readonly)

The quantity of the measured value



8
9
10
# File 'lib/charisma/measurement.rb', line 8

def value
  @value
end

Class Method Details

+ (Symbol) unit

Retrive the units used by the measurement

Returns:

  • (Symbol)


73
74
75
# File 'lib/charisma/measurement.rb', line 73

def unit
  @units
end

+ (String) unit_abbreviation

Retrieve the abbreviation of the units used by the measurement

Returns:

  • (String)


79
80
81
# File 'lib/charisma/measurement.rb', line 79

def unit_abbreviation
  @units_abbreviation
end

+ (Object) units(units)

Define the units used with this measurement.

Used by conforming subclasses.

Parameters:

  • units (Hash)

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



67
68
69
# File 'lib/charisma/measurement.rb', line 67

def units(units)
  @units, @units_abbreviation = units.to_a.flatten
end

Instance Method Details

- (Object) as_json

Provide a hash for later conversion to JSON



58
59
60
# File 'lib/charisma/measurement.rb', line 58

def as_json
  to_hash
end

- (Fixnum) to_f

Return just the quantity of the measurement

Returns:

  • (Fixnum)


27
28
29
# File 'lib/charisma/measurement.rb', line 27

def to_f
  value.to_f
end

- (Object) to_hash

Provide a hash form



53
54
55
# File 'lib/charisma/measurement.rb', line 53

def to_hash
  { :value => value, :units => units.to_s }
end

- (String) to_s

Show the measured value, along with units

Returns:

  • (String)


21
22
23
# File 'lib/charisma/measurement.rb', line 21

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

- (String) u

The standard abbreviation for the measurement's units

Returns:

  • (String)


39
40
41
# File 'lib/charisma/measurement.rb', line 39

def u
  self.class.unit_abbreviation
end

- (Symbol) units

The measurement's units

Returns:

  • (Symbol)

    '



33
34
35
# File 'lib/charisma/measurement.rb', line 33

def units
  self.class.unit
end