Class: Unitwise::Atom

Inherits:
Base
  • Object
show all
Includes:
Compatible
Defined in:
lib/unitwise/atom.rb

Overview

Atoms are the most basic elements in Unitwise. They are named coded and scaled units without prefixes, multipliers, exponents, etc. Examples are 'meter', 'hour', 'pound force'.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, included

Methods inherited from Base

all, find, #initialize, #names=, #slugs, #to_s

Constructor Details

This class inherits a constructor from Unitwise::Base

Class Method Details

.data ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Array of hashes representing atom properties.

API:

  • private



13
14
15
# File 'lib/unitwise/atom.rb', line 13

def data
  @data ||= data_files.reduce([]){|m,f| m += YAML::load File.open(f)}
end

.data_files ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Data files containing atom data

API:

  • private



19
20
21
# File 'lib/unitwise/atom.rb', line 19

def data_files
  %w(base_unit derived_unit).map{|type| Unitwise.data_file type}
end

Instance Method Details

#arbitrary ⇒ true, false Also known as: arbitrary?

Determine if a unit is arbitrary. Arbitrary atoms are not of any specific dimension and have no general meaning, therefore cannot be compared with any other unit.

Returns:

API:

  • public



63
64
65
# File 'lib/unitwise/atom.rb', line 63

def arbitrary
  !!@arbitrary
end

#base? ⇒ true, false

Determine if an atom is base level. All atoms that are not base are defined directly or indirectly in reference to a base atom.

Returns:

API:

  • public



28
29
30
# File 'lib/unitwise/atom.rb', line 28

def base?
  scale.nil? && !@dim.nil?
end

#depth ⇒ Integer

Determine how far away a unit is from a base unit.

Returns:

API:

  • public



71
72
73
# File 'lib/unitwise/atom.rb', line 71

def depth
  base? ? 0 : scale.depth + 1
end

#derived? ⇒ true, false

Determine if an atom is derived. Derived atoms are defined with respect to other atoms.

Returns:

API:

  • public



36
37
38
# File 'lib/unitwise/atom.rb', line 36

def derived?
  !base?
end

#dim ⇒ String

A representation of an atoms composition. Used to determine if two different atoms are compatible.

Returns:

API:

  • public



86
87
88
# File 'lib/unitwise/atom.rb', line 86

def dim
  terminal? ? @dim || property : composition_string
end

#functional(x = scalar, forward = true) ⇒ Numeric

Get a functional value that can be used with other atoms to compare with or operate on.

Parameters:

  • (defaults to: scalar)

    The number to convert to or convert from

  • (defaults to: true)

    Convert to or convert from

Returns:

  • The converted value



114
115
116
# File 'lib/unitwise/atom.rb', line 114

def functional(x=scalar, forward=true)
  scale.functional(x, forward)
end

#metric ⇒ true, false Also known as: metric?

Determine if an atom is metric. Metric atoms can be combined with metric prefixes.

Returns:

API:

  • public



44
45
46
# File 'lib/unitwise/atom.rb', line 44

def metric
  base? ? true : !!@metric
end

#root_terms ⇒ Array

An atom may have a complex scale with several base atoms at various depths. This method returns all of this atoms base level terms.

Returns:

  • An array containing base Unitwise::Term



121
122
123
# File 'lib/unitwise/atom.rb', line 121

def root_terms
  base? ? [Term.new(atom_code: primary_code)] : scale.root_terms
end

#scalar ⇒ Numeric

Get a numeric value that can be used to with other atoms to compare with or operate on. Base units have a scalar of 1.

Returns:

API:

  • public



105
106
107
# File 'lib/unitwise/atom.rb', line 105

def scalar
  base? ? 1 : scale.scalar
end

#scale=(attrs) ⇒ Unitwise::Functional, Unitwise::Scale

Set the atom's scale. It can be set as a Scale or a Functional

Returns:

API:

  • public



93
94
95
96
97
98
99
# File 'lib/unitwise/atom.rb', line 93

def scale=(attrs)
  @scale = if attrs[:function_code]
    Functional.new(attrs[:value], attrs[:unit_code], attrs[:function_code])
  else
    Scale.new(attrs[:value], attrs[:unit_code])
  end
end

#special ⇒ true, false Also known as: special?

Determine if a unit is special. Special atoms are not defined on a traditional ratio scale.

Returns:

API:

  • public



53
54
55
# File 'lib/unitwise/atom.rb', line 53

def special
  !!@special
end

#terminal? ⇒ true, false

Determine if this is the last atom in the scale chain

Returns:

API:

  • public



78
79
80
# File 'lib/unitwise/atom.rb', line 78

def terminal?
  depth <= 3
end