Class Ohm::Model
In: lib/ohm.rb
lib/ohm/json.rb
Parent: Object

The base class for all your models. In order to better understand it, here is a semi-realtime explanation of the details involved when creating a User instance.

Example:

  class User < Ohm::Model
    attribute :name
    index :name

    attribute :email
    unique :email

    counter :points

    set :posts, :Post
  end

  u = User.create(:name => "John", :email => "foo@bar.com")
  u.increment :points
  u.posts.add(Post.create)

When you execute `User.create(...)`, you run the following Redis commands:

  # Generate an ID
  INCR User:id

  # Add the newly generated ID, (let's assume the ID is 1).
  SADD User:all 1

  # Store the unique index
  HSET User:uniques:email foo@bar.com 1

  # Store the name index
  SADD User:indices:name:John 1

  # Store the HASH
  HMSET User:1 name John email foo@bar.com

Next we increment points:

  HINCR User:1:counters points 1

And then we add a Post to the `posts` set. (For brevity, let‘s assume the Post created has an ID of 1).

  SADD User:1:posts 1

Methods

==   []   _sanitized_attributes   all   attribute   attributes   attributes   collection   counter   counters   create   decr   decrement   delete   eql?   exists?   fetch   filters   find   get   hash   id   incr   increment   index   indices   key   key   list   load!   model   mutex   new   new?   redis   redis   redis=   reference   reload_attributes   save   script   set   set   synchronize   to_hash   to_indices   to_json   to_proc   to_reference   track   tracked   unique   uniques   update   update_attributes   with  

Attributes

id  [W] 

Public Class methods

Retrieve a record by ID.

Example:

  u = User.create
  u == User[u.id]
  # =>  true

An Ohm::Set wrapper for Model.key[:all].

The bread and butter macro of all models. Basically declares persisted attributes. All attributes are stored on the Redis hash.

  class User < Ohm::Model
    attribute :name
  end

  user = User.new(name: "John")
  user.name
  # => "John"

  user.name = "Jane"
  user.name
  # => "Jane"

A lambda can be passed as a second parameter to add typecasting support to the attribute.

  class User < Ohm::Model
    attribute :age, ->(x) { x.to_i }
  end

  user = User.new(age: 100)

  user.age
  # => 100

  user.age.kind_of?(Integer)
  # => true

Check rubydoc.info/github/cyx/ohm-contrib#Ohm__DataTypes to see more examples about the typecasting feature.

A macro for defining a method which basically does a find.

Example:

  class Post < Ohm::Model
    reference :user, :User
  end

  class User < Ohm::Model
    collection :posts, :Post
  end

  # is the same as

  class User < Ohm::Model
    def posts
      Post.find(:user_id => self.id)
    end
  end

Declare a counter. All the counters are internally stored in a different Redis hash, independent from the one that stores the model attributes. Counters are updated with the `increment` and `decrement` methods, which interact directly with Redis. Their value can‘t be assigned as with regular attributes.

Example:

  class User < Ohm::Model
    counter :points
  end

  u = User.create
  u.increment :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.

Check if the ID exists within <Model>:all.

Retrieve a set of models given an array of IDs.

Example:

  User.fetch([1, 2, 3])

Find values in indexed fields.

Example:

  class User < Ohm::Model
    attribute :email

    attribute :name
    index :name

    attribute :status
    index :status

    index :provider
    index :tag

    def provider
      email[/@(.*?).com/, 1]
    end

    def tag
      ["ruby", "python"]
    end
  end

  u = User.create(name: "John", status: "pending", email: "foo@me.com")
  User.find(provider: "me", name: "John", status: "pending").include?(u)
  # => true

  User.find(:tag => "ruby").include?(u)
  # => true

  User.find(:tag => "python").include?(u)
  # => true

  User.find(:tag => ["ruby", "python"]).include?(u)
  # => true

Index any method on your model. Once you index a method, you can use it in `find` statements.

Returns the namespace for all the keys generated using this model.

Example:

  class User < Ohm::Model
  end

  User.key.kind_of?(Nest)
  # => true

To find out more about Nest, see:

  http://github.com/soveran/nest

Declare an Ohm::List with the given name.

Example:

  class Comment < Ohm::Model
  end

  class Post < Ohm::Model
    list :comments, :Comment
  end

  p = Post.create
  p.comments.push(Comment.create)
  p.comments.unshift(Comment.create)
  p.comments.size == 2
  # => true

Note: You can‘t use the list until you save the model. If you try to do it, you‘ll receive an Ohm::MissingID error.

