Class: Rulebook::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, &block) ⇒ Rule

Returns a new instance of Rule.

Raises:

  • (TypeError)


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

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/rulebook.rb', line 22

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