Sha256: 34864226b657ddc6796b7327e99963e5df07ed6782846f54b7065632e7da82a7

Contents?: true

Size: 706 Bytes

Versions: 4

Compression:

Stored size: 706 Bytes

Contents

class Rulebook
  # This class creates an instance of a Rule, which holds the
  # Regexp to match against and the block to run when matched
  class Rule
    attr :block
    
    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
    
    def [](query); query.to_s.downcase.match(@what_to_capture); end
    alias_method :match_against, :[]
    def matches_against?(query); !self[query].nil?; end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rulebook-0.4.3 lib/rulebook/rule.rb
rulebook-0.4.2 lib/rulebook/rule.rb
rulebook-0.4.1 lib/rulebook/rule.rb
rulebook-0.4.0 lib/rulebook/rule.rb