Class: QueryRelation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/query_relation.rb,
lib/query_relation/version.rb,
lib/query_relation/queryable.rb

Defined Under Namespace

Modules: Queryable

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = nil, &block) ⇒ QueryRelation

  • bind

  • create_with

  • distinct

  • X

    eager_load (partial - leveraging references)

  • X

    except ? - is this not defined in the interface?

  • extending

  • from

  • X

    group

  • ~~having~~ - NO

  • X

    includes (partial)

  • X

    joins (partial - references includes)

  • left_joins

  • left_outer_joins

  • X

    limit

  • lock

  • .

    none

  • X

    offset

  • X

    order (partial)

  • X

    preload (partial - leveraging includes)

  • readonly

  • X

    references (partial)

  • X

    reorder

  • reverse_order

  • X

    select (partial)

  • X

    unscope

  • uniq

  • X

    where (partial)

  • where.not



41
42
43
44
45
# File 'lib/query_relation.rb', line 41

def initialize(model, opts = nil, &block)
  @klass   = model
  @options = opts ? opts.dup : {}
  @target = block || ->(*args) { klass.send(:search, *args) }
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/query_relation.rb', line 9

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/query_relation.rb', line 10

def options
  @options
end

Instance Method Details

#allObject



185
186
187
# File 'lib/query_relation.rb', line 185

def all
  self
end

#blank?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/query_relation.rb', line 201

def blank?
  to_a.nil? || to_a.empty?
end

#count(*_args) ⇒ Object

count(:all) is very common but [1, 2, 3].count(:all) == 0



191
192
193
# File 'lib/query_relation.rb', line 191

def count(*_args)
  to_a.size
end

#except(*val) ⇒ Object

Parameters:

  • val (Array<Symbol>, Symbol)

    attributes to remove from the query



135
136
137
138
139
140
141
# File 'lib/query_relation.rb', line 135

def except(*val)
  dup.tap do |r|
    val.flatten.compact.each do |key|
      r.options.delete(key)
    end
  end
end

#firstObject

TODO: support arguments



210
211
212
# File 'lib/query_relation.rb', line 210

def first
  defined?(@results) ? @results.first : call_query_method(:first)
end

#group(*columns) ⇒ Object

Parameters:

  • columns (Array<Symbol>, Symbol)

    columns for order, replacing original



116
117
118
# File 'lib/query_relation.rb', line 116

def group(*columns)
  append_hash_arg :group, *columns
end

#includes(*args) ⇒ Object Also known as: preload



76
77
78
# File 'lib/query_relation.rb', line 76

def includes(*args)
  append_hash_array_arg :includes, {}, *args
end

#includes_valuesObject Also known as: preload_values



80
81
82
# File 'lib/query_relation.rb', line 80

def includes_values
  options[:includes] || []
end

#instances_are_derived?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/query_relation.rb', line 219

def instances_are_derived?
  true
end

#lastObject

TODO: support arguments



215
216
217
# File 'lib/query_relation.rb', line 215

def last
  defined?(@results) ? @results.last : call_query_method(:last)
end

#limit(val) ⇒ Object

Parameters:

  • val (Integer)

    maximum number of rows to bring back



98
99
100
# File 'lib/query_relation.rb', line 98

def limit(val)
  assign_arg :limit, val
end

#limit_valueObject



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

def limit_value
  options[:limit]
end

#offset(val) ⇒ Object

Parameters:

  • val (Integer)

    offset



154
155
156
# File 'lib/query_relation.rb', line 154

def offset(val)
  assign_arg :offset, val
end

#offset_valueObject



158
159
160
# File 'lib/query_relation.rb', line 158

def offset_value
  options[:offset]
end

#order(*columns) ⇒ Object

Parameters:

  • columns (Array<Symbol>, Symbol)

    columns for order



107
108
109
# File 'lib/query_relation.rb', line 107

def order(*columns)
  append_hash_array_arg :order, "ASC", *columns
end

#order_valuesObject



111
112
113
# File 'lib/query_relation.rb', line 111

def order_values
  options[:order] || []
end

#pluck(*columns) ⇒ Object

Parameters:

  • columns (Array<Sting,Symbol>, String, Symbol)

    columns to bring back



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/query_relation.rb', line 168

def pluck(*columns)
  columns = columns.flatten.compact

  val = assign_arg(:select, columns).to_a

  if columns.size == 1
    column = columns.first
    val.map { |row| row[column] }
  else
    val.map { |row| columns.map { |col| row[col] } }
  end
end

#presenceObject



197
198
199
# File 'lib/query_relation.rb', line 197

def presence
  to_a if present?
end

#present?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/query_relation.rb', line 205

def present?
  !blank?
end

#references(*args) ⇒ Object Also known as: joins, eager_load



87
88
89
# File 'lib/query_relation.rb', line 87

def references(*args)
  append_hash_array_arg :references, {}, *args
end

#references_valuesObject Also known as: eager_load_values



91
92
93
# File 'lib/query_relation.rb', line 91

def references_values
  options[:references] || []
end

#reorder(*columns) ⇒ Object

Parameters:

  • columns (Array<Symbol>, Symbol)

    columns for order, replacing original



121
122
123
124
125
126
127
128
# File 'lib/query_relation.rb', line 121

def reorder(*columns)
  columns = columns.flatten.compact
  if columns.first.kind_of?(Hash)
    raise ArgumentError, "Need to support #{__callee__}(#{columns.class.name})"
  end

  assign_arg(:order, columns)
end

#select(*columns) ⇒ Object

Parameters:

  • columns (Array<Sting, Symbol>, String, Symbol)

    columns to bring back



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

def select(*columns)
  append_hash_arg :select, *columns
end

#to_aObject



181
182
183
# File 'lib/query_relation.rb', line 181

def to_a
  @results ||= call_query_method(:all)
end

#unscope(*val) ⇒ Object

similar to except. difference being this persists across merges

Parameters:

  • val (Array<Symbol>, Symbol)

    attributes to remove from the query



145
146
147
148
149
150
151
# File 'lib/query_relation.rb', line 145

def unscope(*val)
  dup.tap do |r|
    val.flatten.compact.each do |key|
      r.options[key] = nil
    end
  end
end

#where(*val) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/query_relation.rb', line 47

def where(*val)
  val = val.flatten.compact
  val = val.first if val.size == 1 && val.first.kind_of?(Hash)
  dup.tap do |r|
    old_where = r.options[:where]
    if val.nil? || val.empty?
      # nop
    elsif old_where.nil? || old_where.empty?
      r.options[:where] = val
    elsif old_where.kind_of?(Hash) && val.kind_of?(Hash)
      old_where = r.options[:where] = r.options[:where].dup
      val.each_pair do |key, value|
        old_where[key] = if old_where[key]
                           Array(old_where[key]) + Array(value)
                         else
                           value
                         end
      end
    else
      raise ArgumentError,
            "Need to support #{__callee__}(#{val.class.name}) with existing #{old_where.class.name}"
    end
  end
end

#where_valuesObject



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

def where_values
  options[:where]
end