Class: Prequel::Relations::Projection

Inherits:
Relation
  • Object
show all
Defined in:
lib/prequel/relations/projection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

#find, #join, #project, #query, #singular_table_ref, #table_ref, #to_relation, #where

Constructor Details

#initialize(operand, *symbols) ⇒ Projection

Returns a new instance of Projection.



6
7
8
9
# File 'lib/prequel/relations/projection.rb', line 6

def initialize(operand, *symbols)
  @operand = operand
  assign_derived_columns(symbols)
end

Instance Attribute Details

#operandObject (readonly)

Returns the value of attribute operand.



4
5
6
# File 'lib/prequel/relations/projection.rb', line 4

def operand
  @operand
end

Instance Method Details

#build_tuple(field_values) ⇒ Object



36
37
38
# File 'lib/prequel/relations/projection.rb', line 36

def build_tuple(field_values)
  tuple_class.new(field_values)
end

#columnsObject



19
20
21
# File 'lib/prequel/relations/projection.rb', line 19

def columns
  derived_columns.values
end

#get_column(name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/prequel/relations/projection.rb', line 11

def get_column(name)
  if name.to_s.include?("__")
    super
  else
    derived_columns_by_name[name]
  end
end

#tuple_classObject



40
41
42
43
44
45
46
47
# File 'lib/prequel/relations/projection.rb', line 40

def tuple_class
  @tuple_class ||= Class.new(Tuple).tap do |tuple_class|
    tuple_class.relation = self
    columns.each do |column|
      tuple_class.def_field_reader(column.name)
    end
  end
end

#visit(query) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prequel/relations/projection.rb', line 23

def visit(query)
  operand.visit(query)
  query.select_list = columns.map do |derived_column|
    query.resolve_derived_column(derived_column)
  end

  if projected_table
    query.tuple_builder = query.singular_table_refs[projected_table]
  else
    query.tuple_builder = self
  end
end