Module: Ohm

Defined in:
lib/ohm/pattern.rb,
lib/ohm.rb,
lib/ohm/key.rb,
lib/ohm/version.rb,
lib/ohm/validations.rb,
lib/ohm/compat-1.8.6.rb,
lib/ohm/utils/upgrade.rb

Overview

Pattern extends Array with case equality to provide meaningful semantics in case statements.

After this change, pattern matching-like behavior is possible with Arrays:

    Pattern[Fixnum, String] === [1, "foo"]
    Pattern[Symbol, Array] === [:foo, [1, 2]]

When used in a case statement, it provides a functionality close to that of languages with proper pattern matching. It becomes useful when implementing a polymorphic method:

    def [](index, limit = nil)
      case [index, limit]
      when Pattern[Fixnum, Fixnum] then
        key.lrange(index, limit).collect { |id| model[id] }
      when Pattern[Range, nil] then
        key.lrange(index.first, index.last).collect { |id| model[id] }
      when Pattern[Fixnum, nil] then
        model[key.lindex(index)]
      end
    end

Defined Under Namespace

Modules: Utils, Validations Classes: Error, Key, Model, Pattern

Constant Summary

VERSION =
"0.1.3"
BasicObject =
::BasicObject

Class Method Summary (collapse)

Class Method Details

+ (Object) connect(*options)

Connect to a Redis database.

It is also worth mentioning that you can pass in a URI e.g.

  Ohm.connect :url => "redis://127.0.0.1:6379/0"

Note that the value 0 refers to the database number for the given Redis instance.

Also you can use connect without any arguments. The behavior will be as follows:

  # Connect to redis://127.0.0.1:6379/0
  Ohm.connect

  # Connect to redis://10.0.0.100:22222/5
  ENV["REDIS_URL"] = "redis://10.0.0.100:22222/5"
  Ohm.connect

Examples:

Connect to a database in port 6380.

Ohm.connect(:port => 6380)

Parameters:

  • options ({Symbol => #to_s})

    An options hash.

See Also:



107
108
109
110
# File 'lib/ohm.rb', line 107

def self.connect(*options)
  self.redis = nil
  @options = options
end

+ (Object) flush

Clear the database. You typically use this only during testing, or when you seed your site.



130
131
132
# File 'lib/ohm.rb', line 130

def self.flush
  redis.flushdb
end

+ (Object) redis

Provides access to the Redis database. It is highly recommended that you use this sparingly, and only if you really know what you’re doing.

The better way to access the Redis database and do raw Redis commands would be one of the following:

  1. Use Ohm::Model.key or Ohm::Model#key. So if the name of your model is Post, it would be Post.key or the protected method #key which should be used within your Post model.

  2. Use Ohm::Model.db or Ohm::Model#db. Although this is also accessible, it is much cleaner and terse to use Ohm::Model.key.

Examples:

class Post < Ohm::Model
  def comment_ids
    key[:comments].zrange(0, -1)
  end

  def add_comment_id(id)
    key[:comments].zadd(Time.now.to_i, id)
  end

  def remove_comment_id(id)
    # Let's use the db style here just to demonstrate.
    db.zrem key[:comments], id
  end
end

Post.key[:latest].sadd(1)
Post.key[:latest].smembers == ["1"]
# => true

Post.key[:latest] == "Post:latest"
# => true

p = Post.create
p.comment_ids == []
# => true

p.add_comment_id(101)
p.comment_ids == ["101"]
# => true

p.remove_comment_id(101)
p.comment_ids == []
# => true


62
63
64
# File 'lib/ohm.rb', line 62

def self.redis
  threaded[:redis] ||= connection(*options)
end

+ (Object) redis=(connection)

Assign a new Redis connection. Internally used by connect to clear the cached Redis instance.

If you’re looking to change the connection or reconnect with different parameters, try connect or Ohm::Model.connect.

Parameters:

  • connection (Redis)

    an instance created using `Redis.new`.

See Also:



74
75
76
# File 'lib/ohm.rb', line 74

def self.redis=(connection)
  threaded[:redis] = connection
end