Class: Renee::Core::Matcher
- Inherits:
-
Object
- Object
- Renee::Core::Matcher
- Defined in:
- lib/renee_core/matcher.rb
Overview
Class used for variable matching.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](val) ⇒ Object
Matcher for string.
-
#initialize(matcher) ⇒ Matcher
constructor
A new instance of Matcher.
-
#on_error { ... } ⇒ Object
Used to specific the error handler if the matcher doesn't match anything.
-
#on_transform { ... } ⇒ Object
Used to transform the value matched.
-
#raise_on_error!(error_code = :bad_request) ⇒ Object
Convienence method to creating halting error handler.
Constructor Details
#initialize(matcher) ⇒ Matcher
Returns a new instance of Matcher.
8 9 10 |
# File 'lib/renee_core/matcher.rb', line 8 def initialize(matcher) @matcher = matcher end |
Instance Attribute Details
#name ⇒ Object
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
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.
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.
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.
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 |