class Activecube::Query::Item

Attributes

cube[R]
definition[R]
key[R]

Public Class Methods

new(cube, key, definition) click to toggle source
# File lib/activecube/query/item.rb, line 7
def initialize cube, key, definition
  @key = key
  @cube = cube
  @definition = definition
end

Public Instance Methods

alias!(new_key) click to toggle source
# File lib/activecube/query/item.rb, line 17
def alias! new_key
  self.class.new cube, new_key, definition
end
append_with!(model, cube_query, table, query) click to toggle source
# File lib/activecube/query/item.rb, line 25
def append_with! model, cube_query, table, query

  if definition.respond_to?(:with_expression) &&
    (with_expression = definition.with_expression(model, cube_query, table, query))
    with_expression.each_pair do |key, expr|
      query = try_append_with(query, key, expr)
    end
  end
  query
end
required_column_names() click to toggle source
# File lib/activecube/query/item.rb, line 13
def required_column_names
  definition.class.column_names || []
end
to_s() click to toggle source
# File lib/activecube/query/item.rb, line 21
def to_s
  "#{definition.class.name}(#{key})"
end

Private Instance Methods

try_append_with(query, key, expr) click to toggle source
# File lib/activecube/query/item.rb, line 40
def try_append_with(query, key, expr)
  expr = Arel.sql(expr) if expr.kind_of?(String)
  query = query.where(Arel.sql('1')) unless query.respond_to?(:ast)
  if (with = query.ast.with)
    existing = with.expr.detect{|expr| expr.right==key }
    if existing
      raise "Key #{key} defined twice in WITH statement, with different expressions #{expr.to_sql} AND #{existing.left}" if existing.left!=expr.to_s
      query
    else
      query.with(with.expr + [expr.as(key)])
    end
  else
    query.with(expr.as(key))
  end

end