Class: QueryRelation
- Inherits:
-
Object
- Object
- QueryRelation
- 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
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #all ⇒ Object
- #blank? ⇒ Boolean
-
#count(*_args) ⇒ Object
count(:all) is very common but [1, 2, 3].count(:all) == 0.
- #except(*val) ⇒ Object
-
#first ⇒ Object
TODO: support arguments.
- #group(*columns) ⇒ Object
- #includes(*args) ⇒ Object (also: #preload)
- #includes_values ⇒ Object (also: #preload_values)
-
#initialize(model, opts = nil, &block) ⇒ QueryRelation
constructor
-
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.
- #instances_are_derived? ⇒ Boolean
-
#last ⇒ Object
TODO: support arguments.
- #limit(val) ⇒ Object
- #limit_value ⇒ Object
- #offset(val) ⇒ Object
- #offset_value ⇒ Object
- #order(*columns) ⇒ Object
- #order_values ⇒ Object
- #pluck(*columns) ⇒ Object
- #presence ⇒ Object
- #present? ⇒ Boolean
- #references(*args) ⇒ Object (also: #joins, #eager_load)
- #references_values ⇒ Object (also: #eager_load_values)
- #reorder(*columns) ⇒ Object
- #select(*columns) ⇒ Object
- #to_a ⇒ Object
-
#unscope(*val) ⇒ Object
similar to except.
- #where(*val) ⇒ Object
- #where_values ⇒ Object
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
9 10 11 |
# File 'lib/query_relation.rb', line 9 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/query_relation.rb', line 10 def @options end |
Instance Method Details
#all ⇒ Object
185 186 187 |
# File 'lib/query_relation.rb', line 185 def all self end |
#blank? ⇒ 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
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..delete(key) end end end |
#first ⇒ Object
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
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_values ⇒ Object Also known as: preload_values
80 81 82 |
# File 'lib/query_relation.rb', line 80 def includes_values [:includes] || [] end |
#instances_are_derived? ⇒ Boolean
219 220 221 |
# File 'lib/query_relation.rb', line 219 def instances_are_derived? true end |
#last ⇒ Object
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
98 99 100 |
# File 'lib/query_relation.rb', line 98 def limit(val) assign_arg :limit, val end |
#limit_value ⇒ Object
102 103 104 |
# File 'lib/query_relation.rb', line 102 def limit_value [:limit] end |
#offset(val) ⇒ Object
154 155 156 |
# File 'lib/query_relation.rb', line 154 def offset(val) assign_arg :offset, val end |
#offset_value ⇒ Object
158 159 160 |
# File 'lib/query_relation.rb', line 158 def offset_value [:offset] end |
#order(*columns) ⇒ Object
107 108 109 |
# File 'lib/query_relation.rb', line 107 def order(*columns) append_hash_array_arg :order, "ASC", *columns end |
#order_values ⇒ Object
111 112 113 |
# File 'lib/query_relation.rb', line 111 def order_values [:order] || [] end |
#pluck(*columns) ⇒ Object
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 |
#presence ⇒ Object
197 198 199 |
# File 'lib/query_relation.rb', line 197 def presence to_a if present? end |
#present? ⇒ 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_values ⇒ Object Also known as: eager_load_values
91 92 93 |
# File 'lib/query_relation.rb', line 91 def references_values [:references] || [] end |
#reorder(*columns) ⇒ Object
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
163 164 165 |
# File 'lib/query_relation.rb', line 163 def select(*columns) append_hash_arg :select, *columns end |
#to_a ⇒ Object
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
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.[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.[:where] if val.nil? || val.empty? # nop elsif old_where.nil? || old_where.empty? r.[:where] = val elsif old_where.kind_of?(Hash) && val.kind_of?(Hash) old_where = r.[:where] = r.[: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_values ⇒ Object
72 73 74 |
# File 'lib/query_relation.rb', line 72 def where_values [:where] end |