Module: Unitwise::Composable

Included in:
Atom, Scale, Term, Unit
Defined in:
lib/unitwise/composable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/unitwise/composable.rb', line 4

def self.included(base)
  base.send :include, Comparable
end

Instance Method Details

#<=>(other) ⇒ Object



28
29
30
31
32
# File 'lib/unitwise/composable.rb', line 28

def <=>(other)
  if other.respond_to?(:composition) && similar_to?(other)
    scalar <=> other.scalar
  end
end

#compositionObject



8
9
10
11
12
# File 'lib/unitwise/composable.rb', line 8

def composition
  root_terms.reduce(SignedMultiset.new) do |s, t|
    s.increment(t.atom.dim, t.exponent) if t.atom; s
  end
end

#composition_stringObject



18
19
20
21
22
# File 'lib/unitwise/composable.rb', line 18

def composition_string
  composition.sort.map do |k,v|
    v == 1 ? k.to_s : "#{k}#{v}"
  end.join('.')
end

#dimObject



14
15
16
# File 'lib/unitwise/composable.rb', line 14

def dim
  composition_string
end

#similar_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/unitwise/composable.rb', line 24

def similar_to?(other)
  self.composition == other.composition
end