Class: Rulebook::Rule
- Inherits:
-
Object
- Object
- Rulebook::Rule
- Defined in:
- lib/rulebook/rule.rb
Overview
This class creates an instance of a Rule, which holds the Regexp to match against and the block to run when matched
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
Instance Method Summary collapse
- #[](query) ⇒ Object (also: #match_against)
-
#initialize(what_to_capture, &block) ⇒ Rule
constructor
A new instance of Rule.
- #matches_against?(query) ⇒ Boolean
Constructor Details
#initialize(what_to_capture, &block) ⇒ Rule
Returns a new instance of Rule.
7 8 9 10 11 12 |
# File 'lib/rulebook/rule.rb', line 7 def initialize(what_to_capture, &block) # TODO: Match more than Regexp. Strings and Symbols pls. raise(TypeError, 'what_to_capture must be of type Regexp') unless what_to_capture.is_a?(Regexp) raise(ArgumentError, 'a block is needed') unless block_given? @what_to_capture, @block = what_to_capture, block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
5 6 7 |
# File 'lib/rulebook/rule.rb', line 5 def block @block end |
Instance Method Details
#[](query) ⇒ Object Also known as: match_against
14 |
# File 'lib/rulebook/rule.rb', line 14 def [](query); query.to_s.downcase.match(@what_to_capture); end |
#matches_against?(query) ⇒ Boolean
16 |
# File 'lib/rulebook/rule.rb', line 16 def matches_against?(query); !self[query].nil?; end |