Sha256: 1d6f52ed8b19fb8b1885f083994e059f2596b84a6d70d2fb7fe6b1c8f1f801a3

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

module Spidr
  class Rules

    # Accept rules
    attr_reader :accept

    # Reject rules
    attr_reader :reject

    def initialize(options={})
      @accept = (options[:accept] || [])
      @reject = (options[:reject] || [])
    end

    #
    # Returns +true+ if the _field_ is accepted by the rules,
    # returns +false+ otherwise.
    #
    def accept?(field)
      unless @accept.empty?
        @accept.each do |rule|
          return true if test_field(field,rule)
        end

        return false
      else
        @reject.each do |rule|
          return false if test_field(field,rule)
        end

        return true
      end
    end

    #
    # Returns +true+ if the _field_ is rejected by the rules,
    # returns +false+ otherwise.
    #
    def reject?(field)
      !(accept?(field))
    end

    protected

    #
    # Tests the specified _field_ against the specified _rule_. Returns
    # +true+ when the _rule_ matches the specified _field_, returns
    # +false+ otherwise.
    #
    def test_field(field,rule)
      if rule.kind_of?(Proc)
        return (rule.call(field) == true)
      elsif rule.kind_of?(Regexp)
        return !((field.to_s =~ rule).nil?)
      else
        return field == rule
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spidr-0.1.3 lib/spidr/rules.rb
spidr-0.1.6 lib/spidr/rules.rb
spidr-0.1.0 lib/spidr/rules.rb
spidr-0.1.4 lib/spidr/rules.rb
spidr-0.1.1 lib/spidr/rules.rb
spidr-0.1.5 lib/spidr/rules.rb
spidr-0.1.2 lib/spidr/rules.rb
spidr-0.1.9 lib/spidr/rules.rb
spidr-0.1.8 lib/spidr/rules.rb
spidr-0.1.7 lib/spidr/rules.rb