lib/ohm.rb in ohm-3.0.3 vs lib/ohm.rb in ohm-3.1.0

- old
+ new

@@ -1,9 +1,9 @@ # encoding: UTF-8 require "json" -require "nido" +require "nest" require "redic" require "stal" module Ohm LUA_CACHE = Hash.new { |h, k| h[k] = Hash.new } @@ -171,21 +171,21 @@ @model = model end # Returns the total size of the list using LLEN. def size - redis.call("LLEN", key) + key.call("LLEN") end # Returns the first element of the list using LINDEX. def first - model[redis.call("LINDEX", key, 0)] + model[key.call("LINDEX", 0)] end # Returns the last element of the list using LINDEX. def last - model[redis.call("LINDEX", key, -1)] + model[key.call("LINDEX", -1)] end # Returns an array of elements from the list using LRANGE. # #range receives 2 integers, start and stop # @@ -209,11 +209,11 @@ # post.comments.push(c3) # # [c1, c2] == post.comments.range(0, 1) # # => true def range(start, stop) - fetch(redis.call("LRANGE", key, start, stop)) + fetch(key.call("LRANGE", start, stop)) end # Checks if the model is part of this List. # # An important thing to note is that this method loads all of the @@ -253,16 +253,16 @@ end end # Pushes the model to the _end_ of the list using RPUSH. def push(model) - redis.call("RPUSH", key, model.id) + key.call("RPUSH", model.id) end # Pushes the model to the _beginning_ of the list using LPUSH. def unshift(model) - redis.call("LPUSH", key, model.id) + key.call("LPUSH", model.id) end # Delete a model from the list. # # Note: If your list contains the model multiple times, this method @@ -290,11 +290,11 @@ # def delete(model) # LREM key 0 <id> means remove all elements matching <id> # @see http://redis.io/commands/lrem - redis.call("LREM", key, 0, model.id) + key.call("LREM", 0, model.id) end # Returns an array with all the ID's of the list. # # class Comment < Ohm::Model @@ -314,11 +314,11 @@ # # post.comments.ids # # => ["1", "2", "3"] # def ids - redis.call("LRANGE", key, 0, -1) + key.call("LRANGE", 0, -1) end private def redis @@ -376,11 +376,11 @@ # def ids if Array === key Stal.solve(redis, key) else - redis.call("SMEMBERS", key) + key.call("SMEMBERS") end end # Returns the total size of the set using SCARD. def size @@ -597,11 +597,11 @@ # post = Post.create # # user.posts.add(post) # def add(model) - redis.call("SADD", key, model.id) + key.call("SADD", model.id) end alias_method :<<, :add # Remove a model directly from the set. @@ -612,11 +612,11 @@ # post = Post.create # # user.posts.delete(post) # def delete(model) - redis.call("SREM", key, model.id) + key.call("SREM", model.id) end # Replace all the existing elements of a set with a different # collection of models. This happens atomically in a MULTI-EXEC # block. @@ -717,22 +717,18 @@ # Example: # # class User < Ohm::Model # end # - # User.key == "User" - # User.key.kind_of?(String) + # User.key.kind_of?(Nest) # # => true # - # User.key.kind_of?(Nido) - # # => true + # To find out more about Nest, see: + # http://github.com/soveran/nest # - # To find out more about Nido, see: - # http://github.com/soveran/nido - # def self.key - @key ||= Nido.new(self.name) + @key ||= Nest.new(self.name, redis) end # Retrieve a record by ID. # # Example: @@ -761,11 +757,11 @@ lambda { |id| self[id] } end # Check if the ID exists within <Model>:all. def self.exists?(id) - redis.call("SISMEMBER", key[:all], id) == 1 + key[:all].call("SISMEMBER", id) == 1 end # Find values in `unique` indices. # # Example: @@ -779,11 +775,11 @@ # # => true # def self.with(att, val) raise IndexNotFound unless uniques.include?(att) - id = redis.call("HGET", key[:uniques][att], val) + id = key[:uniques][att].call("HGET", val) new(:id => id).load! if id end # Find values in indexed fields. # @@ -1077,11 +1073,11 @@ counters << name unless counters.include?(name) define_method(name) do return 0 if new? - redis.call("HGET", key[:counters], name).to_i + key[:counters].call("HGET", name).to_i end end # Keep track of `key[name]` and remove when deleting the object. def self.track(name) @@ -1139,19 +1135,17 @@ # # 1. That the passed model is of the same type. # 2. That they represent the same Redis key. # def ==(other) - other.kind_of?(model) && other.key == key - rescue MissingID - false + other.kind_of?(model) && other.hash == hash end # Preload all the attributes of this model from Redis. Used # internally by `Model::[]`. def load! - update_attributes(Utils.dict(redis.call("HGETALL", key))) unless new? + update_attributes(Utils.dict(key.call("HGETALL"))) unless new? return self end # Read an attribute remotely from Redis. Useful if you want to get # the most recent value of the attribute and not rely on locally @@ -1168,24 +1162,24 @@ # u.save | # | u.name == "A" # | u.get(:name) == "B" # def get(att) - @attributes[att] = redis.call("HGET", key, att) + @attributes[att] = key.call("HGET", att) end # Update an attribute value atomically. The best usecase for this # is when you simply want to update one value. # # Note: This method is dangerous because it doesn't update indices # and uniques. Use it wisely. The safe equivalent is `update`. # def set(att, val) if val.to_s.empty? - redis.call("HDEL", key, att) + key.call("HDEL", att) else - redis.call("HSET", key, att, val) + key.call("HSET", att, val) end @attributes[att] = val end @@ -1222,10 +1216,10 @@ # # ad.increment(:hits, 2) # ad.hits # => 3 # def increment(att, count = 1) - redis.call("HINCRBY", key[:counters], att, count) + key[:counters].call("HINCRBY", att, count) end # Decrements a counter atomically. Internally uses `HINCRBY`. # # class Post