Class: Unitwise::Atom

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

Instance Attribute Summary collapse

Attributes inherited from Base

#names, #primary_code, #scale, #secondary_code, #symbol

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composable

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

Methods inherited from Base

all, find, find_by, #initialize, #slugs

Constructor Details

This class inherits a constructor from Unitwise::Base

Instance Attribute Details

#arbitraryObject

Returns the value of attribute arbitrary.



3
4
5
# File 'lib/unitwise/atom.rb', line 3

def arbitrary
  @arbitrary
end

#classificationObject

Returns the value of attribute classification.



3
4
5
# File 'lib/unitwise/atom.rb', line 3

def classification
  @classification
end

#dimObject



50
51
52
# File 'lib/unitwise/atom.rb', line 50

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

#metricObject

Returns the value of attribute metric.



3
4
5
# File 'lib/unitwise/atom.rb', line 3

def metric
  @metric
end

#propertyObject

Returns the value of attribute property.



3
4
5
# File 'lib/unitwise/atom.rb', line 3

def property
  @property
end

#specialObject

Returns the value of attribute special.



3
4
5
# File 'lib/unitwise/atom.rb', line 3

def special
  @special
end

Class Method Details

.dataObject



9
10
11
# File 'lib/unitwise/atom.rb', line 9

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

.data_filesObject



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

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

Instance Method Details

#arbitrary?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/unitwise/atom.rb', line 38

def arbitrary?
  !!arbitrary
end

#base?Boolean

Returns:

  • (Boolean)


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

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

#depthObject



42
43
44
# File 'lib/unitwise/atom.rb', line 42

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

#derived?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/unitwise/atom.rb', line 22

def derived?
  !base?
end

#functional(x = scalar, direction = 1) ⇒ Object



66
67
68
# File 'lib/unitwise/atom.rb', line 66

def functional(x=scalar, direction=1)
  scale.functional(x, direction)
end

#inspectObject



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

def inspect
  "<#{self.class} #{to_s}>"
end

#metric?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/unitwise/atom.rb', line 26

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

#nonmetric?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/unitwise/atom.rb', line 30

def nonmetric?
  !metric?
end

#root_termsObject



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

def root_terms
  base? ? [Term.new(atom: self)] : scale.root_terms
end

#scalarObject



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

def scalar
  base? ? 1 : scale.scalar
end

#scale=(attrs) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/unitwise/atom.rb', line 54

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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/unitwise/atom.rb', line 34

def special?
  !!special
end

#terminal?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/unitwise/atom.rb', line 46

def terminal?
  depth <= 3
end

#to_sObject



74
75
76
# File 'lib/unitwise/atom.rb', line 74

def to_s
  "#{primary_code} (#{names.join('|')})"
end