Class: Rulebook::Rule

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(what_to_capture, &block) ⇒ Rule

Returns a new instance of Rule.

Raises:

  • (TypeError)


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

#blockObject (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

Returns:

  • (Boolean)


16
# File 'lib/rulebook/rule.rb', line 16

def matches_against?(query); !self[query].nil?; end