Class: Unitwise::Compound
- Inherits:
-
Object
- Object
- Unitwise::Compound
- 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
-
.all ⇒ Array
List all possible compounds.
-
.build ⇒ Array
Builds up the list of possible compounds.
-
.search(term) ⇒ Array
Search for compounds with a search term.
Instance Method Summary collapse
Class Method Details
.all ⇒ Array
List all possible compounds
10 11 12 |
# File 'lib/unitwise/compound.rb', line 10 def self.all @all || build end |
.build ⇒ Array
Builds up the list of possible compounds. Only required if you are defining your own atoms/prefixes.
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
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_string ⇒ Object
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_strings ⇒ Object
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 |