Sha256: 5bb99ba614296957c67f83d23b3dd4f0aef6adf3ca53bee8f60fe60fdc074a4f

Contents?: true

Size: 670 Bytes

Versions: 10

Compression:

Stored size: 670 Bytes

Contents

module Mutations
  class DuckFilter < AdditionalFilter
    @default_options = {
      :nils => false,       # true allows an explicit nil to be valid. Overrides any other options
      :methods => nil       # The object needs to respond to each of the symbols in this array.
    }

    def filter(data)

      # Handle nil case
      if data.nil?
        return [nil, nil] if options[:nils]
        return [nil, :nils]
      end

      # Ensure the data responds to each of the methods
      Array(options[:methods]).each do |method|
        return [data, :duck] unless data.respond_to?(method)
      end

      # We win, it's valid!
      [data, nil]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mutations-0.9.1 lib/mutations/duck_filter.rb
mutations-0.9.0 lib/mutations/duck_filter.rb
mutations-0.8.3 lib/mutations/duck_filter.rb
mutations-0.8.2 lib/mutations/duck_filter.rb
mutations-0.8.1 lib/mutations/duck_filter.rb
mutations-0.8.0 lib/mutations/duck_filter.rb
mutations-0.7.2 lib/mutations/duck_filter.rb
mutations-0.7.1 lib/mutations/duck_filter.rb
mutations-0.7.0 lib/mutations/duck_filter.rb
mutations-0.6.0 lib/mutations/duck_filter.rb