class Torque::PostgreSQL::AuxiliaryStatement::Settings

Attributes

base[R]
cte[R]
source[R]

Public Class Methods

new(base, source) click to toggle source
# File lib/torque/postgresql/auxiliary_statement/settings.rb, line 17
def initialize(base, source)
  @base = base
  @source = source
end

Public Instance Methods

base_name() click to toggle source
# File lib/torque/postgresql/auxiliary_statement/settings.rb, line 22
def base_name
  @base.name
end
base_table() click to toggle source
# File lib/torque/postgresql/auxiliary_statement/settings.rb, line 26
def base_table
  @base.arel_table
end
col(name) click to toggle source

Grant an easy access to arel table columns

# File lib/torque/postgresql/auxiliary_statement/settings.rb, line 38
def col(name)
  query_table[name.to_s]
end
Also aliased as: column
column(name)
Alias for: col
query(value = nil, command = nil) click to toggle source

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

# 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_table() click to toggle source

Get the arel version of the table set on the query

# 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