Sha256: 7866e5af89668e03546b7926dc290d50441f0697ea3755a89aa792202c91159a

Contents?: true

Size: 966 Bytes

Versions: 7

Compression:

Stored size: 966 Bytes

Contents

module QueryReport
  class Filter
    attr_reader :column, :type, :comparators, :block, :custom

    def initialize(column, options, &block)
      @column = column
      @type = options if options.kind_of? String
      if options.kind_of? Hash
        @type = options[:type]
        @comparators = options[:comp] || detect_comparators(@type)
      end
      @block = block
      @custom = @block ? true : false
    end

    def self.supported_types
      [:date, :text]
    end

    def keys
      @keys ||= (@comparators || {}).keys.map { |comp| "#{column.to_s}_#{comp}" }
    end

    supported_types.each do |supported_type|
      define_method("#{supported_type.to_s}?") do
        @type == supported_type
      end
    end

    private
    def detect_comparators(type)
      case type
        when :date
          return {gteq: 'From', lteq: 'To'}
        when :text
          return {cont: @column.to_s.humanize}
      end
      {eq: 'Equal'}
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
query_report-0.0.7 lib/query_report/filter.rb
query_report-0.0.6 lib/query_report/filter.rb
query_report-0.0.5 lib/query_report/filter.rb
query_report-0.0.4 lib/query_report/filter.rb
query_report-0.0.3 lib/query_report/filter.rb
query_report-0.0.2 lib/query_report/filter.rb
query_report-0.0.1 lib/query_report/filter.rb