module Unitwise::Compatible

Compatible is used to establish compatibility between units, terms, or measurements. This is done by determining the objects atomic composition represented as a Signed Multiset.

Public Class Methods

included(base) click to toggle source

@api private

# File lib/unitwise/compatible.rb, line 7
def self.included(base)
  base.send :include, Comparable
  base.send :include, Memoizable unless base < Memoizable
  base.send :memoize, :composition, :composition_string
end
new(*args) click to toggle source
Calls superclass method
# File lib/unitwise/compatible.rb, line 13
def initialize(*args)
  super(*args)
  freeze
end

Public Instance Methods

<=>(other) click to toggle source

Compare whether the instance is greater, less than or equal to other. @return [-1 0 1] @api public

# File lib/unitwise/compatible.rb, line 54
def <=>(other)
  if other.respond_to?(:composition) && compatible_with?(other)
    scalar <=> other.scalar
  end
end
compatible_with?(other) click to toggle source

Determine if this instance is similar to or compatible with other @return [true false] @api public

# File lib/unitwise/compatible.rb, line 47
def compatible_with?(other)
  composition == other.composition
end
composition() click to toggle source

A representation of a unit based on the atoms it's derived from. @return [SignedMultiset] @api public

# File lib/unitwise/compatible.rb, line 21
def composition
  root_terms.reduce(SignedMultiset.new) do |s, t|
    s.increment(t.atom.dim, t.exponent) if t.atom
    s
  end
end
composition_string() click to toggle source

A string representation of a unit based on the atoms it's derived from @return [String] @api public

# File lib/unitwise/compatible.rb, line 38
def composition_string
  composition.sort.map do |k, v|
    v == 1 ? k.to_s : "#{k}#{v}"
  end.join('.')
end
dim() click to toggle source

Define a default dim for included classes. @return [String] @api public

# File lib/unitwise/compatible.rb, line 31
def dim
  composition_string
end