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
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#source ⇒ Object
(also: #cte)
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #base_name ⇒ Object
- #base_table ⇒ Object
-
#col(name) ⇒ Object
(also: #column)
Grant an easy access to arel table columns.
-
#initialize(base, 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 Arel-based select manager - 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.
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
#base ⇒ Object (readonly)
Returns the value of attribute base.
9 10 11 |
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 9 def base @base end |
#source ⇒ Object (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_name ⇒ Object
22 23 24 |
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 22 def base_name @base.name end |
#base_table ⇒ Object
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
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_table ⇒ Object
Get the arel version of the table set on the query
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 |