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
241
242
243
244
245
246
247
248
249
|
# File 'lib/frozen_record/scope.rb', line 241
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
#==(other) ⇒ Object
132
133
134
135
|
# File 'lib/frozen_record/scope.rb', line 132
def ==(other)
self.class === other &&
comparable_attributes == other.comparable_attributes
end
|
#average(attribute) ⇒ Object
84
85
86
|
# File 'lib/frozen_record/scope.rb', line 84
def average(attribute)
pluck(attribute).sum.to_f / count
end
|
#exists? ⇒ Boolean
96
97
98
|
# File 'lib/frozen_record/scope.rb', line 96
def exists?
!empty?
end
|
#find(id) ⇒ Object
40
41
42
43
|
# File 'lib/frozen_record/scope.rb', line 40
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
45
46
47
|
# File 'lib/frozen_record/scope.rb', line 45
def find_by(criterias)
where(criterias).first
end
|
#find_by!(criterias) ⇒ Object
49
50
51
|
# File 'lib/frozen_record/scope.rb', line 49
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)
matching_records.find { |r| r.id == id }
end
|
#first! ⇒ Object
53
54
55
|
# File 'lib/frozen_record/scope.rb', line 53
def first!
first or raise RecordNotFound, "No record matched"
end
|
#hash ⇒ Object
128
129
130
|
# File 'lib/frozen_record/scope.rb', line 128
def hash
comparable_attributes.hash
end
|
#ids ⇒ Object
76
77
78
|
# File 'lib/frozen_record/scope.rb', line 76
def ids
pluck(primary_key)
end
|
#last! ⇒ Object
57
58
59
|
# File 'lib/frozen_record/scope.rb', line 57
def last!
last or raise RecordNotFound, "No record matched"
end
|
#limit(amount) ⇒ Object
116
117
118
|
# File 'lib/frozen_record/scope.rb', line 116
def limit(amount)
spawn.limit!(amount)
end
|
#maximum(attribute) ⇒ Object
92
93
94
|
# File 'lib/frozen_record/scope.rb', line 92
def maximum(attribute)
pluck(attribute).max
end
|
#minimum(attribute) ⇒ Object
88
89
90
|
# File 'lib/frozen_record/scope.rb', line 88
def minimum(attribute)
pluck(attribute).min
end
|
#offset(amount) ⇒ Object
120
121
122
|
# File 'lib/frozen_record/scope.rb', line 120
def offset(amount)
spawn.offset!(amount)
end
|
#order(*ordering) ⇒ Object
112
113
114
|
# File 'lib/frozen_record/scope.rb', line 112
def order(*ordering)
spawn.order!(*ordering)
end
|
#pluck(*attributes) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/frozen_record/scope.rb', line 65
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
124
125
126
|
# File 'lib/frozen_record/scope.rb', line 124
def respond_to_missing?(method_name, *)
array_delegable?(method_name) || @klass.respond_to?(method_name) || super
end
|
#sum(attribute) ⇒ Object
80
81
82
|
# File 'lib/frozen_record/scope.rb', line 80
def sum(attribute)
pluck(attribute).sum
end
|
#to_a ⇒ Object
61
62
63
|
# File 'lib/frozen_record/scope.rb', line 61
def to_a
query_results
end
|
#where(criterias = :chain) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/frozen_record/scope.rb', line 100
def where(criterias = :chain)
if criterias == :chain
WhereChain.new(self)
else
spawn.where!(criterias)
end
end
|
#where_not(criterias) ⇒ Object
108
109
110
|
# File 'lib/frozen_record/scope.rb', line 108
def where_not(criterias)
spawn.where_not!(criterias)
end
|