Class: Rulebook::Rule
- Inherits:
-
Object
- Object
- Rulebook::Rule
- Defined in:
- lib/rulebook.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
Instance Method Summary collapse
- #[](query) ⇒ Object (also: #match)
-
#initialize(regexp, &block) ⇒ Rule
constructor
A new instance of Rule.
- #matches?(query) ⇒ Boolean
Constructor Details
#initialize(regexp, &block) ⇒ Rule
Returns a new instance of Rule.
10 11 12 13 14 15 |
# File 'lib/rulebook.rb', line 10 def initialize(regexp, &block) # TODO: Match more than Regexp. Strings and Symbols pls. raise(TypeError, 'regexp must be of type Regexp') unless regexp.is_a?(Regexp) raise(ArgumentError, 'a block is needed') unless block_given? @regexp, @block = regexp, block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
8 9 10 |
# File 'lib/rulebook.rb', line 8 def block @block end |
Instance Method Details
#[](query) ⇒ Object Also known as: match
17 18 19 |
# File 'lib/rulebook.rb', line 17 def [](query) query.to_s.downcase.match(@regexp) end |
#matches?(query) ⇒ Boolean
22 23 24 |
# File 'lib/rulebook.rb', line 22 def matches?(query) !self[query].nil? end |