Class: Ardb::RelationSpy
- Inherits:
-
Object
show all
- Defined in:
- lib/ardb/relation_spy.rb
Defined Under Namespace
Classes: AppliedExpression
Constant Summary
collapse
- NotFoundError =
Class.new(RuntimeError)
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of RelationSpy.
10
11
12
13
14
15
16
17
|
# File 'lib/ardb/relation_spy.rb', line 10
def initialize
@applied, @results = [], []
@offset_value, @limit_value = nil, nil
@pluck_values = {}
@maximum_values = {}
@minimum_values = {}
end
|
Instance Attribute Details
#applied ⇒ Object
Returns the value of attribute applied.
5
6
7
|
# File 'lib/ardb/relation_spy.rb', line 5
def applied
@applied
end
|
#limit_value ⇒ Object
Returns the value of attribute limit_value.
6
7
8
|
# File 'lib/ardb/relation_spy.rb', line 6
def limit_value
@limit_value
end
|
#maximum_values ⇒ Object
Returns the value of attribute maximum_values.
7
8
9
|
# File 'lib/ardb/relation_spy.rb', line 7
def maximum_values
@maximum_values
end
|
#minimum_values ⇒ Object
Returns the value of attribute minimum_values.
7
8
9
|
# File 'lib/ardb/relation_spy.rb', line 7
def minimum_values
@minimum_values
end
|
#offset_value ⇒ Object
Returns the value of attribute offset_value.
6
7
8
|
# File 'lib/ardb/relation_spy.rb', line 6
def offset_value
@offset_value
end
|
#pluck_values ⇒ Object
Returns the value of attribute pluck_values.
7
8
9
|
# File 'lib/ardb/relation_spy.rb', line 7
def pluck_values
@pluck_values
end
|
#results ⇒ Object
Returns the value of attribute results.
8
9
10
|
# File 'lib/ardb/relation_spy.rb', line 8
def results
@results
end
|
Instance Method Details
#==(other) ⇒ Object
25
26
27
|
# File 'lib/ardb/relation_spy.rb', line 25
def ==(other)
other.is_a?(self.class) ? @applied == other.applied : super
end
|
#all ⇒ Object
124
125
126
|
# File 'lib/ardb/relation_spy.rb', line 124
def all
@results[(@offset_value || 0), (@limit_value || @results.size)] || []
end
|
#count ⇒ Object
ActiveRecord::Calculations
130
131
132
|
# File 'lib/ardb/relation_spy.rb', line 130
def count
all.size
end
|
#except(*skips) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/ardb/relation_spy.rb', line 83
def except(*skips)
skips = skips.map(&:to_sym)
dup.tap do |r|
r.applied.reject!{ |a| skips.include?(a.type) }
r.limit_value = nil if skips.include?(:limit)
r.offset_value = nil if skips.include?(:offset)
end
end
|
#find(id) ⇒ Object
ActiveRecord::FinderMethods
103
104
105
106
|
# File 'lib/ardb/relation_spy.rb', line 103
def find(id)
record = @results.find{ |result| result.id == id }
record || raise(NotFoundError)
end
|
#first ⇒ Object
108
109
110
|
# File 'lib/ardb/relation_spy.rb', line 108
def first
all.first
end
|
#first! ⇒ Object
112
113
114
|
# File 'lib/ardb/relation_spy.rb', line 112
def first!
first || raise(NotFoundError)
end
|
#initialize_copy(copied_from) ⇒ Object
19
20
21
22
23
|
# File 'lib/ardb/relation_spy.rb', line 19
def initialize_copy(copied_from)
super
@applied = copied_from.applied.dup
@results = copied_from.results.dup
end
|
#last ⇒ Object
116
117
118
|
# File 'lib/ardb/relation_spy.rb', line 116
def last
all.last
end
|
#last! ⇒ Object
120
121
122
|
# File 'lib/ardb/relation_spy.rb', line 120
def last!
last || raise(NotFoundError)
end
|
#limit(value) ⇒ Object
59
60
61
62
63
|
# File 'lib/ardb/relation_spy.rb', line 59
def limit(value)
@limit_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:limit, [value])
self
end
|
#maximum(column_name) ⇒ Object
138
139
140
|
# File 'lib/ardb/relation_spy.rb', line 138
def maximum(column_name)
@maximum_values[column_name]
end
|
#merge(other) ⇒ Object
ActiveRecord::SpawnMethods
73
74
75
76
77
78
79
80
81
|
# File 'lib/ardb/relation_spy.rb', line 73
def merge(other)
return self if equal?(other)
if other.is_a?(self.class)
other.applied.each{ |a| send(a.type, *a.args) }
else
@applied << AppliedExpression.new(:merge, [other])
end
self
end
|
#minimum(column_name) ⇒ Object
142
143
144
|
# File 'lib/ardb/relation_spy.rb', line 142
def minimum(column_name)
@minimum_values[column_name]
end
|
#offset(value) ⇒ Object
65
66
67
68
69
|
# File 'lib/ardb/relation_spy.rb', line 65
def offset(value)
@offset_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:offset, [value])
self
end
|
#only(*onlies) ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/ardb/relation_spy.rb', line 92
def only(*onlies)
onlies = onlies.map(&:to_sym)
dup.tap do |r|
r.applied.reject!{ |a| !onlies.include?(a.type) }
r.limit_value = nil unless onlies.include?(:limit)
r.offset_value = nil unless onlies.include?(:offset)
end
end
|
#pluck(column_name) ⇒ Object
134
135
136
|
# File 'lib/ardb/relation_spy.rb', line 134
def pluck(column_name)
[@pluck_values[column_name]] * @results.size
end
|
#reset! ⇒ Object
33
34
35
36
37
38
|
# File 'lib/ardb/relation_spy.rb', line 33
def reset!
@applied.clear
@results.clear
@offset_value = nil
@limit_value = nil
end
|
#to_sql ⇒ Object
29
30
31
|
# File 'lib/ardb/relation_spy.rb', line 29
def to_sql
@applied.map(&:to_sql).join(", ")
end
|