Initialize a model using a dictionary of attributes.

Example:

  u = User.new(:name => "John")

A macro for defining an attribute, an index, and an accessor for a given model.

Example:

  class Post < Ohm::Model
    reference :user, :User
  end

  # It's the same as:

  class Post < Ohm::Model
    attribute :user_id
    index :user_id

    def user
      @_memo[:user] ||= User[user_id]
    end

    def user=(user)
      self.user_id = user.id
      @_memo[:user] = user
    end

    def user_id=(user_id)
      @_memo.delete(:user_id)
      self.user_id = user_id
    end
  end

Declare an Ohm::Set with the given name.

Example:

  class User < Ohm::Model
    set :posts, :Post
  end

  u = User.create
  u.posts.empty?
  # => true

Note: You can‘t use the set until you save the model. If you try to do it, you‘ll receive an Ohm::MissingID error.

Retrieve a set of models given an array of IDs.

Example:

  ids = [1, 2, 3]
  ids.map(&User)

Note: The use of this should be a last resort for your actual application runtime, or for simply debugging in your console. If you care about performance, you should pipeline your reads. For more information checkout the implementation of Ohm::List#fetch.

Keep track of `key[name]` and remove when deleting the object.

Create a unique index for any method on your model. Once you add a unique index, you can use it in `with` statements.

Note: if there is a conflict while saving, an `Ohm::UniqueIndexViolation` violation is raised.

Find values in `unique` indices.

Example:

  class User < Ohm::Model
    unique :email
  end

  u = User.create(:email => "foo@bar.com")
  u == User.with(:email, "foo@bar.com")
  # => true

Protected Class methods

Public Instance methods

Check for equality by doing the following assertions:

  1. That the passed model is of the same type.
  2. That they represent the same Redis key.

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" }
decr(att, count = 1)

Alias for decrement

Decrements a counter atomically. Internally uses `HINCRBY`.

  class Post
    counter :score
  end

  post = Post.create

  post.decrement(:score)
  post.score # => -1

  post.decrement(:hits, 2)
  post.score # => -3

Delete the model, including all the following keys:

If the model has uniques or indices, they‘re also cleaned up.

eql?(other)

Alias for #==

Read an attribute remotely from Redis. Useful if you want to get the most recent value of the attribute and not rely on locally cached value.

Example:

  User.create(:name => "A")

  Session 1     |    Session 2
  --------------|------------------------
  u = User[1]   |    u = User[1]
  u.name = "B"  |
  u.save        |
                |    u.name == "A"
                |    u.get(:name) == "B"

Return a value that allows the use of models as hash keys.

Example:

  h = {}

  u = User.new

  h[:u] = u
  h[:u] == u
  # => true

Access the ID used to store this model. The ID is used together with the name of the class in order to form the Redis key.

Example:

  class User < Ohm::Model; end

  u = User.create
  u.id
  # => 1

  u.key
  # => User:1
incr(att, count = 1)

Alias for increment

Increments a counter atomically. Internally uses `HINCRBY`.

  class Ad
    counter :hits
  end

  ad = Ad.create

  ad.increment(:hits)
  ad.hits # => 1

  ad.increment(:hits, 2)
  ad.hits # => 3

Returns the namespace for the keys generated using this model. Check `Ohm::Model.key` documentation for more details.

Preload all the attributes of this model from Redis. Used internally by `Model::[]`.

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

Reset the attributes table and load the passed values.

Persist the model attributes and update indices and unique indices. The `counter`s and `set`s are not touched during save.

Example:

  class User < Ohm::Model
    attribute :name
  end

  u = User.new(:name => "John").save
  u.kind_of?(User)
  # => true

Run lua scripts and cache the sha in order to improve successive calls.

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`.

Export the ID of the model. The approach of Ohm is to whitelist public attributes, as opposed to exporting each (possibly sensitive) attribute.

Example:

  class User < Ohm::Model
    attribute :name
  end

  u = User.create(:name => "John")
  u.to_hash
  # => { :id => "1" }

In order to add additional attributes, you can override `to_hash`:

  class User < Ohm::Model
    attribute :name

    def to_hash
      super.merge(:name => name)
    end
  end

  u = User.create(:name => "John")
  u.to_hash
  # => { :id => "1", :name => "John" }

Export a JSON representation of the model by encoding `to_hash`.

Update the model attributes and call save.

Example:

  User[1].update(:name => "John")

  # It's the same as:

  u = User[1]
  u.update_attributes(:name => "John")
  u.save

Write the dictionary of key-value pairs to the model.

Protected Instance methods

[Validate]