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.
9
10
11
12
|
# File 'lib/ardb/relation_spy.rb', line 9
def initialize
@applied, @results = [], []
@offset_value, @limit_value = nil, nil
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
|
#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
|
#results ⇒ Object
Returns the value of attribute results.
7
8
9
|
# File 'lib/ardb/relation_spy.rb', line 7
def results
@results
end
|
Instance Method Details
#==(other) ⇒ Object
20
21
22
|
# File 'lib/ardb/relation_spy.rb', line 20
def ==(other)
other.kind_of?(self.class) ? @applied == other.applied : super
end
|
#all ⇒ Object
121
122
123
|
# File 'lib/ardb/relation_spy.rb', line 121
def all
@results[(@offset_value || 0), (@limit_value || @results.size)] || []
end
|
#count ⇒ Object
ActiveRecord::Calculations
127
128
129
|
# File 'lib/ardb/relation_spy.rb', line 127
def count
all.size
end
|
#except(*skips) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/ardb/relation_spy.rb', line 80
def except(*skips)
skips = skips.map(&:to_sym)
self.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
100
101
102
103
|
# File 'lib/ardb/relation_spy.rb', line 100
def find(id)
record = @results.detect{ |result| result.id == id }
record || raise(NotFoundError)
end
|
#first ⇒ Object
105
106
107
|
# File 'lib/ardb/relation_spy.rb', line 105
def first
self.all.first
end
|
#first! ⇒ Object
109
110
111
|
# File 'lib/ardb/relation_spy.rb', line 109
def first!
self.first || raise(NotFoundError)
end
|
#initialize_copy(copied_from) ⇒ Object
14
15
16
17
18
|
# File 'lib/ardb/relation_spy.rb', line 14
def initialize_copy(copied_from)
super
@applied = copied_from.applied.dup
@results = copied_from.results.dup
end
|
#last ⇒ Object
113
114
115
|
# File 'lib/ardb/relation_spy.rb', line 113
def last
self.all.last
end
|
#last! ⇒ Object
117
118
119
|
# File 'lib/ardb/relation_spy.rb', line 117
def last!
self.last || raise(NotFoundError)
end
|
#limit(value) ⇒ Object
56
57
58
59
60
|
# File 'lib/ardb/relation_spy.rb', line 56
def limit(value)
@limit_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:limit, [ value ])
self
end
|
#merge(other) ⇒ Object
ActiveRecord::SpawnMethods
70
71
72
73
74
75
76
77
78
|
# File 'lib/ardb/relation_spy.rb', line 70
def merge(other)
return self if self.equal?(other)
if other.kind_of?(self.class)
other.applied.each{ |a| self.send(a.type, *a.args) }
else
@applied << AppliedExpression.new(:merge, [ other ])
end
self
end
|
#offset(value) ⇒ Object
62
63
64
65
66
|
# File 'lib/ardb/relation_spy.rb', line 62
def offset(value)
@offset_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:offset, [ value ])
self
end
|
#only(*onlies) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/ardb/relation_spy.rb', line 89
def only(*onlies)
onlies = onlies.map(&:to_sym)
self.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
|
#reset! ⇒ Object
28
29
30
31
32
33
|
# File 'lib/ardb/relation_spy.rb', line 28
def reset!
@applied.clear
@results.clear
@offset_value = nil
@limit_value = nil
end
|
#to_sql ⇒ Object
24
25
26
|
# File 'lib/ardb/relation_spy.rb', line 24
def to_sql
@applied.map(&:to_sql).join(", ")
end
|