Sha256: 5ab2bf06505bc8d3e1096bd47a784237ab2494f7b5f97d1d5e4860122f21f1c9

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Spidr
  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.each do |rule|
          return true if test_data(data,rule)
        end

        return false
      else
        @reject.each do |rule|
          return false if test_data(data,rule)
        end

        return true
      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)
        return (rule.call(data) == true)
      elsif rule.kind_of?(Regexp)
        return !((data.to_s =~ rule).nil?)
      else
        return data == rule
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spidr-0.2.2 lib/spidr/rules.rb
spidr-0.2.1 lib/spidr/rules.rb
spidr-0.2.0 lib/spidr/rules.rb