Class: Quo::RelationBackedQuerySpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/quo/relation_backed_query_specification.rb,
sig/generated/quo/relation_backed_query_specification.rbs

Overview

RelationBackedQuerySpecification encapsulates all the options for building a SQL query This separates the storage of query options from the actual query construction and provides a cleaner interface for RelationBackedQuery

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Object

Parameters:

  • options (Hash[Symbol, untyped]) (defaults to: {})


15
16
17
# File 'lib/quo/relation_backed_query_specification.rb', line 15

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

@rbs! @options: Hash[Symbol, untyped]

Returns:

  • (Object)


12
13
14
# File 'lib/quo/relation_backed_query_specification.rb', line 12

def options
  @options
end

Class Method Details

.blankQuo::RelationBackedQuerySpecification

Returns a blank specification



169
170
171
# File 'lib/quo/relation_backed_query_specification.rb', line 169

def self.blank
  @blank ||= new
end

.build(options = {}) ⇒ Quo::RelationBackedQuerySpecification

Builds a new specification from a hash of options

Parameters:

  • options (Hash[Symbol, untyped]) (defaults to: {})

Returns:



163
164
165
# File 'lib/quo/relation_backed_query_specification.rb', line 163

def self.build(options = {})
  new(options)
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (Symbol)

Returns:

  • (Object)


64
65
66
# File 'lib/quo/relation_backed_query_specification.rb', line 64

def [](key)
  options[key]
end

#apply_to(relation) ⇒ ActiveRecord::Relation

Apply all the specification options to the given ActiveRecord relation

Parameters:

  • relation (ActiveRecord::Relation)

Returns:

  • (ActiveRecord::Relation)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/quo/relation_backed_query_specification.rb', line 30

def apply_to(relation)
  rel = relation
  rel = rel.select(*options[:select]) if options[:select]
  rel = rel.where(options[:where]) if options[:where]
  rel = rel.order(options[:order]) if options[:order]
  rel = rel.group(*options[:group]) if options[:group]
  rel = rel.limit(options[:limit]) if options[:limit]
  rel = rel.offset(options[:offset]) if options[:offset]
  if (joins = options[:joins])
    rel = joins.is_a?(Array) ? rel.joins(*joins) : rel.joins(joins)
  end
  if (left_outer_joins = options[:left_outer_joins])
    rel = left_outer_joins.is_a?(Array) ? rel.left_outer_joins(*left_outer_joins) : rel.left_outer_joins(left_outer_joins)
  end
  rel = rel.includes(*options[:includes]) if options[:includes]
  rel = rel.preload(*options[:preload]) if options[:preload]
  rel = rel.eager_load(*options[:eager_load]) if options[:eager_load]
  rel = rel.distinct if options[:distinct]
  rel = rel.reorder(options[:reorder]) if options[:reorder]
  rel = rel.extending(*options[:extending]) if options[:extending]
  rel = rel.unscope(options[:unscope]) if options[:unscope]
  rel
end

#distinct(enabled = true) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • enabled (Boolean) (defaults to: true)

Returns:



138
139
140
# File 'lib/quo/relation_backed_query_specification.rb', line 138

def distinct(enabled = true)
  merge(distinct: enabled)
end

#eager_load(*associations) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • associations (Object)

Returns:



132
133
134
# File 'lib/quo/relation_backed_query_specification.rb', line 132

def eager_load(*associations)
  merge(eager_load: associations)
end

#extending(*modules) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • modules (Object)

Returns:



150
151
152
# File 'lib/quo/relation_backed_query_specification.rb', line 150

def extending(*modules)
  merge(extending: modules)
end

#group(*columns) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • columns (Object)

Returns:



90
91
92
# File 'lib/quo/relation_backed_query_specification.rb', line 90

def group(*columns)
  merge(group: columns)
end

#has?(key) ⇒ Boolean

Parameters:

  • key (Symbol)

Returns:

  • (Boolean)


58
59
60
# File 'lib/quo/relation_backed_query_specification.rb', line 58

def has?(key)
  options.key?(key)
end

#includes(*associations) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • associations (Object)

Returns:



120
121
122
# File 'lib/quo/relation_backed_query_specification.rb', line 120

def includes(*associations)
  merge(includes: associations)
end

#joins(*tables) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • tables (Object)

Returns:



108
109
110
# File 'lib/quo/relation_backed_query_specification.rb', line 108

def joins(*tables)
  merge(joins: tables)
end

#left_outer_joins(*tables) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • tables (Object)

Returns:



114
115
116
# File 'lib/quo/relation_backed_query_specification.rb', line 114

def left_outer_joins(*tables)
  merge(left_outer_joins: tables)
end

#limit(value) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • value (Integer)

Returns:



96
97
98
# File 'lib/quo/relation_backed_query_specification.rb', line 96

def limit(value)
  merge(limit: value)
end

#merge(new_options) ⇒ Quo::RelationBackedQuerySpecification

Creates a new specification with merged options

Parameters:

Returns:



22
23
24
25
# File 'lib/quo/relation_backed_query_specification.rb', line 22

def merge(new_options)
  new_options = new_options.options if new_options.is_a?(self.class)
  self.class.new(options.merge(new_options))
end

#offset(value) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • value (Integer)

Returns:



102
103
104
# File 'lib/quo/relation_backed_query_specification.rb', line 102

def offset(value)
  merge(offset: value)
end

#order(order_clause) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • order_clause (Object)

Returns:



84
85
86
# File 'lib/quo/relation_backed_query_specification.rb', line 84

def order(order_clause)
  merge(order: order_clause)
end

#preload(*associations) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • associations (Object)

Returns:



126
127
128
# File 'lib/quo/relation_backed_query_specification.rb', line 126

def preload(*associations)
  merge(preload: associations)
end

#reorder(order_clause) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • order_clause (Object)

Returns:



144
145
146
# File 'lib/quo/relation_backed_query_specification.rb', line 144

def reorder(order_clause)
  merge(reorder: order_clause)
end

#select(*fields) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • fields (Object)

Returns:



72
73
74
# File 'lib/quo/relation_backed_query_specification.rb', line 72

def select(*fields)
  merge(select: fields)
end

#unscope(*args) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • args (Object)

Returns:



156
157
158
# File 'lib/quo/relation_backed_query_specification.rb', line 156

def unscope(*args)
  merge(unscope: args)
end

#where(conditions) ⇒ Quo::RelationBackedQuerySpecification

Parameters:

  • conditions (Object)

Returns:



78
79
80
# File 'lib/quo/relation_backed_query_specification.rb', line 78

def where(conditions)
  merge(where: conditions)
end