Class: FrozenRecord::Scope
- Inherits:
-
Object
- Object
- FrozenRecord::Scope
show all
- Defined in:
- lib/frozen_record/scope.rb
Defined Under Namespace
Classes: WhereChain
Constant Summary
collapse
- BLACKLISTED_ARRAY_METHODS =
[
:compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
:keep_if, :pop, :shift, :delete_at, :compact
].to_set
Instance Method Summary
collapse
Constructor Details
#initialize(klass) ⇒ Scope
Returns a new instance of Scope.
24
25
26
27
28
29
30
31
|
# File 'lib/frozen_record/scope.rb', line 24
def initialize(klass)
@klass = klass
@where_values = []
@where_not_values = []
@order_values = []
@limit = nil
@offset = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/frozen_record/scope.rb', line 187
def method_missing(method_name, *args, &block)
if array_delegable?(method_name)
to_a.public_send(method_name, *args, &block)
elsif @klass.respond_to?(method_name)
delegate_to_class(method_name, *args, &block)
else
super
end
end
|
Instance Method Details
#average(attribute) ⇒ Object
81
82
83
|
# File 'lib/frozen_record/scope.rb', line 81
def average(attribute)
pluck(attribute).sum.to_f / count
end
|
#exists? ⇒ Boolean
93
94
95
|
# File 'lib/frozen_record/scope.rb', line 93
def exists?
!empty?
end
|
#find(id) ⇒ Object
37
38
39
40
|
# File 'lib/frozen_record/scope.rb', line 37
def find(id)
raise RecordNotFound, "Can't lookup record without ID" unless id
find_by_id(id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
end
|
#find_by(criterias) ⇒ Object
42
43
44
|
# File 'lib/frozen_record/scope.rb', line 42
def find_by(criterias)
where(criterias).first
end
|
#find_by!(criterias) ⇒ Object
46
47
48
|
# File 'lib/frozen_record/scope.rb', line 46
def find_by!(criterias)
where(criterias).first!
end
|
#find_by_id(id) ⇒ Object
33
34
35
|
# File 'lib/frozen_record/scope.rb', line 33
def find_by_id(id)
matching_records.find { |r| r.id == id }
end
|
#first! ⇒ Object
50
51
52
|
# File 'lib/frozen_record/scope.rb', line 50
def first!
first or raise RecordNotFound, "No record matched"
end
|
#ids ⇒ Object
73
74
75
|
# File 'lib/frozen_record/scope.rb', line 73
def ids
pluck(primary_key)
end
|
#last! ⇒ Object
54
55
56
|
# File 'lib/frozen_record/scope.rb', line 54
def last!
last or raise RecordNotFound, "No record matched"
end
|
#limit(amount) ⇒ Object
113
114
115
|
# File 'lib/frozen_record/scope.rb', line 113
def limit(amount)
spawn.limit!(amount)
end
|
#maximum(attribute) ⇒ Object
89
90
91
|
# File 'lib/frozen_record/scope.rb', line 89
def maximum(attribute)
pluck(attribute).max
end
|
#minimum(attribute) ⇒ Object
85
86
87
|
# File 'lib/frozen_record/scope.rb', line 85
def minimum(attribute)
pluck(attribute).min
end
|
#offset(amount) ⇒ Object
117
118
119
|
# File 'lib/frozen_record/scope.rb', line 117
def offset(amount)
spawn.offset!(amount)
end
|
#order(*ordering) ⇒ Object
109
110
111
|
# File 'lib/frozen_record/scope.rb', line 109
def order(*ordering)
spawn.order!(*ordering)
end
|
#pluck(*attributes) ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/frozen_record/scope.rb', line 62
def pluck(*attributes)
case attributes.length
when 1
to_a.map(&attributes.first)
when 0
raise NotImplementedError, '`.pluck` without arguments is not supported yet'
else
to_a.map { |r| attributes.map { |a| r[a] }}
end
end
|
#respond_to_missing?(method_name) ⇒ Boolean
121
122
123
|
# File 'lib/frozen_record/scope.rb', line 121
def respond_to_missing?(method_name, *)
array_delegable?(method_name) || @klass.respond_to?(method_name) || super
end
|
#sum(attribute) ⇒ Object
77
78
79
|
# File 'lib/frozen_record/scope.rb', line 77
def sum(attribute)
pluck(attribute).sum
end
|
#to_a ⇒ Object
58
59
60
|
# File 'lib/frozen_record/scope.rb', line 58
def to_a
query_results
end
|
#where(criterias = :chain) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/frozen_record/scope.rb', line 97
def where(criterias = :chain)
if criterias == :chain
WhereChain.new(self)
else
spawn.where!(criterias)
end
end
|
#where_not(criterias) ⇒ Object
105
106
107
|
# File 'lib/frozen_record/scope.rb', line 105
def where_not(criterias)
spawn.where_not!(criterias)
end
|