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(source) ⇒ Settings

Returns a new instance of Settings.



13
14
15
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 13

def initialize(source)
  @source = source
end

Instance Attribute Details

#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

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

Grant an easy access to arel table columns



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

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 string or a proc that requires the table name as first argument

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 39

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

  valid_type = command.respond_to?(:call) || command.is_a?(String)
  raise ArgumentError, <<-MSG.strip.gsub(/\n +/, ' ') 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.strip.gsub(/\n +/, ' ') 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)


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

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

#sql(string) ⇒ Object

Grant an easy access to arel sql literal



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

def sql(string)
  ::Arel::Nodes::SqlLiteral.new(string)
end