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.
- #magnitude(scalar = scalar()) ⇒ 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(magnitude = 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, #initialize
Methods inherited from Base
all, find, #names=, #slugs, #to_s
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.
12 13 14 |
# File 'lib/unitwise/atom.rb', line 12 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
18 19 20 |
# File 'lib/unitwise/atom.rb', line 18 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.
62 63 64 |
# File 'lib/unitwise/atom.rb', line 62 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.
27 28 29 |
# File 'lib/unitwise/atom.rb', line 27 def base? !!(@dim && !scale) end |
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
70 71 72 |
# File 'lib/unitwise/atom.rb', line 70 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.
35 36 37 |
# File 'lib/unitwise/atom.rb', line 35 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 |
#magnitude(scalar = scalar()) ⇒ Object
109 110 111 |
# File 'lib/unitwise/atom.rb', line 109 def magnitude(scalar = scalar()) special? ? scale.magnitude(scalar) : BigDecimal('1') end |
#metric ⇒ true, false Also known as: metric?
Determine if an atom is metric. Metric atoms can be combined with metric prefixes.
43 44 45 |
# File 'lib/unitwise/atom.rb', line 43 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.
116 117 118 |
# File 'lib/unitwise/atom.rb', line 116 def root_terms base? ? [Term.new(:atom_code => primary_code)] : scale.root_terms end |
#scalar(magnitude = 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(magnitude = 1) base? ? BigDecimal(magnitude.to_s) : scale.scalar(magnitude) 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.
52 53 54 |
# File 'lib/unitwise/atom.rb', line 52 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 |