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)
-
+ (Object) connect(*options)
Connect to a Redis database.
-
+ (Object) flush
Clear the database.
-
+ (Object) redis
Provides access to the Redis database.
-
+ (Object) redis=(connection)
Assign a new Redis connection.
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
107 108 109 110 |
# File 'lib/ohm.rb', line 107 def self.connect(*) self.redis = nil @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:
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.
Use Ohm::Model.db or Ohm::Model#db. Although this is also accessible, it is much cleaner and terse to use Ohm::Model.key.
62 63 64 |
# File 'lib/ohm.rb', line 62 def self.redis threaded[:redis] ||= connection(*) 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.
74 75 76 |
# File 'lib/ohm.rb', line 74 def self.redis=(connection) threaded[:redis] = connection end |