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 Attribute Summary collapse
-
.config ⇒ Object
readonly
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#bound_attributes ⇒ Object
readonly
Returns the value of attribute bound_attributes.
-
#join_sources ⇒ Object
readonly
Returns the value of attribute join_sources.
Class Method Summary collapse
-
.arel_query?(obj) ⇒ Boolean
Identify if the query set may be used as arel.
-
.build(statement, base, options = nil, bound_attributes = [], join_sources = []) ⇒ Object
Fast access to statement build.
-
.configurator(config) ⇒ Object
Set a configuration block or static hash.
-
.configure(base, instance) ⇒ Object
Run a configuration block or get the static configuration.
-
.create(table_or_settings, &block) ⇒ Object
A way to create auxiliary statements outside of models configurations, being able to use on extensions.
-
.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.
-
.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
-
#build(base) ⇒ Object
Build the statement on the given arel and return the WITH statement.
-
#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
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 114 def initialize(*args) = args. args_key = Torque::PostgreSQL.config.auxiliary_statement.send_arguments_key @join = .fetch(:join, {}) @args = .fetch(args_key, {}) @where = .fetch(:where, {}) @select = .fetch(:select, {}) @join_type = .fetch(:join_type, nil) @bound_attributes = [] @join_sources = [] end |
Class Attribute Details
.config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 11 def config @config end |
Instance Attribute Details
#bound_attributes ⇒ Object (readonly)
Returns the value of attribute bound_attributes.
111 112 113 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 111 def bound_attributes @bound_attributes end |
#join_sources ⇒ Object (readonly)
Returns the value of attribute join_sources.
111 112 113 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 111 def join_sources @join_sources end |
Class Method Details
.arel_query?(obj) ⇒ Boolean
Identify if the query set may be used as arel
52 53 54 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 52 def arel_query?(obj) !obj.nil? && obj.is_a?(::Arel::SelectManager) end |
.build(statement, base, options = nil, bound_attributes = [], join_sources = []) ⇒ Object
Fast access to statement build
36 37 38 39 40 41 42 43 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 36 def build(statement, base, = nil, bound_attributes = [], join_sources = []) klass = instantiate(statement, base, ) result = klass.build(base) bound_attributes.concat(klass.bound_attributes) join_sources.concat(klass.join_sources) result end |
.configurator(config) ⇒ Object
Set a configuration block or static hash
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 74 def configurator(config) if config.is_a?(Hash) # Map the aliases config[:attributes] = config.delete(:select) if config.key?(:select) # Create the struct that mocks a configuration result config = OpenStruct.new(config) table_name = config[:query]&.klass&.name&.underscore instance_variable_set(:@table_name, table_name) end @config = config end |
.configure(base, instance) ⇒ Object
Run a configuration block or get the static configuration
89 90 91 92 93 94 95 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 89 def configure(base, instance) return @config unless @config.respond_to?(:call) settings = Settings.new(base, instance) settings.instance_exec(settings, &@config) settings end |
.create(table_or_settings, &block) ⇒ Object
A way to create auxiliary statements outside of models configurations, being able to use on extensions
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 58 def create(table_or_settings, &block) klass = Class.new(AuxiliaryStatement) if block_given? klass.instance_variable_set(:@table_name, table_or_settings) klass.configurator(block) elsif relation_query?(table_or_settings) klass.configurator(query: table_or_settings) else klass.configurator(table_or_settings) end klass end |
.instantiate(statement, base, options = nil) ⇒ Object
Create a new instance of an auxiliary statement
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 21 def instantiate(statement, base, = nil) klass = while base < ActiveRecord::Base list = base.auxiliary_statements_list break list[statement] if list.present? && list.key?(statement) base = base.superclass end return klass.new() unless klass.nil? raise ArgumentError, <<-MSG.squish 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
14 15 16 17 18 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 14 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 |
.relation_query?(obj) ⇒ Boolean
Identify if the query set may be used as a relation
46 47 48 49 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 46 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
98 99 100 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 98 def table @table ||= ::Arel::Table.new(table_name) end |
.table_name ⇒ Object
Get the name of the table of the configurated statement
103 104 105 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 103 def table_name @table_name ||= self.name.demodulize.split('_').first.underscore end |
Instance Method Details
#build(base) ⇒ Object
Build the statement on the given arel and return the WITH statement
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 129 def build(base) @bound_attributes.clear @join_sources.clear # Prepare all the data for the statement prepare(base) # Add the join condition to the list @join_sources << build_join(base) # Return the statement with its dependencies [@dependencies, ::Arel::Nodes::As.new(table, build_query(base))] end |