Class: Unitwise::Compound

Inherits:
Object
  • Object
show all
Defined in:
lib/unitwise/compound.rb

Overview

A compound is a combination of an atom and a prefix. This class is isolated from the rest of the code base and primarily exists for the convenience of listing and searching possible combinations of atoms and prefixes.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allArray

List all possible compounds

Returns:

  • (Array)


10
11
12
# File 'lib/unitwise/compound.rb', line 10

def self.all
  @all || build
end

.buildArray

Builds up the list of possible compounds. Only required if you are defining your own atoms/prefixes.

Returns:

  • (Array)


18
19
20
21
22
23
24
25
26
# File 'lib/unitwise/compound.rb', line 18

def self.build
  compounds = Atom.all.map { |a| new(a) }
  Atom.all.select(&:metric).each do |a|
    Prefix.all.each do |p|
      compounds << new(a, p)
    end
  end
  @all = compounds
end

.search(term) ⇒ Array

Search for compounds with a search term

Parameters:

  • What (String, Regexp)

    to search for

Returns:

  • (Array)


32
33
34
35
36
# File 'lib/unitwise/compound.rb', line 32

def self.search(term)
  all.select do |compound|
    compound.search_strings.any? { |str| Regexp.new(term).match(str) }
  end
end

Instance Method Details

#atom=(value) ⇒ Object



58
59
60
# File 'lib/unitwise/compound.rb', line 58

def atom=(value)
  value.is_a?(Atom) ? super(value) : super(Atom.find value)
end

#attribute_stringObject



71
72
73
74
75
76
# File 'lib/unitwise/compound.rb', line 71

def attribute_string
  [:atom, :prefix, :primary_code, :secondary_code,
   :symbol, :names, :slugs].map do |attr|
    "#{attr}='#{send attr}'"
  end.join(', ')
end

#prefix=(value) ⇒ Object



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

def prefix=(value)
  value.is_a?(Prefix) ? super(value) : super(Prefix.find value)
end

#search_stringsObject



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

def search_strings
  @search_strings ||= [primary_code, secondary_code, symbol,
                       names, slugs].flatten.uniq
end