module ActiveHouse::Querying::Union

Public Instance Methods

build_union_query_part() click to toggle source
# File lib/active_house/querying/union.rb, line 10
def build_union_query_part
  return if values[:union].values.empty?

  "UNION ALL\n#{values[:union].values.map(&:to_query).join("\n")}"
end
except_union(name) click to toggle source
# File lib/active_house/querying/union.rb, line 48
def except_union(name)
  dup.except_union!(name)
end
except_union!(name) click to toggle source
# File lib/active_house/querying/union.rb, line 41
def except_union!(name)
  new_unions = values[:union].map { |n, q| [n, q.dup] }.to_h
  new_unions.delete(name.to_sym)
  values[:union] = new_unions
  self
end
format_unions(queries) click to toggle source
# File lib/active_house/querying/union.rb, line 52
def format_unions(queries)
  raise ArgumentError, 'unions must be a Hash' unless queries.is_a?(Hash)
  raise ArgumentError, 'unions hash is empty' if queries.empty?

  new_unions = values[:union].map { |n, q| [n, q.dup] }.to_h

  queries.each do |name, query|
    query = query.all if query.is_a?(ActiveHouse::Model)
    new_unions[name.to_sym] = query.dup
  end

  new_unions
end
initial_values() click to toggle source
Calls superclass method
# File lib/active_house/querying/union.rb, line 16
def initial_values
  super.merge union: {}
end
union(queries) click to toggle source

@param queries [Hash] - hash where key is union name and value is a query key needed for possibility to update/replace union query

# File lib/active_house/querying/union.rb, line 30
def union(queries)
  dup.union!(queries)
end
union!(queries) click to toggle source

@param queries [Hash] - hash where key is union name and value is a query key needed for possibility to update/replace union query

# File lib/active_house/querying/union.rb, line 22
def union!(queries)
  formatted_queries = format_unions(queries)
  values[:union] = formatted_queries
  self
end
update_union(name) { |values[to_sym]| ... } click to toggle source
# File lib/active_house/querying/union.rb, line 34
def update_union(name)
  name = name.to_sym
  raise ArgumentError, "can't find union by name #{name}" unless values[:union].key?(name)
  new_union = yield values[:union][name.to_sym]
  union(name.to_sym => new_union)
end