Class: Remi::Lookup::RegexSieve

Inherits:
Object
  • Object
show all
Defined in:
lib/remi/lookup/regex_sieve.rb

Overview

Public: RegexSieve class. The RegexSieve functions in a manner similar a hash. The regex sieve is initialized with a hash where the keys are regular expressions and the values can be any valid Ruby object. The order of the keys matters. When the regex sieve is accessed using the array accessor [], it returns the first matching record. By default, only the values are returned, but the key and all matching capture groups can optionally be returned.

Examples:

r = RegexSieve.new({
  /something/ => 'Something',
  /something else/ => 'This will never get matched because the one above will match first',
  /cool$/ => 'Cool',
  /cool beans/ => 'Really Cool'
})

r['something else'] # => 'Something'
r['cool beans'] # => 'Really Cool'

Instance Method Summary collapse

Constructor Details

#initialize(sieve) ⇒ RegexSieve

Returns a new instance of RegexSieve.



24
25
26
# File 'lib/remi/lookup/regex_sieve.rb', line 24

def initialize(sieve)
  @sieve = sieve
end

Instance Method Details

#[](key, *opt) ⇒ Object

Public: Array accessor for Regex Sieve.

key - The string that will be matched to the keys in the sieve. opt - By default, only the values in the hash used to initialize the sieve

will be returned.  However, if you want to return the keys or the
capture groups then use :regex, :match, or both, respectively.

Example:

r['something'] # => 'Something
r['something', :regex] # => { value: 'Something', regex: /something/ }
r['sometinng', :match, :regex] # => { value: 'Something', regex: /something/, match: #<MatchData "something"> }


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/remi/lookup/regex_sieve.rb', line 39

def [](key, *opt)
  opt = opt | [:value]

  regex_match = nil
  found = @sieve.find do |regex, v|
    regex_match = regex.match(key)
  end

  return nil if found.nil?
  full_result = { value: found[1], regex: found[0], match: regex_match }

  full_result.select! { |k, v| opt.include?(k) }
  full_result.size > 1 ? full_result : full_result.values.first
end