Class: BabySqueel::DSL
Instance Attribute Summary
Attributes inherited from Table
Class Method Summary collapse
-
.evaluate(scope, &block) ⇒ Object
Evaluates a block in the context of a new DSL instance.
Instance Method Summary collapse
-
#evaluate(&block) ⇒ Object
Evaluates a DSL block.
-
#func(name, *args) ⇒ Object
Create a SQL function.
Methods inherited from Table
#[], #_arel, #alias, #alias!, #association, #initialize, #inner, #inner!, #on, #on!, #outer, #outer!
Constructor Details
This class inherits a constructor from BabySqueel::Table
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object (private)
39 40 41 42 43 44 45 |
# File 'lib/baby_squeel/dsl.rb', line 39 def method_missing(meth, *args, &block) if !args.empty? && !block_given? func(meth, args) else super end end |
Class Method Details
.evaluate(scope, &block) ⇒ Object
Evaluates a block in the context of a new DSL instance.
8 9 10 |
# File 'lib/baby_squeel/dsl.rb', line 8 def self.evaluate(scope, &block) new(scope).evaluate(&block) end |
Instance Method Details
#evaluate(&block) ⇒ Object
Evaluates a DSL block. If arity is given, this method ‘yield` itself, rather than `instance_eval`.
29 30 31 32 33 34 35 |
# File 'lib/baby_squeel/dsl.rb', line 29 def evaluate(&block) if block.arity.zero? Nodes.unwrap instance_eval(&block) else Nodes.unwrap yield(self) end end |
#func(name, *args) ⇒ Object
Create a SQL function. See Arel::Nodes::NamedFunction.
Arguments
-
name- The name of a SQL function (ex. coalesce). -
args- The arguments to be passed to the SQL function.
Example
Post.select { func('coalesce', id, 1) }
#=> SELECT COALESCE("posts"."id", 1) FROM "posts"
23 24 25 |
# File 'lib/baby_squeel/dsl.rb', line 23 def func(name, *args) Nodes.wrap Arel::Nodes::NamedFunction.new(name.to_s, args) end |