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.1.1"
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
-
#count(*_args) ⇒ Object
count(:all) is very common but [1, 2, 3].count(:all) == 0.
- #except(*val) ⇒ Object
-
#first ⇒ Object
TODO: support arguments.
- #group(*args) ⇒ Object
- #includes(*args) ⇒ Object
-
#initialize(model, opts = nil, &block) ⇒ QueryRelation
constructor
-
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.
- #instances_are_derived? ⇒ Boolean
-
#last ⇒ Object
TODO: support arguments.
- #limit(val) ⇒ Object
- #limit_value ⇒ Object
- #offset(val) ⇒ Object
- #offset_value ⇒ Object
- #order(*args) ⇒ Object
- #order_values ⇒ Object
- #references(*args) ⇒ Object
- #reorder(*val) ⇒ Object
- #select(*args) ⇒ Object
- #to_a ⇒ Object
-
#unscope(*val) ⇒ Object
similar to except.
- #where(*val) ⇒ Object
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
15 16 17 |
# File 'lib/query_relation.rb', line 15 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/query_relation.rb', line 16 def @options end |
Instance Method Details
#all ⇒ Object
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..delete(key) end end end |
#first ⇒ Object
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
170 171 172 |
# File 'lib/query_relation.rb', line 170 def instances_are_derived? true end |
#last ⇒ Object
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_value ⇒ Object
87 88 89 |
# File 'lib/query_relation.rb', line 87 def limit_value [:limit] end |
#offset(val) ⇒ Object
131 132 133 |
# File 'lib/query_relation.rb', line 131 def offset(val) assign_arg :offset, val end |
#offset_value ⇒ Object
135 136 137 |
# File 'lib/query_relation.rb', line 135 def offset_value [: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_values ⇒ Object
95 96 97 |
# File 'lib/query_relation.rb', line 95 def order_values [: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.[:order] = val end end |
#select(*args) ⇒ Object
140 141 142 |
# File 'lib/query_relation.rb', line 140 def select(*args) append_hash_arg :select, *args end |
#to_a ⇒ Object
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.[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.[:where] if val.blank? # nop elsif old_where.blank? r.[: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 |