Class: Torque::PostgreSQL::AuxiliaryStatement
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::AuxiliaryStatement
- Defined in:
- lib/torque/postgresql/auxiliary_statement.rb,
lib/torque/postgresql/auxiliary_statement/settings.rb
Defined Under Namespace
Classes: Settings
Constant Summary collapse
- TABLE_COLUMN_AS_STRING =
/\A(?:"?(\w+)"?\.)?"?(\w+)"?\z/.freeze
Class Method Summary collapse
-
.arel_query?(obj) ⇒ Boolean
Identify if the query set may be used as arel.
-
.base ⇒ Object
Get the base class associated to this statement.
-
.base_name ⇒ Object
Get the name of the base class.
-
.base_table ⇒ Object
Get the arel table of the base class.
-
.configurator(block) ⇒ Object
Set a configuration block, if the class is already set up, just clean the query and wait it to be setup again.
-
.instantiate(statement, base, options = nil) ⇒ Object
Create a new instance of an auxiliary statement.
-
.lookup(name, base) ⇒ Object
Find or create the class that will handle statement.
-
.project(column, arel_table = nil) ⇒ Object
Project a column on a given table, or use the column table.
-
.query_table ⇒ Object
Get the arel table of the query.
-
.relation_query?(obj) ⇒ Boolean
Identify if the query set may be used as a relation.
-
.table ⇒ Object
Get the arel version of the statement table.
-
.table_name ⇒ Object
Get the name of the table of the configurated statement.
Instance Method Summary collapse
-
#bound_attributes ⇒ Object
Get the bound attributes from statement qeury.
-
#build_arel(arel, base) ⇒ Object
Build the statement on the given arel and return the WITH statement.
-
#columns ⇒ Object
Get the columns that will be selected for this statement.
-
#ensure_dependencies!(base) ⇒ Object
Ensure that all the dependencies are loaded in the base relation.
-
#initialize(*args) ⇒ AuxiliaryStatement
constructor
Start a new auxiliary statement giving extra options.
Constructor Details
#initialize(*args) ⇒ AuxiliaryStatement
Start a new auxiliary statement giving extra options
189 190 191 192 193 194 195 196 197 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 189 def initialize(*args) = args. args_key = Torque::PostgreSQL.config.auxiliary_statement.send_arguments_key @join = .fetch(:join, {}) @args = .fetch(args_key, {}) @select = .fetch(:select, {}) @join_type = .fetch(:join_type, join_type) end |
Class Method Details
.arel_query?(obj) ⇒ Boolean
Identify if the query set may be used as arel
46 47 48 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 46 def arel_query?(obj) !obj.nil? && obj.is_a?(::Arel::SelectManager) end |
.base ⇒ Object
Get the base class associated to this statement
58 59 60 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 58 def base self.parent end |
.base_name ⇒ Object
Get the name of the base class
63 64 65 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 63 def base_name base.name end |
.base_table ⇒ Object
Get the arel table of the base class
78 79 80 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 78 def base_table @base_table ||= base.arel_table end |
.configurator(block) ⇒ Object
Set a configuration block, if the class is already set up, just clean the query and wait it to be setup again
52 53 54 55 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 52 def configurator(block) @config = block @query = nil end |
.instantiate(statement, base, options = nil) ⇒ Object
Create a new instance of an auxiliary statement
31 32 33 34 35 36 37 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 31 def instantiate(statement, base, = nil) klass = base.auxiliary_statements_list[statement] return klass.new() unless klass.nil? raise ArgumentError, <<-MSG.strip There's no '#{statement}' auxiliary statement defined for #{base.class.name}. MSG end |
.lookup(name, base) ⇒ Object
Find or create the class that will handle statement
24 25 26 27 28 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 24 def lookup(name, base) const = name.to_s.camelize << '_' << self.name.demodulize return base.const_get(const, false) if base.const_defined?(const, false) base.const_set(const, Class.new(AuxiliaryStatement)) end |
.project(column, arel_table = nil) ⇒ Object
Project a column on a given table, or use the column table
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 88 def project(column, arel_table = nil) if column.respond_to?(:as) return column elsif (as_string = TABLE_COLUMN_AS_STRING.match(column.to_s)) column = as_string[2] arel_table = ::Arel::Table.new(as_string[1]) unless as_string[1].nil? end arel_table ||= table arel_table[column.to_s] end |
.query_table ⇒ Object
Get the arel table of the query
83 84 85 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 83 def query_table @query_table ||= query.arel_table end |
.relation_query?(obj) ⇒ Boolean
Identify if the query set may be used as a relation
40 41 42 43 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 40 def relation_query?(obj) !obj.nil? && obj.respond_to?(:ancestors) && \ obj.ancestors.include?(ActiveRecord::Base) end |
.table ⇒ Object
Get the arel version of the statement table
68 69 70 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 68 def table @table ||= ::Arel::Table.new(table_name) end |
.table_name ⇒ Object
Get the name of the table of the configurated statement
73 74 75 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 73 def table_name @table_name ||= self.name.demodulize.split('_').first.underscore end |
Instance Method Details
#bound_attributes ⇒ Object
Get the bound attributes from statement qeury
214 215 216 217 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 214 def bound_attributes return [] unless relation_query?(self.class.query) self.class.query.send(:bound_attributes) end |
#build_arel(arel, base) ⇒ Object
Build the statement on the given arel and return the WITH statement
205 206 207 208 209 210 211 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 205 def build_arel(arel, base) # Build the join for this statement arel.join(table, arel_join).on(*join_columns) # Return the subquery for this statement ::Arel::Nodes::As.new(table, mount_query) end |
#columns ⇒ Object
Get the columns that will be selected for this statement
200 201 202 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 200 def columns exposed_attributes + @select.values.map(&method(:project)) end |
#ensure_dependencies!(base) ⇒ Object
Ensure that all the dependencies are loaded in the base relation
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 220 def ensure_dependencies!(base) requires.each do |dependent| dependent_klass = base.model.auxiliary_statements_list[dependent] next if base.auxiliary_statements_values.any? do |cte| cte.is_a?(dependent_klass) end instance = AuxiliaryStatement.instantiate(dependent, base) instance.ensure_dependencies!(base) base.auxiliary_statements_values += [instance] end end |