lib/ohm.rb in ohm-0.1.3 vs lib/ohm.rb in ohm-0.1.4

- old
+ new

@@ -468,11 +468,11 @@ # end # # @see http://code.google.com/p/redis/wiki/SmembersCommand SMEMBERS # in Redis Command Reference. def each(&block) - key.smembers.each { |id| block.call(model[id]) } + key.smembers.each { |id| block.call(model.to_proc[id]) } end # Convenient way to scope access to a predefined set, useful for access # control. # @@ -782,11 +782,11 @@ # end # # @see http://code.google.com/p/redis/wiki/LrangeCommand LRANGE # in Redis Command Reference. def each(&block) - key.lrange(0, -1).each { |id| block.call(model[id]) } + key.lrange(0, -1).each { |id| block.call(model.to_proc[id]) } end # Thin wrapper around *RPUSH*. # # @example @@ -843,13 +843,13 @@ # @see http://code.google.com/p/redis/wiki/LrangeCommand LRANGE # in Redis Command Reference. def [](index, limit = nil) case [index, limit] when Pattern[Fixnum, Fixnum] then - key.lrange(index, limit).collect { |id| model[id] } + key.lrange(index, limit).collect { |id| model.to_proc[id] } when Pattern[Range, nil] then - key.lrange(index.first, index.last).collect { |id| model[id] } + key.lrange(index.first, index.last).collect { |id| model.to_proc[id] } when Pattern[Fixnum, nil] then model[key.lindex(index)] end end @@ -930,11 +930,11 @@ def inspect "#<List (#{model}): #{key.lrange(0, -1).inspect}>" end end - # All validations which need to access the _Redis_ database goes here. + # All validations that need access to the _Redis_ database go here. # As of this writing, {Ohm::Model::Validations#assert_unique} is the only # assertion contained within this module. module Validations include Ohm::Validations @@ -1285,11 +1285,11 @@ new(:id => id) if id && exists?(id) end # @private Used for conveniently doing [1, 2].map(&Post) for example. def self.to_proc - Proc.new { |id| self[id] } + lambda { |id| new(:id => id) } end # Returns a {Ohm::Model::Set set} containing all the members of a given # class. # @@ -1712,12 +1712,12 @@ # # you may want to call this method from outside of the class # # definition: # Post.connect(:port => 6380, :db => 2) # # @see file:README.html#connecting Ohm.connect options documentation. - def self.connect(*options) - self.db = Ohm.connection(*options) + def self.connect(options = {}) + @options = options end # @return [Ohm::Key] A key scoped to the model which uses this object's # id. # @@ -1801,11 +1801,13 @@ private # Provides access to the Redis database. This is shared accross all models and instances. def self.db - Ohm.threaded[self] || Ohm.redis + return Ohm.redis unless defined?(@options) + + Redis.connect(@options) end def self.db=(connection) Ohm.threaded[self] = connection end @@ -1997,6 +1999,5 @@ def lock_expired?(timestamp) timestamp.to_f < Time.now.to_f end end end -