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.



17
18
19
20
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 17

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



9
10
11
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 9

def base
  @base
end

#sourceObject (readonly) Also known as: cte

Returns the value of attribute source.



9
10
11
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 9

def source
  @source
end

Instance Method Details

#base_nameObject



22
23
24
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 22

def base_name
  @base.name
end

#base_tableObject



26
27
28
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 26

def base_table
  @base.arel_table
end

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

Grant an easy access to arel table columns



38
39
40
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 38

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)


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

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)


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

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