Class: FrozenRecord::Scope
- Inherits:
-
Object
- Object
- FrozenRecord::Scope
show all
- Defined in:
- lib/frozen_record/scope.rb
Defined Under Namespace
Classes: CoverMatcher, IncludeMatcher, Matcher, WhereChain
Constant Summary
collapse
- DISALLOWED_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.
27
28
29
30
31
32
33
34
|
# File 'lib/frozen_record/scope.rb', line 27
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
246
247
248
249
250
251
252
253
254
|
# File 'lib/frozen_record/scope.rb', line 246
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)
scoping { @klass.public_send(method_name, *args, &block) }
else
super
end
end
|
Instance Method Details
#==(other) ⇒ Object
137
138
139
140
|
# File 'lib/frozen_record/scope.rb', line 137
def ==(other)
self.class === other &&
comparable_attributes == other.comparable_attributes
end
|
#average(attribute) ⇒ Object
89
90
91
|
# File 'lib/frozen_record/scope.rb', line 89
def average(attribute)
pluck(attribute).sum.to_f / count
end
|
#exists? ⇒ Boolean
101
102
103
|
# File 'lib/frozen_record/scope.rb', line 101
def exists?
!empty?
end
|
#find(id) ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/frozen_record/scope.rb', line 40
def find(id)
raise RecordNotFound, "Can't lookup record without ID" unless id
scope = self
if @limit || @offset
scope = limit(nil).offset(nil)
end
scope.find_by_id(id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
end
|
#find_by(criterias) ⇒ Object
50
51
52
|
# File 'lib/frozen_record/scope.rb', line 50
def find_by(criterias)
where(criterias).first
end
|
#find_by!(criterias) ⇒ Object
54
55
56
|
# File 'lib/frozen_record/scope.rb', line 54
def find_by!(criterias)
where(criterias).first!
end
|
#find_by_id(id) ⇒ Object
36
37
38
|
# File 'lib/frozen_record/scope.rb', line 36
def find_by_id(id)
find_by(@klass.primary_key => id)
end
|
#first! ⇒ Object
58
59
60
|
# File 'lib/frozen_record/scope.rb', line 58
def first!
first or raise RecordNotFound, "No record matched"
end
|
#hash ⇒ Object
133
134
135
|
# File 'lib/frozen_record/scope.rb', line 133
def hash
comparable_attributes.hash
end
|
#ids ⇒ Object
81
82
83
|
# File 'lib/frozen_record/scope.rb', line 81
def ids
pluck(primary_key)
end
|
#last! ⇒ Object
62
63
64
|
# File 'lib/frozen_record/scope.rb', line 62
def last!
last or raise RecordNotFound, "No record matched"
end
|
#limit(amount) ⇒ Object
121
122
123
|
# File 'lib/frozen_record/scope.rb', line 121
def limit(amount)
spawn.limit!(amount)
end
|
#maximum(attribute) ⇒ Object
97
98
99
|
# File 'lib/frozen_record/scope.rb', line 97
def maximum(attribute)
pluck(attribute).max
end
|
#minimum(attribute) ⇒ Object
93
94
95
|
# File 'lib/frozen_record/scope.rb', line 93
def minimum(attribute)
pluck(attribute).min
end
|
#offset(amount) ⇒ Object
125
126
127
|
# File 'lib/frozen_record/scope.rb', line 125
def offset(amount)
spawn.offset!(amount)
end
|
#order(*ordering) ⇒ Object
117
118
119
|
# File 'lib/frozen_record/scope.rb', line 117
def order(*ordering)
spawn.order!(*ordering)
end
|
#pluck(*attributes) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/frozen_record/scope.rb', line 70
def pluck(*attributes)
case attributes.length
when 1
to_a.map(&attributes.first.to_sym)
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
129
130
131
|
# File 'lib/frozen_record/scope.rb', line 129
def respond_to_missing?(method_name, *)
array_delegable?(method_name) || @klass.respond_to?(method_name) || super
end
|
#sum(attribute) ⇒ Object
85
86
87
|
# File 'lib/frozen_record/scope.rb', line 85
def sum(attribute)
pluck(attribute).sum
end
|
#to_a ⇒ Object
66
67
68
|
# File 'lib/frozen_record/scope.rb', line 66
def to_a
query_results
end
|
#where(criterias = :chain) ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/frozen_record/scope.rb', line 105
def where(criterias = :chain)
if criterias == :chain
WhereChain.new(self)
else
spawn.where!(criterias)
end
end
|
#where_not(criterias) ⇒ Object
113
114
115
|
# File 'lib/frozen_record/scope.rb', line 113
def where_not(criterias)
spawn.where_not!(criterias)
end
|