Sha256: fb4c6c44c3b8b6ee49d0bdfe8e22c51c1309933b55df10b9970f830748ba8549

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

module Mutations
  class TimeFilter < AdditionalFilter
    @default_options = {
      :nils => false,       # true allows an explicit nil to be valid. Overrides any other options
      :format => nil,       # If nil, Time.parse will be used for coercion, otherwise we will use Time.strptime
      :after => nil,        # A Time object, representing the minimum time allowed, inclusive
      :before => nil        # A Time object, representing the maximum time 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?(Time) # Time
        actual_time = data
      elsif data.is_a?(String)
        begin
          actual_time = if options[:format]
                          Time.strptime(data, options[:format])
                        else
                          Time.parse(data)
                        end
        rescue ArgumentError
          return [nil, :time]
        end
      elsif data.respond_to?(:to_time) # Date, DateTime
        actual_time = data.to_time
      else
        return [nil, :time]
      end

      if options[:after]
        if actual_time <= options[:after]
          return [nil, :after]
        end
      end

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mutations-0.9.1 lib/mutations/time_filter.rb
mutations-0.9.0 lib/mutations/time_filter.rb
mutations-0.8.3 lib/mutations/time_filter.rb
mutations-0.8.2 lib/mutations/time_filter.rb
mutations-0.8.1 lib/mutations/time_filter.rb
mutations-0.8.0 lib/mutations/time_filter.rb