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
-
.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
184 185 186 187 188 189 190 191 192 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 184 def initialize(*args) = args. uses_key = Torque::PostgreSQL.config.auxiliary_statement.send_arguments_key @join = .fetch(:join, {}) @uses = .fetch(uses_key, []) @select = .fetch(:select, {}) @join_type = .fetch(:join_type, join_type) end |
Class Method Details
.base ⇒ Object
Get the base class associated to this statement
53 54 55 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 53 def base self.parent end |
.base_name ⇒ Object
Get the name of the base class
58 59 60 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 58 def base_name base.name end |
.base_table ⇒ Object
Get the arel table of the base class
73 74 75 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 73 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
47 48 49 50 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 47 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) if base.const_defined?(const) 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
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 83 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
78 79 80 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 78 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
63 64 65 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 63 def table @table ||= ::Arel::Table.new(table_name) end |
.table_name ⇒ Object
Get the name of the table of the configurated statement
68 69 70 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 68 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
209 210 211 212 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 209 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
200 201 202 203 204 205 206 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 200 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
195 196 197 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 195 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
215 216 217 218 219 220 221 222 223 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 215 def ensure_dependencies!(base) requires.each do |dependent| next if base.auxiliary_statements.key?(dependent) instance = AuxiliaryStatement.instantiate(dependent, base) instance.ensure_dependencies!(base) base.auxiliary_statements[dependent] = instance end end |