lib/spidr/rules.rb in spidr-0.1.9 vs lib/spidr/rules.rb in spidr-0.2.0

- old
+ new

@@ -5,56 +5,78 @@ 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 = (options[:accept] || []) - @reject = (options[:reject] || []) + @accept = [] + @reject = [] + + @accept += options[:accept] if options[:accept] + @reject += options[:reject] if options[:reject] end # - # Returns +true+ if the _field_ is accepted by the rules, - # returns +false+ otherwise. + # Determines whether the data should be accepted or rejected. # - def accept?(field) + # @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_field(field,rule) + return true if test_data(data,rule) end return false else @reject.each do |rule| - return false if test_field(field,rule) + return false if test_data(data,rule) end return true end end # - # Returns +true+ if the _field_ is rejected by the rules, - # returns +false+ otherwise. + # Determines whether the data should be rejected or accepted. # - def reject?(field) - !(accept?(field)) + # @return [Boolean] + # Specifies whether the given data was rejected, using the rules + # rejection patterns. + # + def reject?(data) + !(accept?(data)) end protected # - # Tests the specified _field_ against the specified _rule_. Returns - # +true+ when the _rule_ matches the specified _field_, returns - # +false+ otherwise. + # Tests the given data against a given pattern. # - def test_field(field,rule) + # @return [Boolean] + # Specifies whether the given data matched the pattern. + # + def test_data(data,rule) if rule.kind_of?(Proc) - return (rule.call(field) == true) + return (rule.call(data) == true) elsif rule.kind_of?(Regexp) - return !((field.to_s =~ rule).nil?) + return !((data.to_s =~ rule).nil?) else - return field == rule + return data == rule end end end end