Class: Hayfork::StatementBuilder
- Inherits:
-
Object
- Object
- Hayfork::StatementBuilder
- Defined in:
- lib/hayfork/statement_builder.rb
Instance Attribute Summary collapse
-
#row ⇒ Object
readonly
Returns the value of attribute row.
Instance Method Summary collapse
-
#initialize(haystack, relation, attributes: {}) ⇒ StatementBuilder
constructor
A new instance of StatementBuilder.
- #insert(field, options = {}) ⇒ Object
-
#joins(join_value, &block) ⇒ Object
reject SQL literals?.
- #set(key, value) ⇒ Object
- #to_a ⇒ Object
- #to_delete_sql ⇒ Object
- #to_insert_sql(**args) ⇒ Object
- #to_update_sql ⇒ Object
-
#where(*args, &block) ⇒ Object
reject SQL literals?.
Constructor Details
#initialize(haystack, relation, attributes: {}) ⇒ StatementBuilder
Returns a new instance of StatementBuilder.
7 8 9 10 11 12 13 |
# File 'lib/hayfork/statement_builder.rb', line 7 def initialize(haystack, relation, attributes: {}) @haystack = haystack @relation = relation @attributes = attributes @statements = [] @row = model.arel_table end |
Instance Attribute Details
#row ⇒ Object (readonly)
Returns the value of attribute row.
5 6 7 |
# File 'lib/hayfork/statement_builder.rb', line 5 def row @row end |
Instance Method Details
#insert(field, options = {}) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/hayfork/statement_builder.rb', line 29 def insert(field, ={}) Statement.new(haystack, relation, field, ).tap do |statement| statement.merge(attributes) if attributes.any? statements << statement end end |
#joins(join_value, &block) ⇒ Object
reject SQL literals?
20 21 22 |
# File 'lib/hayfork/statement_builder.rb', line 20 def joins(join_value, &block) # reject SQL literals? apply? Hayfork.join(relation, join_value), &block end |
#set(key, value) ⇒ Object
16 17 18 |
# File 'lib/hayfork/statement_builder.rb', line 16 def set(key, value) @attributes.merge!(key => value) end |
#to_a ⇒ Object
52 53 54 |
# File 'lib/hayfork/statement_builder.rb', line 52 def to_a statements end |
#to_delete_sql ⇒ Object
47 48 49 |
# File 'lib/hayfork/statement_builder.rb', line 47 def to_delete_sql statements.map { |statement| " " << statement.delete.to_sql }.join.strip end |
#to_insert_sql(**args) ⇒ Object
37 38 39 |
# File 'lib/hayfork/statement_builder.rb', line 37 def to_insert_sql(**args) statements.map { |statement| " " << statement.insert.to_sql(**args) }.join.strip end |
#to_update_sql ⇒ Object
41 42 43 44 45 |
# File 'lib/hayfork/statement_builder.rb', line 41 def to_update_sql updates = statements.select(&:may_change_on_update?) return "-- nothing to update" if updates.empty? updates.map { |statement| " " << statement.update.to_sql.lstrip }.join.strip end |
#where(*args, &block) ⇒ Object
reject SQL literals?
24 25 26 |
# File 'lib/hayfork/statement_builder.rb', line 24 def where(*args, &block) # reject SQL literals? apply? relation.where(*args), &block end |