Class: Ardb::RelationSpy

Inherits:
Object
  • 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

#initializeRelationSpy

Returns a new instance of RelationSpy.



8
9
10
11
12
13
14
15
# File 'lib/ardb/relation_spy.rb', line 8

def initialize
  @applied, @results = [], []
  @offset_value, @limit_value = nil, nil

  @pluck_values   = {}
  @maximum_values = {}
  @minimum_values = {}
end

Instance Attribute Details

#appliedObject (readonly)

Returns the value of attribute applied.



3
4
5
# File 'lib/ardb/relation_spy.rb', line 3

def applied
  @applied
end

#limit_valueObject

Returns the value of attribute limit_value.



4
5
6
# File 'lib/ardb/relation_spy.rb', line 4

def limit_value
  @limit_value
end

#maximum_valuesObject

Returns the value of attribute maximum_values.



5
6
7
# File 'lib/ardb/relation_spy.rb', line 5

def maximum_values
  @maximum_values
end

#minimum_valuesObject

Returns the value of attribute minimum_values.



5
6
7
# File 'lib/ardb/relation_spy.rb', line 5

def minimum_values
  @minimum_values
end

#offset_valueObject

Returns the value of attribute offset_value.



4
5
6
# File 'lib/ardb/relation_spy.rb', line 4

def offset_value
  @offset_value
end

#pluck_valuesObject

Returns the value of attribute pluck_values.



5
6
7
# File 'lib/ardb/relation_spy.rb', line 5

def pluck_values
  @pluck_values
end

#resultsObject

Returns the value of attribute results.



6
7
8
# File 'lib/ardb/relation_spy.rb', line 6

def results
  @results
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/ardb/relation_spy.rb', line 23

def ==(other)
  other.kind_of?(self.class) ? @applied == other.applied : super
end

#allObject



122
123
124
# File 'lib/ardb/relation_spy.rb', line 122

def all
  @results[(@offset_value || 0), (@limit_value || @results.size)] || []
end

#countObject

ActiveRecord::Calculations



128
129
130
# File 'lib/ardb/relation_spy.rb', line 128

def count
  all.size
end

#except(*skips) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/ardb/relation_spy.rb', line 81

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



101
102
103
104
# File 'lib/ardb/relation_spy.rb', line 101

def find(id)
  record = @results.detect{ |result| result.id == id }
  record || raise(NotFoundError)
end

#firstObject



106
107
108
# File 'lib/ardb/relation_spy.rb', line 106

def first
  self.all.first
end

#first!Object



110
111
112
# File 'lib/ardb/relation_spy.rb', line 110

def first!
  self.first || raise(NotFoundError)
end

#initialize_copy(copied_from) ⇒ Object



17
18
19
20
21
# File 'lib/ardb/relation_spy.rb', line 17

def initialize_copy(copied_from)
  super
  @applied = copied_from.applied.dup
  @results = copied_from.results.dup
end

#lastObject



114
115
116
# File 'lib/ardb/relation_spy.rb', line 114

def last
  self.all.last
end

#last!Object



118
119
120
# File 'lib/ardb/relation_spy.rb', line 118

def last!
  self.last || raise(NotFoundError)
end

#limit(value) ⇒ Object



57
58
59
60
61
# File 'lib/ardb/relation_spy.rb', line 57

def limit(value)
  @limit_value = value ? value.to_i : nil
  @applied << AppliedExpression.new(:limit, [ value ])
  self
end

#maximum(column_name) ⇒ Object



136
137
138
# File 'lib/ardb/relation_spy.rb', line 136

def maximum(column_name)
  @maximum_values[column_name]
end

#merge(other) ⇒ Object

ActiveRecord::SpawnMethods



71
72
73
74
75
76
77
78
79
# File 'lib/ardb/relation_spy.rb', line 71

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

#minimum(column_name) ⇒ Object



140
141
142
# File 'lib/ardb/relation_spy.rb', line 140

def minimum(column_name)
  @minimum_values[column_name]
end

#offset(value) ⇒ Object



63
64
65
66
67
# File 'lib/ardb/relation_spy.rb', line 63

def offset(value)
  @offset_value = value ? value.to_i : nil
  @applied << AppliedExpression.new(:offset, [ value ])
  self
end

#only(*onlies) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ardb/relation_spy.rb', line 90

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

#pluck(column_name) ⇒ Object



132
133
134
# File 'lib/ardb/relation_spy.rb', line 132

def pluck(column_name)
  [@pluck_values[column_name]] * @results.size
end

#reset!Object



31
32
33
34
35
36
# File 'lib/ardb/relation_spy.rb', line 31

def reset!
  @applied.clear
  @results.clear
  @offset_value = nil
  @limit_value  = nil
end

#to_sqlObject



27
28
29
# File 'lib/ardb/relation_spy.rb', line 27

def to_sql
  @applied.map(&:to_sql).join(", ")
end