Class: Torque::PostgreSQL::AuxiliaryStatement::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/auxiliary_statement/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, source) ⇒ Settings

Returns a new instance of Settings.



15
16
17
18
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 15

def initialize(base, source)
  @base = base
  @source = source
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 7

def base
  @base
end

#sourceObject (readonly) Also known as: cte

Returns the value of attribute source.



7
8
9
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 7

def source
  @source
end

Instance Method Details

#base_nameObject



20
21
22
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 20

def base_name
  @base.name
end

#base_tableObject



24
25
26
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 24

def base_table
  @base.arel_table
end

#col(name) ⇒ Object Also known as: column

Grant an easy access to arel table columns



36
37
38
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 36

def col(name)
  query_table[name.to_s]
end

#query(value = nil, command = nil) ⇒ Object

There are two ways of setting the query:

  • A simple relation based on a Model

  • A Arel-based select manager

  • A string or a proc that requires the table name as first argument

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 46

def query(value = nil, command = nil)
  return @query if value.nil?
  return @query = value if relation_query?(value)

  if value.is_a?(::Arel::SelectManager)
    @query = value
    @query_table = value.source.left.name
    return
  end

  valid_type = command.respond_to?(:call) || command.is_a?(String)

  raise ArgumentError, <<-MSG.squish if command.nil?
    To use proc or string as query, you need to provide the table name
    as the first argument
  MSG

  raise ArgumentError, <<-MSG.squish unless valid_type
    Only relation, string and proc are valid object types for query,
    #{command.inspect} given.
  MSG

  @query = command
  @query_table = ::Arel::Table.new(value)
end

#query_tableObject

Get the arel version of the table set on the query

Raises:

  • (StandardError)


29
30
31
32
33
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 29

def query_table
  raise StandardError, 'The query is not defined yet' if query.nil?
  return query.arel_table if relation_query?(query)
  @query_table
end