lib/ohm.rb in ohm-2.0.0.alpha5 vs lib/ohm.rb in ohm-2.0.0.rc1

- old
+ new

@@ -54,15 +54,16 @@ # # class Comment < Ohm::Model # reference :user, User # NameError undefined constant User. # end # - # Instead of relying on some clever `const_missing` hack, we can - # simply use a Symbol. + # # Instead of relying on some clever `const_missing` hack, we can + # # simply use a symbol or a string. # # class Comment < Ohm::Model # reference :user, :User + # reference :post, "Post" # end # def self.const(context, name) case name when Symbol, String @@ -164,11 +165,10 @@ # Returns the total size of the list using LLEN. def size redis.call("LLEN", key) end - alias :count :size # Returns the first element of the list using LINDEX. def first model[redis.call("LINDEX", key, 0)] end @@ -340,11 +340,10 @@ # Returns the total size of the set using SCARD. def size execute { |key| redis.call("SCARD", key) } end - alias :count :size # Syntactic sugar for `sort_by` or `sort` when you only need the # first element. # # Example: @@ -383,15 +382,33 @@ # def [](id) model[id] if exists?(id) end - private + # Returns +true+ if +id+ is included in the set. Otherwise, returns +false+. + # + # Example: + # + # class Post < Ohm::Model + # end + # + # class User < Ohm::Model + # set :posts, :Post + # end + # + # user = User.create + # post = Post.create + # user.posts.add(post) + # + # user.posts.exists?('nonexistent') # => false + # user.posts.exists?(post.id) # => true + # def exists?(id) execute { |key| redis.call("SISMEMBER", key, id) == 1 } end + private def to_key(att) if model.counters.include?(att) namespace["*:counters->%s" % att] else namespace["*->%s" % att] @@ -567,11 +584,11 @@ # # You can also do it in one line. # User.find(:name => "John").except(:country => "US") # def except(dict) MultiSet.new( - namespace, model, Command[:sdiffstore, command, intersected(dict)] + namespace, model, Command[:sdiffstore, command, unioned(dict)] ) end # Do a union to the existing set using any number of filters. # @@ -596,10 +613,14 @@ def intersected(dict) Command[:sinterstore, *model.filters(dict)] end + def unioned(dict) + Command[:sunionstore, *model.filters(dict)] + end + def execute # namespace[:tmp] is where all the temp keys should be stored in. # redis will be where all the commands are executed against. response = command.call(namespace[:tmp], redis) @@ -675,11 +696,11 @@ def self.redis @redis ||= Redic.new(Ohm.redis.url) end - # The namespace for all the keys generated using this model. + # Returns the namespace for all the keys generated using this model. # # Example: # # class User < Ohm::Model # @@ -1016,11 +1037,11 @@ # end # # u = User.create # u.incr :points # - # Ohm.redis.hget "User:1:counters", "points" + # u.points # # => 1 # # Note: You can't use counters until you save the model. If you # try to do it, you'll receive an Ohm::MissingID error. # @@ -1047,25 +1068,12 @@ # Syntactic sugar for Model.new(atts).save def self.create(atts = {}) new(atts).save end - # Manipulate the Redis hash of attributes directly. - # - # Example: - # - # class User < Ohm::Model - # attribute :name - # end - # - # u = User.create(:name => "John") - # u.key.hget(:name) - # # => John - # - # For more details see - # http://github.com/soveran/nest - # + # Returns the namespace for the keys generated using this model. + # Check `Ohm::Model.key` documentation for more details. def key model.key[id] end # Initialize a model using a dictionary of attributes. @@ -1151,10 +1159,25 @@ end @attributes[att] = val end + # Returns +true+ if the model is not persisted. Otherwise, returns +false+. + # + # Example: + # + # class User < Ohm::Model + # attribute :name + # end + # + # u = User.new(:name => "John") + # u.new? + # # => true + # + # u.save + # u.new? + # # => false def new? !defined?(@id) end # Increment a counter atomically. Internally uses HINCRBY. @@ -1182,9 +1205,23 @@ def hash new? ? super : key.hash end alias :eql? :== + # Returns a hash of the attributes with their names as keys + # and the values of the attributes as values. It doesn't + # include the ID of the model. + # + # Example: + # + # class User < Ohm::Model + # attribute :name + # end + # + # u = User.create(:name => "John") + # u.attributes + # # => { :name => "John" } + # def attributes @attributes end # Export the ID of the model. The approach of Ohm is to