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
14
15
16
|
# File 'lib/ardb/relation_spy.rb', line 14
def ==(other)
other.kind_of?(self.class) ? @applied == other.applied : super
end
|
#all ⇒ Object
98
99
100
|
# File 'lib/ardb/relation_spy.rb', line 98
def all
@results[(@offset_value || 0), (@limit_value || @results.size)] || []
end
|
#count ⇒ Object
ActiveRecord::Calculations
104
105
106
|
# File 'lib/ardb/relation_spy.rb', line 104
def count
all.size
end
|
#except(*skips) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/ardb/relation_spy.rb', line 59
def except(*skips)
skips = skips.map(&:to_sym)
@applied.reject!{ |a| skips.include?(a.type) }
@limit_value = nil if skips.include?(:limit)
@offset_value = nil if skips.include?(:offset)
self
end
|
#find(id) ⇒ Object
ActiveRecord::FinderMethods
77
78
79
80
|
# File 'lib/ardb/relation_spy.rb', line 77
def find(id)
record = @results.detect{ |result| result.id == id }
record || raise(NotFoundError)
end
|
#first ⇒ Object
82
83
84
|
# File 'lib/ardb/relation_spy.rb', line 82
def first
self.all.first
end
|
#first! ⇒ Object
86
87
88
|
# File 'lib/ardb/relation_spy.rb', line 86
def first!
self.first || raise(NotFoundError)
end
|
#last ⇒ Object
90
91
92
|
# File 'lib/ardb/relation_spy.rb', line 90
def last
self.all.last
end
|
#last! ⇒ Object
94
95
96
|
# File 'lib/ardb/relation_spy.rb', line 94
def last!
self.last || raise(NotFoundError)
end
|
#limit(value) ⇒ Object
35
36
37
38
39
|
# File 'lib/ardb/relation_spy.rb', line 35
def limit(value)
@limit_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:limit, [ value ])
self
end
|
#merge(other) ⇒ Object
ActiveRecord::SpawnMethods
49
50
51
52
53
54
55
56
57
|
# File 'lib/ardb/relation_spy.rb', line 49
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
41
42
43
44
45
|
# File 'lib/ardb/relation_spy.rb', line 41
def offset(value)
@offset_value = value ? value.to_i : nil
@applied << AppliedExpression.new(:offset, [ value ])
self
end
|
#only(*onlies) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/ardb/relation_spy.rb', line 67
def only(*onlies)
onlies = onlies.map(&:to_sym)
@applied.reject!{ |a| !onlies.include?(a.type) }
@limit_value = nil unless onlies.include?(:limit)
@offset_value = nil unless onlies.include?(:offset)
self
end
|