class Hayfork::StatementBuilder

Attributes

attributes[R]
haystack[R]
relation[R]
row[R]
statements[R]

Public Class Methods

new(haystack, relation, attributes: {}) click to toggle source
# 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

Public Instance Methods

insert(field, options={}) click to toggle source
# File lib/hayfork/statement_builder.rb, line 29
def insert(field, options={})
  Statement.new(haystack, relation, field, options).tap do |statement|
    statement.merge(attributes) if attributes.any?
    statements << statement
  end
end
joins(join_value, &block) click to toggle source
# 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) click to toggle source
# File lib/hayfork/statement_builder.rb, line 16
def set(key, value)
  @attributes.merge!(key => value)
end
to_a() click to toggle source
# File lib/hayfork/statement_builder.rb, line 52
def to_a
  statements
end
to_delete_sql() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/hayfork/statement_builder.rb, line 24
def where(*args, &block) # reject SQL literals?
  apply? relation.where(*args), &block
end

Private Instance Methods

apply?(modified_relation) { |statements| ... } click to toggle source
# File lib/hayfork/statement_builder.rb, line 59
def apply?(modified_relation, &block)
  if block_given?
    statements = StatementBuilder.new(haystack, modified_relation, attributes: attributes.dup)
    if block.arity.zero?
      statements.instance_eval(&block)
    else
      yield statements
    end
    @statements.concat statements.to_a
  else
    @relation = modified_relation
  end
end
model() click to toggle source
# File lib/hayfork/statement_builder.rb, line 73
def model
  relation.model
end