Module: Unitwise::Search

Defined in:
lib/unitwise/search.rb

Overview

The search module provides a simple search mechanism around known basic units. The full list of avaliable units infinite, so this search creates a small subset of atoms and prefixes to help users find what they are looking for. Thus, there is a multitude of valid units that may be constructed that this module will not be aware of.

Class Method Summary collapse

Class Method Details

.all ⇒ Array

An abbreviated list of possible units. These are known combinations of atoms and prefixes.

Returns:

  • (Array) —

    A list of known units



13
14
15
16
17
18
19
20
21
22
# File 'lib/unitwise/search.rb', line 13

def all
  @all ||= begin
    units = []
    Atom.all.each do |a|
      units << build(a)
      Unitwise::Prefix.all.each { |p| units << build(a, p) } if a.metric?
    end
    units
  end
end

.search(term) ⇒ Array

Search the list of known units for a match.

Parameters:

  • term (String, Regexp) —

    The term to search for

Returns:

  • (Array) —

    A list of matching units.



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

def search(term)
  all.select do |unit|
    unit.aliases.any? { |str| Regexp.new(term).match(str) }
  end
end