Sha256: e1b14e927ad830524f7a7af63c551c50f20400b7342e2af8288607a8da6693a3

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

__DIR__ = File.dirname(__FILE__)
$:.unshift(__DIR__) unless $:.include?(__DIR__)

class Rulebook
  VERSION = "0.5.0"
  
  class Rule
    attr :block
    
    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
    
    def [](query)
      query.to_s.downcase.match(@regexp)
    end
    alias_method :match, :[]
    
    def matches?(query)
      !self[query].nil?
    end
  end
  
  attr_accessor :rules
  
  def initialize
    @rules = []
  end
  
  def add(regexp, &block)
    @rules << Rule.new(regexp, &block)
  end
  # alias_method :<<. :add
  
  def [](query)
    @rules.find_all { |rule| rule.matches?(query) }
  end
  alias_method :rules_that_match_against, :[]
  alias_method :match, :[]
end

require 'meta_tools'
require 'core_ext'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rulebook-0.5.0 lib/rulebook.rb