Class: Redisted::Base::Relation
- Inherits:
-
Object
- Object
- Redisted::Base::Relation
- Defined in:
- lib/redisted/relation.rb
Overview
Relation
Instance Method Summary collapse
- #all ⇒ Object
- #delete_all ⇒ Object
-
#destroy_all ⇒ Object
TODO: Don’t instantiate the object for delete…do it manually…
- #each(&proc) ⇒ Object
-
#first ⇒ Object
Calculate Results.
-
#initialize(the_class, redis, scopes, indices) ⇒ Relation
constructor
A new instance of Relation.
- #internal_add_reference(redis_key) ⇒ Object
-
#is_in(index_name, *args) ⇒ Object
Low Level Setup Conditions.
- #last ⇒ Object
- #method_missing(id, *args) ⇒ Object
Constructor Details
#initialize(the_class, redis, scopes, indices) ⇒ Relation
Returns a new instance of Relation.
9 10 11 12 13 14 15 |
# File 'lib/redisted/relation.rb', line 9 def initialize the_class,redis,scopes,indices @the_class=the_class @redis=redis @scopes=scopes @indices=indices @is_in_list=[] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/redisted/relation.rb', line 131 def method_missing(id, *args) return add_one args[0] if id.to_s=="<<" return @scopes[id].call(*args) if @scopes[id] return is_in id[3..-1],*args if id[0,3]=="by_" super end |
Instance Method Details
#all ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/redisted/relation.rb', line 79 def all ret=[] key=merge_keys @is_in_list if key id_list=@redis.zrange key,0,-1 id_list.each do |id| obj=@the_class.new obj.load id ret<<obj end else keyprefix="#{prefix}:" key_list=@redis.keys "#{keyprefix}*" key_list.each do |key| obj=@the_class.new obj.load key[keyprefix.size..-1] ret<<obj end end ret end |
#delete_all ⇒ Object
120 121 122 123 124 125 |
# File 'lib/redisted/relation.rb', line 120 def delete_all # TODO: Don't instantiate the object for delete...do it manually... #self.each do |m| # m.delete #end end |
#destroy_all ⇒ Object
TODO: Don’t instantiate the object for delete…do it manually… self.each do |m|
m.delete
end
126 127 128 129 130 |
# File 'lib/redisted/relation.rb', line 126 def destroy_all self.each do |m| m.destroy end end |
#each(&proc) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/redisted/relation.rb', line 100 def each &proc key=merge_keys @is_in_list if key id_list=@redis.zrange key,0,-1 id_list.each do |id| obj=@the_class.new obj.load id proc.call(obj) end else keyprefix="#{prefix}:" key_list=@redis.keys "#{keyprefix}*" key_list.each do |key| obj=@the_class.new obj.load key[keyprefix.size..-1] proc.call(obj) end end nil end |
#first ⇒ Object
Calculate Results
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/redisted/relation.rb', line 45 def first key=merge_keys @is_in_list id=nil if key id_list=@redis.zrange key,0,-1 # LEELEE: Performance improve... raise InvalidQuery,"No keys" if id_list.size==0 id=id_list.first else keyprefix="#{prefix}:" key_list=@redis.keys "#{keyprefix}*" raise InvalidQuery,"No keys" if key_list.size==0 id=id_list.first[keyprefix.size..-1] end obj=@the_class.new obj.load id obj end |
#internal_add_reference(redis_key) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/redisted/relation.rb', line 30 def internal_add_reference redis_key @is_in_list<<{ :index_name=>nil, :args=>nil, :index=>{ :redis_key=>redis_key } } end |
#is_in(index_name, *args) ⇒ Object
Low Level Setup Conditions
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/redisted/relation.rb', line 19 def is_in index_name,*args index=@indices[index_name.to_sym] raise InvalidQuery,"Could not find index '#{index_name}'" if index.nil? if index[:match] raise InvalidQuery,"No parameter provided to index when one was expected" if args.size!=1 else raise InvalidQuery,"Parameter provided to index when not expected" if args.size!=0 end @is_in_list<<{:index_name=>index_name,:index=>index,:args=>args} self end |
#last ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/redisted/relation.rb', line 62 def last key=merge_keys @is_in_list id=nil if key id_list=@redis.zrange key,0,-1 # LEELEE: Performance improve... raise InvalidQuery,"No keys" if id_list.size==0 id=id_list.last else keyprefix="#{prefix}:" key_list=@redis.keys "#{keyprefix}*" raise InvalidQuery,"No keys" if key_list.size==0 id=id_list.last[keyprefix.size..-1] end obj=@the_class.new obj.load id obj end |