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
-
#_(expr) ⇒ Object
Create a Grouping node.
-
#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)
108 109 110 111 112 113 114 |
# File 'lib/baby_squeel/dsl.rb', line 108 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
#_(expr) ⇒ Object
Create a Grouping node. This allows you to set balanced pairs of parentheses around your SQL.
Arguments
-
expr- The expression to group.
Example
Post.where.has{_([summary, description]).in(...)}
#=> SELECT "posts".* FROM "posts" WHERE ("posts"."summary", "posts"."description") IN (...)"
Post.select{[id, _(Comment.where.has{post_id == posts.id}.selecting{COUNT(id)})]}.as('comment_count')}
#=> SELECT "posts"."id", (SELECT COUNT("comments"."id") FROM "comments" WHERE "comments.post_id" = "posts"."id") AS "comment_count" FROM "posts"
40 41 42 43 |
# File 'lib/baby_squeel/dsl.rb', line 40 def _(expr) expr = Arel.sql(expr.to_sql) if expr.is_a? ::ActiveRecord::Relation Nodes.wrap Arel::Nodes::Grouping.new(expr) end |
#evaluate(&block) ⇒ Object
Evaluates a DSL block. If arity is given, this method ‘yield` itself, rather than `instance_eval`.
98 99 100 101 102 103 104 |
# File 'lib/baby_squeel/dsl.rb', line 98 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) }
69 70 71 |
# File 'lib/baby_squeel/dsl.rb', line 69 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.selecting { func('coalesce', id, 1) }
#=> SELECT COALESCE("posts"."id", 1) FROM "posts"
56 57 58 |
# File 'lib/baby_squeel/dsl.rb', line 56 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) }
82 83 84 |
# File 'lib/baby_squeel/dsl.rb', line 82 def not_exists(rel) func 'NOT EXISTS', sql(rel.to_sql) end |
#quoted(value) ⇒ Object
Quotes a string and marks it as SQL
92 93 94 |
# File 'lib/baby_squeel/dsl.rb', line 92 def quoted(value) sql _scope.connection.quote(value) end |