Class: Torque::PostgreSQL::AuxiliaryStatement

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AuxiliaryStatement

Start a new auxiliary statement giving extra options



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 112

def initialize(*args)
  options = args.extract_options!
  args_key = Torque::PostgreSQL.config.auxiliary_statement.send_arguments_key

  @join = options.fetch(:join, {})
  @args = options.fetch(args_key, {})
  @where = options.fetch(:where, {})
  @select = options.fetch(:select, {})
  @join_type = options.fetch(:join_type, nil)

  @bound_attributes = []
  @join_sources = []
end

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 9

def config
  @config
end

Instance Attribute Details

#bound_attributesObject (readonly)

Returns the value of attribute bound_attributes.



109
110
111
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 109

def bound_attributes
  @bound_attributes
end

#join_sourcesObject (readonly)

Returns the value of attribute join_sources.



109
110
111
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 109

def join_sources
  @join_sources
end

Class Method Details

.arel_query?(obj) ⇒ Boolean

Identify if the query set may be used as arel

Returns:

  • (Boolean)


50
51
52
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 50

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



34
35
36
37
38
39
40
41
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 34

def build(statement, base, options = nil, bound_attributes = [], join_sources = [])
  klass = instantiate(statement, base, options)
  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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 72

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



87
88
89
90
91
92
93
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 87

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 56

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

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 19

def instantiate(statement, base, options = 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(options) 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



12
13
14
15
16
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 12

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

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 44

def relation_query?(obj)
  !obj.nil? && obj.respond_to?(:ancestors) && \
    obj.ancestors.include?(ActiveRecord::Base)
end

.tableObject

Get the arel version of the statement table



96
97
98
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 96

def table
  @table ||= ::Arel::Table.new(table_name)
end

.table_nameObject

Get the name of the table of the configurated statement



101
102
103
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 101

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



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/torque/postgresql/auxiliary_statement.rb', line 127

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