Module Criteria::Filterable
In: lib/expressions.rb
lib/filterable.rb

mixin for classes that can contain expressions

Methods

External Aliases

lt -> before
gt -> after

Attributes

expressions  [R] 

Public Class methods

[Source]

     # File lib/expressions.rb, line 137
137:     def Filterable.expression name, klass=nil
138:       class_name = if klass
139:         klass.name
140:       else
141:         "#{name.to_s.capitalize}Expression"
142:       end
143:       module_eval "def \#{name.to_s} name, value\nadd \#{class_name}.new(name, value)\nend\n"
144:     end

Public Instance methods

<<(expression, &block)

Alias for add

Adds an expression to the current Filterable. Propagates the Filterable‘s TableAlias to the newly added expression.

[Source]

    # File lib/filterable.rb, line 9
 9:     def add(expression, &block)
10:       (@expressions ||= []) << expression
11:       expression.table_alias = table_alias unless expression.is_a? TableAlias
12:       #yield expression if block_given?

13:       expression.instance_eval &block if block_given?
14:       return self unless expression.class.include? Filterable
15:       expression
16:     end
and(&block)

Alias for conjunction

builds the condition (where clause) for this query

[Source]

    # File lib/filterable.rb, line 56
56:     def conditions
57:       return nil unless @expressions
58:       con = @expressions.inject([]) do |cond,expr| 
59:           cond << expr.conditions if expr.conditions and not expr.conditions.empty?
60:           cond
61:         end
62:       return " (  #{con.compact.join(' AND ')}  ) " if con and not con.empty?
63:       nil
64:     end

[Source]

     # File lib/expressions.rb, line 174
174:     def conjunction &block
175:       add Conjunction.new, &block
176:     end

[Source]

     # File lib/expressions.rb, line 169
169:     def disjunction &block
170:       add Disjunction.new, &block
171:     end

creates a new expression. If the field name the expression is created for is not valid for the current TableAlias, an exception is raised.

[Source]

    # File lib/filterable.rb, line 49
49:     def expression_for( expression_name, attribute=nil, *args)
50:       # check if an attribute of this name exists

51:       raise "#{attribute.to_s} is not a column on model class #{table_alias.model_class.name}" unless attribute and table_alias.model_class.columns_hash[attribute.to_s]
52:       send expression_name, attribute, *args
53:     end

delegates to the find() method of the parent query

[Source]

    # File lib/filterable.rb, line 21
21:     def find options={}
22:       query.find options
23:     end

[Source]

     # File lib/expressions.rb, line 179
179:     def is_not_null name
180:       add IsNotNullExpression.new(name)
181:     end

[Source]

     # File lib/expressions.rb, line 183
183:     def is_null name
184:       add IsNullExpression.new(name)
185:     end

dynamically builds aliases for adding criteria. If a program calls some_name_<operation>, send(:operation, ‘some_name’, args) will be executed

[Source]

    # File lib/filterable.rb, line 27
27:     def method_missing name, *args
28:       name = name.to_s
29:       # check for magic join

30:       if table_alias.model_class.reflect_on_association(name.intern)
31:         return join(name.intern)
32:       end
33:       case name
34:         when /(.*)_is_null/
35:           return expression_for(:is_null, $1)
36:         when /(.*)_is_not_null/
37:           return expression_for(:is_not_null, $1)
38:         when /(.*)_not_(.*)/
39:           return expression_for(:not).expression_for($2, $1.intern, *args)
40:           return self
41:         when /(.*)_(.*)/
42:           return expression_for($2, $1.intern, *args)
43:         else
44:           super
45:       end
46:     end

[Source]

     # File lib/expressions.rb, line 165
165:     def not &block
166:       add NotExpression.new, &block
167:     end
or(&block)

Alias for disjunction

builds he positional parameter array for this query

[Source]

    # File lib/filterable.rb, line 67
67:     def parameters
68:       return nil unless @expressions
69:       @expressions.inject([]) do |params,expr|
70:         if expr.parameters
71:           if expr.class.include? Filterable
72:             params += expr.parameters
73:           else
74:             params << expr.parameters
75:           end
76:         else
77:           params
78:         end
79:       end.compact
80:     end

[Validate]