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.



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

def data
  @data ||= data_files.map { |file| YAML.load(File.open file) }.flatten
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



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:

  • (true, false)


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:

  • (true, false)


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

def base?
  !!(@dim && !scale)
end

#depth ⇒ Integer

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

Returns:

  • (Integer)


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:

  • (true, false)


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:

  • (String)


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

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

#inverse_scalar(x = 1) ⇒ Object



109
110
111
# File 'lib/unitwise/atom.rb', line 109

def inverse_scalar(x = 1)
  base? ? 1 : scale.inverse_scalar(x)
end

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

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

Returns:

  • (true, false)


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:

  • (Array) —

    An array containing base Unitwise::Term



123
124
125
# File 'lib/unitwise/atom.rb', line 123

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

#scalar(x = 1) ⇒ 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:



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

def scalar(x = 1)
  base? ? 1 : scale.scalar(x)
end

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

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



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:

  • (true, false)


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:

  • (true, false)


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

def terminal?
  depth <= 3
end