Class: Renee::Core::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/renee_core/matcher.rb

Overview

Class used for variable matching.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ Matcher

Returns a new instance of Matcher.

Parameters:

  • matcher (Regexp)

    The regexp matcher to determine what is part of the variable.



8
9
10
# File 'lib/renee_core/matcher.rb', line 8

def initialize(matcher)
  @matcher = matcher
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/renee_core/matcher.rb', line 5

def name
  @name
end

Instance Method Details

#[](val) ⇒ Object

Matcher for string

Parameters:

  • val (String)

    The value to attempt to match on.

Raises:

  • (ClientError)

    If the match fails to match and there is an error handler defined.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/renee_core/matcher.rb', line 37

def [](val)
  match = nil
  case @matcher
  when Array
    match = nil
    @matcher.find { |m| match = m[val] }
  else
    if match = /^#{@matcher.to_s}/.match(val)
      match = [match[0]]
      match << @transform_handler.call(match.first) if @transform_handler
      match
    end
  end
  if match
    match
  elsif @error_handler
    raise ClientError.new("There was an error interpreting the value #{val.inspect} for #{name.inspect}", &@error_handler)
  end
end

#on_error { ... } ⇒ Object

Used to specific the error handler if the matcher doesn't match anything. By default, there is no error handler.

Yields:

  • The block to be executed it fails to match.



14
15
16
17
# File 'lib/renee_core/matcher.rb', line 14

def on_error(&blk)
  @error_handler = blk
  self
end

#on_transform { ... } ⇒ Object

Used to transform the value matched.

Yields:

  • TODO



21
22
23
24
# File 'lib/renee_core/matcher.rb', line 21

def on_transform(&blk)
  @transform_handler = blk
  self
end

#raise_on_error!(error_code = :bad_request) ⇒ Object

Convienence method to creating halting error handler.

Parameters:

  • error_code (Symbol, Integer) (defaults to: :bad_request)

    The HTTP code to halt with.

See Also:



29
30
31
32
# File 'lib/renee_core/matcher.rb', line 29

def raise_on_error!(error_code = :bad_request)
  on_error { halt error_code }
  self
end