Sha256: 1a7571f056ffe185240718fdeaf15d9520d7e613da41633ad40bee75fd58610e

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

module Spidr
  #
  # The {Rules} class represents collections of acceptance and rejection
  # rules, which are used to filter data.
  #
  class Rules

    # Accept rules
    attr_reader :accept

    # Reject rules
    attr_reader :reject

    #
    # Creates a new Rules object.
    #
    # @param [Hash] options
    #   Additional options.
    #
    # @option options [Array<String, Regexp, Proc>] :accept
    #   The patterns to accept data with.
    #
    # @option options [Array<String, Regexp, Proc>] :reject
    #   The patterns to reject data with.
    #
    def initialize(options={})
      @accept = []
      @reject = []

      @accept += options[:accept] if options[:accept]
      @reject += options[:reject] if options[:reject]
    end

    #
    # Determines whether the data should be accepted or rejected.
    #
    # @return [Boolean]
    #   Specifies whether the given data was accepted, using the rules
    #   acceptance patterns.
    #
    def accept?(data)
      unless @accept.empty?
        @accept.any? { |rule| test_data(data,rule) }
      else
        !@reject.any? { |rule| test_data(data,rule) }
      end
    end

    #
    # Determines whether the data should be rejected or accepted.
    #
    # @return [Boolean]
    #   Specifies whether the given data was rejected, using the rules
    #   rejection patterns.
    #
    def reject?(data)
      !accept?(data)
    end

    protected

    #
    # Tests the given data against a given pattern.
    #
    # @return [Boolean]
    #   Specifies whether the given data matched the pattern.
    #
    def test_data(data,rule)
      if rule.kind_of?(Proc)
        rule.call(data) == true
      elsif rule.kind_of?(Regexp)
        !((data.to_s =~ rule).nil?)
      else
        data == rule
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
spidr-0.6.1 lib/spidr/rules.rb
spidr-0.6.0 lib/spidr/rules.rb
spidr-0.5.0 lib/spidr/rules.rb
spidr_epg_gem-0.0.1 lib/spidr_epg/rules.rb
spidr_epg_gem-0.0.0 lib/spidr_epg/rules.rb
spidr_epg-1.0.0 lib/spidr/rules.rb
spidr-0.4.1 lib/spidr/rules.rb
spidr-0.4.0 lib/spidr/rules.rb
spidr-0.3.2 lib/spidr/rules.rb