Class: BabySqueel::DSL
- Defined in:
- lib/baby_squeel/dsl.rb
Instance Attribute Summary
Attributes inherited from Relation
Attributes inherited from Table
Class Method Summary collapse
-
.evaluate(scope, &block) ⇒ Object
Evaluates a block and unwraps the nodes.
-
.evaluate!(scope, &block) ⇒ Object
Evaluates a block in the context of a DSL instance.
-
.evaluate_sifter(scope, *args, &block) ⇒ Object
Evaluates a block in the context of a new DSL instance and passes all arguments to the block.
Instance Method Summary collapse
-
#evaluate(&block) ⇒ Object
Evaluates a DSL block.
-
#exists(relation) ⇒ Object
Generate an EXISTS subselect from an ActiveRecord::Relation.
-
#func(name, *args) ⇒ Object
Create a SQL function.
-
#not_exists(rel) ⇒ Object
Generate a NOT EXISTS subselect from an ActiveRecord::Relation.
-
#quoted(value) ⇒ Object
Quotes a string and marks it as SQL.
-
#sql(value) ⇒ Object
See Arel::sql.
Methods inherited from Relation
#association, #initialize, #sift
Methods inherited from Table
#[], #_arel, #alias, #alias!, #find_alias, #initialize, #inner, #inner!, #on, #on!, #outer, #outer!
Constructor Details
This class inherits a constructor from BabySqueel::Relation
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object (private)
90 91 92 93 94 95 96 |
# File 'lib/baby_squeel/dsl.rb', line 90 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 and unwraps the nodes
9 10 11 |
# File 'lib/baby_squeel/dsl.rb', line 9 def evaluate(scope, &block) Nodes.unwrap evaluate!(scope, &block) end |
.evaluate!(scope, &block) ⇒ Object
Evaluates a block in the context of a DSL instance
14 15 16 |
# File 'lib/baby_squeel/dsl.rb', line 14 def evaluate!(scope, &block) new(scope).evaluate(&block) end |
.evaluate_sifter(scope, *args, &block) ⇒ Object
Evaluates a block in the context of a new DSL instance and passes all arguments to the block.
20 21 22 23 24 |
# File 'lib/baby_squeel/dsl.rb', line 20 def evaluate_sifter(scope, *args, &block) evaluate scope do |root| root.instance_exec(*args, &block) end end |
Instance Method Details
#evaluate(&block) ⇒ Object
Evaluates a DSL block. If arity is given, this method ‘yield` itself, rather than `instance_eval`.
80 81 82 83 84 85 86 |
# File 'lib/baby_squeel/dsl.rb', line 80 def evaluate(&block) if block.arity.zero? instance_eval(&block) else yield(self) end end |
#exists(relation) ⇒ Object
Generate an EXISTS subselect from an ActiveRecord::Relation
Arguments
-
relation- An ActiveRecord::Relation
Example
Post.where.has { exists Post.where(id: 1) }
51 52 53 |
# File 'lib/baby_squeel/dsl.rb', line 51 def exists(relation) func 'EXISTS', sql(relation.to_sql) 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"
38 39 40 |
# File 'lib/baby_squeel/dsl.rb', line 38 def func(name, *args) Nodes.wrap Arel::Nodes::NamedFunction.new(name.to_s, args) end |
#not_exists(rel) ⇒ Object
Generate a NOT EXISTS subselect from an ActiveRecord::Relation
Arguments
-
relation- An ActiveRecord::Relation
Example
Post.where.has { not_exists Post.where(id: 1) }
64 65 66 |
# File 'lib/baby_squeel/dsl.rb', line 64 def not_exists(rel) func 'NOT EXISTS', sql(rel.to_sql) end |
#quoted(value) ⇒ Object
Quotes a string and marks it as SQL
74 75 76 |
# File 'lib/baby_squeel/dsl.rb', line 74 def quoted(value) sql _scope.connection.quote(value) end |