Class: Unitwise::Atom
- 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
-
.data ⇒ Object
private
Array of hashes representing atom properties.
-
.data_files ⇒ Object
private
Data files containing atom data.
Instance Method Summary collapse
-
#arbitrary ⇒ true, false
(also: #arbitrary?)
Determine if a unit is arbitrary.
-
#base? ⇒ true, false
Determine if an atom is base level.
-
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
-
#derived? ⇒ true, false
Determine if an atom is derived.
-
#dim ⇒ String
A representation of an atoms composition.
- #inverse_scalar(x = 1) ⇒ Object
-
#metric ⇒ true, false
(also: #metric?)
Determine if an atom is metric.
-
#root_terms ⇒ Array
An atom may have a complex scale with several base atoms at various depths.
-
#scalar(x = 1) ⇒ Numeric
Get a numeric value that can be used to with other atoms to compare with or operate on.
-
#scale=(attrs) ⇒ Unitwise::Functional, Unitwise::Scale
Set the atom's scale.
-
#special ⇒ true, false
(also: #special?)
Determine if a unit is special.
-
#terminal? ⇒ true, false
Determine if this is the last atom in the scale chain.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
78 79 80 |
# File 'lib/unitwise/atom.rb', line 78 def terminal? depth <= 3 end |