Class: Torque::PostgreSQL::AuxiliaryStatement::Settings
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::AuxiliaryStatement::Settings
- Defined in:
- lib/torque/postgresql/auxiliary_statement/settings.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
(also: #cte)
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#col(name) ⇒ Object
(also: #column)
Grant an easy access to arel table columns.
-
#initialize(source) ⇒ Settings
constructor
A new instance of Settings.
-
#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.
-
#query_table ⇒ Object
Get the arel version of the table set on the query.
-
#sql(string) ⇒ Object
Grant an easy access to arel sql literal.
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
#source ⇒ Object (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
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_table ⇒ Object
Get the arel version of the table set on the query
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 |