Sha256: 33a2b6304f89786e41a102d3a71b940180492a32c80f679f4c37a7ea32b6348b

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module Mutations
  class DateFilter < AdditionalFilter
    @default_options = {
      :nils => false,       # true allows an explicit nil to be valid. Overrides any other options
      :format => nil,       # If nil, Date.parse will be used for coercion. If something like "%Y-%m-%d", Date.strptime is used
      :after => nil,        # A date object, representing the minimum date allowed, inclusive
      :before => nil        # A date object, representing the maximum date allowed, inclusive
    }

    def filter(data)
      # Handle nil case
      if data.nil?
        return [nil, nil] if options[:nils]
        return [nil, :nils]
      end
      
      # Now check if it's empty:
      return [data, :empty] if "" == data 

      if data.is_a?(Date) # Date and DateTime
        actual_date = data
      elsif data.is_a?(String)
        begin
          actual_date = if options[:format]
            Date.strptime(data, options[:format])
          else
            Date.parse(data)
          end
        rescue ArgumentError
          return [nil, :date]
        end
      elsif data.respond_to?(:to_date)  # Time
        actual_date = data.to_date
      else
        return [nil, :date]
      end
      
      # Ok, its a valid date, check if it falls within the range
      if options[:after]
        if actual_date <= options[:after]
          return [nil, :after]
        end
      end

      if options[:before]
        if actual_date >= options[:before]
          return [nil, :before]
        end
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutations-0.8.2 lib/mutations/date_filter.rb
mutations-0.8.1 lib/mutations/date_filter.rb
mutations-0.8.0 lib/mutations/date_filter.rb