Sha256: a994cb5cf6e514443cfce7eaaaa9e50ce8ba809d1fd9d18cd38d609dc492e103

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module RecordFilter
  module Restrictions # :nodoc: all
    class Base
      def initialize(column_name, value, options={})
        @column_name, @value, @negated = column_name, value, !!options.delete(:negated)
        @value = @value.id if @value.kind_of?(ActiveRecord::Base)
      end

      def to_conditions
        @value.nil? ? [to_sql] : [to_sql, @value]
      end

      def to_sql
        @negated ? to_negative_sql : to_positive_sql
      end

      def to_negative_sql
        "NOT (#{to_positive_sql})"
      end
    end

    class EqualTo < Base
      def to_positive_sql
        "#{@column_name} = ?"
      end

      def to_negative_sql
        "#{@column_name} <> ?"
      end
    end

    class IsNull < Base
      def to_positive_sql
        "#{@column_name} IS NULL"
      end

      def to_negative_sql
        "#{@column_name} IS NOT NULL"
      end
    end

    class LessThan < Base
      def to_positive_sql
        "#{@column_name} < ?"
      end
    end

    class LessThanOrEqualTo < Base
      def to_positive_sql
        "#{@column_name} <= ?"
      end
    end

    class GreaterThan < Base
      def to_positive_sql
        "#{@column_name} > ?"
      end
    end

    class GreaterThanOrEqualTo < Base
      def to_positive_sql
        "#{@column_name} >= ?"
      end
    end

    class In < Base
      def to_positive_sql
        "#{@column_name} IN (?)"
      end

      def to_negative_sql
        "#{@column_name} NOT IN (?)"
      end

      def to_conditions
        # Need to put in the value even if it's null in this case.
        [to_sql, @value]
      end
    end

    class Between < Base
      def to_conditions
        ["#{@column_name} #{'NOT ' if @negated}BETWEEN ? AND ?", @value.first, @value.last]
      end
    end

    class Like < Base
      def to_positive_sql
        "#{@column_name} LIKE ?"
      end

      def to_negative_sql
        "#{@column_name} NOT LIKE ?"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aub-record_filter-0.9.4 lib/record_filter/restrictions.rb
aub-record_filter-0.9.5 lib/record_filter/restrictions.rb