Class: Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/redisant/criteria.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Criteria



7
8
9
10
11
12
13
14
15
16
# File 'lib/redisant/criteria.rb', line 7

def initialize base
  if base.is_a? Relation
    @ids_key = base.redis_key
    @object_class = base.object_class
    criteria[:relation] = base
  else
    @ids_key = base.id_key
    @object_class = base
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



149
150
151
152
# File 'lib/redisant/criteria.rb', line 149

def method_missing(*args, &block)
  load_if_needed
  @result.send(*args, &block)
end

Instance Attribute Details

#ids_keyObject (readonly)

Returns the value of attribute ids_key.



5
6
7
# File 'lib/redisant/criteria.rb', line 5

def ids_key
  @ids_key
end

#object_classObject (readonly)

Returns the value of attribute object_class.



5
6
7
# File 'lib/redisant/criteria.rb', line 5

def object_class
  @object_class
end

Instance Method Details

#any?Boolean



80
81
82
83
84
# File 'lib/redisant/criteria.rb', line 80

def any?
  criteria[:exists] = true
  criteria[:ids] = true
  result
end

#countObject



37
38
39
40
# File 'lib/redisant/criteria.rb', line 37

def count
  criteria[:count] = true
  result
end

#count?Boolean



121
122
123
# File 'lib/redisant/criteria.rb', line 121

def count?
  criteria[:count] == true
end

#criteriaObject



23
24
25
# File 'lib/redisant/criteria.rb', line 23

def criteria
  @criteria ||= {:where => {}}
end

#first(options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/redisant/criteria.rb', line 47

def first options={}
  merge_options options
  criteria[:offset] = 0
  criteria[:limit] = 1
  criteria[:order] = :asc
  result
end

#get_conditionsObject



137
138
139
# File 'lib/redisant/criteria.rb', line 137

def get_conditions
  criteria[:where]
end

#get_orderObject



145
146
147
# File 'lib/redisant/criteria.rb', line 145

def get_order
  criteria[:order]
end

#get_relationObject



141
142
143
# File 'lib/redisant/criteria.rb', line 141

def get_relation
  criteria[:relation]
end

#idsObject



75
76
77
78
# File 'lib/redisant/criteria.rb', line 75

def ids
  criteria[:ids] = true
  self
end

#ids?Boolean



87
88
89
# File 'lib/redisant/criteria.rb', line 87

def ids?
  criteria[:ids] == true
end

#last(options = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/redisant/criteria.rb', line 55

def last options={}
  merge_options options
  criteria[:offset] = 0
  criteria[:limit] = 1
  criteria[:order] = :desc
  result
end

#limit(limit) ⇒ Object



32
33
34
35
# File 'lib/redisant/criteria.rb', line 32

def limit limit
  criteria[:limit] = limit
  self
end

#limit?Boolean



117
118
119
# File 'lib/redisant/criteria.rb', line 117

def limit?
  criteria[:limit] != nil
end

#load_if_neededObject



154
155
156
157
158
# File 'lib/redisant/criteria.rb', line 154

def load_if_needed
  loaded = @loaded
  @loaded = true
  reload! unless loaded
end

#loaded?Boolean



161
162
163
# File 'lib/redisant/criteria.rb', line 161

def loaded?
  @loaded == true
end

#num_conditionsObject



129
130
131
132
133
134
135
# File 'lib/redisant/criteria.rb', line 129

def num_conditions
  n = criteria[:where].keys.size
  if criteria[:relation]
    n += 1
  end
  n
end

#order(options) ⇒ Object



68
69
70
71
72
73
# File 'lib/redisant/criteria.rb', line 68

def order options
  raise Redisant::InvalidArgument.new('Invalid order') unless ['asc','desc'].include? options.to_s
  
  criteria[:order] = options
  self
end

#order?Boolean



113
114
115
# File 'lib/redisant/criteria.rb', line 113

def order?
  criteria[:order] != nil
end

#randomObject



42
43
44
45
# File 'lib/redisant/criteria.rb', line 42

def random
  criteria[:random] = true
  result
end

#random?Boolean



125
126
127
# File 'lib/redisant/criteria.rb', line 125

def random?
  criteria[:random] == true
end

#relation(relation) ⇒ Object



18
19
20
21
# File 'lib/redisant/criteria.rb', line 18

def relation relation
  criteria[:relation] = relation
  self
end

#reload!Object



170
171
172
# File 'lib/redisant/criteria.rb', line 170

def reload!
  @result = Query.new(self).run
end

#resultObject



165
166
167
168
# File 'lib/redisant/criteria.rb', line 165

def result
  load_if_needed
  @result
end

#single?Boolean



103
104
105
106
107
# File 'lib/redisant/criteria.rb', line 103

def single?
  criteria[:limit] == 1 ||
  criteria[:random] ||
  criteria[:count]
end

#sort(options) ⇒ Object



63
64
65
66
# File 'lib/redisant/criteria.rb', line 63

def sort options
  criteria[:sort] = options
  self
end

#sort?Boolean



109
110
111
# File 'lib/redisant/criteria.rb', line 109

def sort?
  criteria[:sort] != nil
end

#where(options) ⇒ Object



27
28
29
30
# File 'lib/redisant/criteria.rb', line 27

def where options
  merge_options options
  self
end

#where?Boolean



91
92
93
# File 'lib/redisant/criteria.rb', line 91

def where?
  criteria[:where].size > 0
end

#where_multi?Boolean



99
100
101
# File 'lib/redisant/criteria.rb', line 99

def where_multi?
  num_conditions >= 1
end

#where_single?Boolean



95
96
97
# File 'lib/redisant/criteria.rb', line 95

def where_single?
  num_conditions == 1
end