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.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

  • bind

  • create_with

  • distinct

  • eager_load

  • X

    except ? - is this not defined in the interface?

  • extending

  • from

  • X

    group

  • ~~having~~ - NO

  • X

    includes (partial)

  • joins

  • X

    limit

  • lock

  • .

    none

  • X

    offset

  • X

    order (partial)

  • preload

  • readonly

  • X

    references (partial)

  • X

    reorder

  • reverse_order

  • X

    select (partial)

  • X

    unscope

  • uniq

  • X

    where (partial)

  • where.not



45
46
47
48
49
# File 'lib/query_relation.rb', line 45

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.



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

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/query_relation.rb', line 16

def options
  @options
end

Instance Method Details

#allObject



148
149
150
# File 'lib/query_relation.rb', line 148

def all
  self
end

#count(*_args) ⇒ Object

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



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

def count(*_args)
  to_a.size
end

#except(*val) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/query_relation.rb', line 114

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

#firstObject

TODO: support arguments



161
162
163
# File 'lib/query_relation.rb', line 161

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

#group(*args) ⇒ Object



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

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

#includes(*args) ⇒ Object



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

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

#instances_are_derived?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/query_relation.rb', line 170

def instances_are_derived?
  true
end

#lastObject

TODO: support arguments



166
167
168
# File 'lib/query_relation.rb', line 166

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

#limit(val) ⇒ Object



83
84
85
# File 'lib/query_relation.rb', line 83

def limit(val)
  assign_arg :limit, val
end

#limit_valueObject



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

def limit_value
  options[:limit]
end

#offset(val) ⇒ Object



131
132
133
# File 'lib/query_relation.rb', line 131

def offset(val)
  assign_arg :offset, val
end

#offset_valueObject



135
136
137
# File 'lib/query_relation.rb', line 135

def offset_value
  options[:offset]
end

#order(*args) ⇒ Object



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

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

#order_valuesObject



95
96
97
# File 'lib/query_relation.rb', line 95

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

#references(*args) ⇒ Object



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

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

#reorder(*val) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/query_relation.rb', line 103

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

  dup.tap do |r|
    r.options[:order] = val
  end
end

#select(*args) ⇒ Object

Parameters:

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


140
141
142
# File 'lib/query_relation.rb', line 140

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

#to_aObject



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

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

#unscope(*val) ⇒ Object

similar to except. difference being this persists across merges



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

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

#where(*val) ⇒ Object



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

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.blank?
      # nop
    elsif old_where.blank?
      r.options[:where] = val
    elsif old_where.kind_of?(Hash) && val.kind_of?(Hash)
      val.each_pair do |key, value|
        old_where[key] = if old_where[key]
                           Array.wrap(old_where[key]) + Array.wrap(value)
                         else
                           value
                         end
      end
    else
      raise ArgumentError,
            "Need to support #{__callee__}(#{val.class.name}) with existing #{old_where.class.name}"
    end
  end
end