Class RecordFilter::DSL::Restriction

  1. lib/record_filter/dsl/restriction.rb (view online)
Parent: Object

The Restriction class is used to specify conditions in your filters. It is accessed through the use of ConjunctionDSL.with, which can be chained with further methods below in order to create many types of conditions.

Blog.filter do
  with(:id, 2)          # :conditions => { ['id = ?', 2] }
  with(:id).is_null     # :conditions => { ['id IS NULL'] }
  with(:id).in([2,3,4]) # :conditions => { ['id IN (?)', [2,3,4]] }
  ...
end

The following restriction types are supported:

  • Between
  • Equality
  • Comparisons (>, >=, <, <=)
  • In
  • Is Null and Is Not Null
  • Like
  • Negation of any restriction through not

Constants

DEFAULT_VALUE = Object.new

Public instance methods

and ()

Create a secondary AND restriction that will be conjuncted with the current one. This function allows you to easily create restrictions on one line that would otherwise require the use of all_of and is intended as a space-saver.

Example

with(:expired_at).greater_than(Time.now).and.less_than(3.days.from_now)

# conditions => [‘expired_at > ? AND expired_at < ?’, Time.now, 3.days.from_now]

Returns

Restriction:A new restriction object that this one will be OR’ed with.

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 298
      def and
        @conjuncted_restriction_type = :all_of
        @conjuncted_restriction = Restriction.new(@column)
      end
between (start, finish=nil)

Create a between filter of the form [‘column BETWEEN ? AND ?’, start, finish]

Parameters

start:The starting limit for the between test.
finish:The ending limit for the between test.

Returns

Restriction:self

Alternatives

With the second argument omitted, the method can also accept either a tuple (Array) or a range as the first argument. If a tuple is given, it will be used as [start, finish]. If a range is given, its beginning and ending values will be used.

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 253
      def between(start, finish=nil)
        @operator = :between
        if !finish.nil?
          @value = [start, finish]
        else
          @value = start
        end
        self
      end
equal_to (value)

Make the restriction into an equality test of the form [‘column = ?’, value]

Parameters

value:The value to use in the restriction.

Returns

Restriction:self

Alternatives

If nil is passed as the value, this will create an is_null restriction.

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 90
      def equal_to(value)
        @value, @operator = value, :equal_to
        @value, @operator = nil, :is_null if value.nil?
        self
      end
greater_than (value)

Create a comparison restriction of the form [‘column > ?’, value]

Parameters

value:The value to be used for comparison.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 169
      def greater_than(value)
        @value, @operator = value, :greater_than
        self
      end
greater_than_or_equal_to (value)

Create a comparison restriction of the form [‘column >= ?’, value]

Parameters

value:The value to be used in the comparison.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 185
      def greater_than_or_equal_to(value)
        @value, @operator = value, :greater_than_or_equal_to
        self
      end
gt (value)

Alias for greater_than

gte (value)
in (value)

Create an IN restriction of the form [‘column IN (?)’, value]

Parameters

value:Either a single item, an array of values, or a range to form the values to be tested for inclusion.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 201
      def in(value)
        @value, @operator = value, :in
        self
      end
is_not_null ()

Change the restriction into a negated test for null of the form [‘column IS NOT NULL’]

Parameters

none

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 121
      def is_not_null
        @operator = :is_null
        @negated = true
        self
      end
is_null ()

Change the restriction into a test for null in the form [‘column IS NULL’]

Parameters

none

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 105
      def is_null
        @value, @operator = nil, :is_null
        self
      end
less_than (value)

Create a less_than condition of the form [‘column < ?’, value]

Parameters

value:The value to be compared.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 137
      def less_than(value)
        @value, @operator = value, :less_than
        self
      end
less_than_or_equal_to (value)

Create a comparison restriction of the form [‘column <= ?’, value]

Parameters

value:The value to be used in the comparison.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 153
      def less_than_or_equal_to(value)
        @value, @operator = value, :less_than_or_equal_to
        self
      end
like (value)

Create a LIKE restriction of the form [‘column LIKE ?’, value]

Parameters

value:The value to be tested against the column.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 231
      def like(value)
        @value, @operator = value, :like
        self
      end
lt (value)

Alias for less_than

lte (value)
nil ()

Alias for is_null

not (value=DEFAULT_VALUE)

Negate the restriction, taking an optional value for inequality restrictions.

Parameters

value:A value to be used when specifying inequality restrictions.

Returns

Restriction:self

Alternatives

The value option works in the same way as in the initializer, as a shortcut for inequality or is_null restrictions if it is provided. This allows restrictions to be specified like: with(:permalink).not(nil) If no value is specified, it will simply negate the restriction.

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 71
      def not(value=DEFAULT_VALUE)
        @negated = !@negated
        take_value(value)
        self
      end
not_in (value)

Create a negated IN restriction of the form [‘column NOT IN (?)’, value]

Parameters

value:Either a single item or an array of values to form the inclusion test.

Returns

Restriction:self

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 216
      def not_in(value)
        @value, @operator, @negated = value, :in, true
        self
      end
null ()

Alias for is_null

or ()

Create a secondary OR restriction that will be conjuncted with the current one. This function allows you to easily create restrictions on one line that would otherwise require the use of any_of and is intended as a space-saver.

Example

with(:expired_at).greater_than(Time.now).or.is_null

# conditions => [‘expired_at > ? OR expired_at IS NULL’, Time.now]

Returns

Restriction:A new restriction object that this one will be OR’ed with.

@public

[show source]
# File lib/record_filter/dsl/restriction.rb, line 278
      def or
        @conjuncted_restriction_type = :any_of
        @conjuncted_restriction = Restriction.new(@column)
      end