Module: Mao::Filter

Included in:
Binary, Column
Defined in:
lib/mao/filter.rb

Overview

A mix-in for any kind of filter in a where clause (vis-à-vis Mao::Query#where).

Defined Under Namespace

Classes: Binary, Column, Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

The options hash for this filter.



34
35
36
# File 'lib/mao/filter.rb', line 34

def options
  @options
end

Class Method Details

.finalize_or_literal(obj) ⇒ Object

If obj is a Mao::Filter, call #finalize on it; otherwise, use Mao.escape_literal to escape obj.to_s.



8
9
10
11
12
13
14
# File 'lib/mao/filter.rb', line 8

def self.finalize_or_literal(obj)
  if obj.is_a? Mao::Filter
    obj.finalize
  else
    Mao.escape_literal(obj)
  end
end

.sql(finalized) ⇒ Object

Generate the SQL for the finalized object finalized. If finalized is a String, it’s returned without modification.



18
19
20
21
22
23
24
25
# File 'lib/mao/filter.rb', line 18

def self.sql(finalized)
  if finalized.is_a? String
    finalized
  else
    klass, *args = finalized
    Mao::Filter.const_get(klass).sql(*args)
  end
end

Instance Method Details

#!=(rhs) ⇒ Object

Returns an inequality binary filter where the current object is the LHS and rhs is the RHS.



56
57
58
# File 'lib/mao/filter.rb', line 56

def !=(rhs)
  Mao::Filter::Binary.new(:op => '<>', :lhs => self, :rhs => rhs)
end

#<(rhs) ⇒ Object

Returns a less-than binary filter where the current object is the LHS and rhs is the RHS.



74
75
76
# File 'lib/mao/filter.rb', line 74

def <(rhs)
  Mao::Filter::Binary.new(:op => '<', :lhs => self, :rhs => rhs)
end

#<=(rhs) ⇒ Object

Returns a less-than-or-equal-to binary filter where the current object is the LHS and rhs is the RHS.



80
81
82
# File 'lib/mao/filter.rb', line 80

def <=(rhs)
  Mao::Filter::Binary.new(:op => '<=', :lhs => self, :rhs => rhs)
end

#==(rhs) ⇒ Object

Returns an equality binary filter where the current object is the LHS and rhs is the RHS.



50
51
52
# File 'lib/mao/filter.rb', line 50

def ==(rhs)
  Mao::Filter::Binary.new(:op => '=', :lhs => self, :rhs => rhs)
end

#>(rhs) ⇒ Object

Returns a greater-than binary filter where the current object is the LHS and rhs is the RHS.



62
63
64
# File 'lib/mao/filter.rb', line 62

def >(rhs)
  Mao::Filter::Binary.new(:op => '>', :lhs => self, :rhs => rhs)
end

#>=(rhs) ⇒ Object

Returns a greater-than-or-equal-to binary filter where the current object is the LHS and rhs is the RHS.



68
69
70
# File 'lib/mao/filter.rb', line 68

def >=(rhs)
  Mao::Filter::Binary.new(:op => '>=', :lhs => self, :rhs => rhs)
end

#and(rhs) ⇒ Object

Returns an AND filter where the current object is the LHS and rhs is the RHS.



38
39
40
# File 'lib/mao/filter.rb', line 38

def and(rhs)
  Mao::Filter::Binary.new(:op => 'AND', :lhs => self, :rhs => rhs).freeze
end

#in(rhs) ⇒ Object

Returns a filter where the current object is checked if it is IN rhs, typically a list.



94
95
96
# File 'lib/mao/filter.rb', line 94

def in(rhs)
  Mao::Filter::Binary.new(:op => 'IN', :lhs => self, :rhs => rhs)
end

#initialize(options = {}) ⇒ Object

Initialize a filter object with the given options. Filters are intended to be used immutably, and all methods on same return new, immutable filters.



29
30
31
# File 'lib/mao/filter.rb', line 29

def initialize(options={})
  @options = options.freeze
end

#null?Boolean

Returns a filter where the current object is checked if it IS NULL.

HACK(arlen): ? Calling this "nil?" results in the world crashing down
around us.  But it seems a pity to have this be not-quite-like-Ruby.
Would it be better to make #==(nil) map to IS NULL instead of = NULL?


88
89
90
# File 'lib/mao/filter.rb', line 88

def null?
  Mao::Filter::Binary.new(:op => 'IS', :lhs => self, :rhs => nil)
end

#or(rhs) ⇒ Object

Returns an OR filter where the current object is the LHS and rhs is the RHS.



44
45
46
# File 'lib/mao/filter.rb', line 44

def or(rhs)
  Mao::Filter::Binary.new(:op => 'OR', :lhs => self, :rhs => rhs).freeze
end