Class: Torque::PostgreSQL::AuxiliaryStatement::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/auxiliary_statement/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, source, recursive = false) ⇒ Settings

Returns a new instance of Settings.



17
18
19
20
21
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 17

def initialize(base, source, recursive = false)
  @base = base
  @source = source
  @recursive = recursive
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#depthObject (readonly)

Returns the value of attribute depth.



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

def depth
  @depth
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#sourceObject (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_nameObject



23
24
25
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 23

def base_name
  @base.name
end

#base_tableObject



27
28
29
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 27

def base_table
  @base.arel_table
end

#col(name) ⇒ Object Also known as: column

Grant an easy access to arel table columns



71
72
73
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 71

def col(name)
  query_table[name.to_s]
end

#connect(value = nil) ⇒ Object Also known as: connect=

Assume ‘parent_` as the other part if provided a Symbol or String



96
97
98
99
100
101
102
103
104
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 96

def connect(value = nil)
  return @connect if value.nil?

  value = [value.to_sym, :"parent_#{value}"] \
    if value.is_a?(String) || value.is_a?(Symbol)
  value = value.to_a.first if value.is_a?(Hash)

  @connect = value
end

#depth?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 35

def depth?
  defined?(@depth)
end

#path?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 39

def path?
  defined?(@path)
end

#query(value = nil, command = nil) ⇒ Object

There are three ways of setting the query:

  • A simple relation based on a Model

  • A Arel-based select manager

  • A string or a proc



81
82
83
84
85
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 81

def query(value = nil, command = nil)
  return @query if value.nil?

  @query = sanitize_query(value, command)
end

#query_tableObject

Get the arel version of the table set on the query

Raises:

  • (StandardError)


64
65
66
67
68
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 64

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

#recursive?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 31

def recursive?
  @recursive
end

#sub_query(value = nil, command = nil) ⇒ Object

Same as query, but for the second part of the union for recursive cte



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

def sub_query(value = nil, command = nil)
  return unless recursive?
  return @sub_query if value.nil?

  @sub_query = sanitize_query(value, command)
end

#union_all!Object

Set recursive operation to use union all



54
55
56
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 54

def union_all!
  @union_all = true if recursive?
end

#with_depth(name = 'depth', start: 0, as: nil) ⇒ Object

Add an attribute to the result showing the depth of each iteration



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

def with_depth(name = 'depth', start: 0, as: nil)
  @depth = [name.to_s, start, as&.to_s] if recursive?
end

#with_depth_and_pathObject

Add both depth and path to the result



59
60
61
# File 'lib/torque/postgresql/auxiliary_statement/settings.rb', line 59

def with_depth_and_path
  with_depth && with_path
end

#with_path(name = 'path', source: nil, as: nil) ⇒ Object

Add an attribute to the result showing the path of each record



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

def with_path(name = 'path', source: nil, as: nil)
  @path = [name.to_s, source&.to_s, as&.to_s] if recursive?
end