module OhmUtil

Constants

LUA_CACHE
LUA_DELETE
LUA_SAVE

Public Class Methods

const(context, name) click to toggle source

Used by: `attribute`, `counter`, `set`, `reference`, `collection`.

Employed as a solution to avoid `NameError` problems when trying to load models referring to other models not yet loaded.

Example:

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 or a string.

class Comment < Ohm::Model
  reference :user, :User
  reference :post, "Post"
end
# File lib/ohm_util.rb, line 62
def self.const(context, name)
        case name
        when Symbol, String
                context.const_get(name)
        else name
        end
end
dict(arr) click to toggle source
# File lib/ohm_util.rb, line 70
def self.dict(arr)
        Hash[*arr]
end
script(redis, file, *args) click to toggle source

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

# File lib/ohm_util.rb, line 88
def self.script(redis, file, *args)
        begin
                cache = LUA_CACHE[redis.url]

                if cache.key?(file)
                        sha = cache[file]
                else
                        src = File.read(file)
                        sha = redis.call("SCRIPT", "LOAD", src)

                        cache[file] = sha
                end

                redis.call!("EVALSHA", sha, *args)

        rescue RuntimeError

                case $!.message
                when ErrorPatterns::NOSCRIPT
                        LUA_CACHE[redis.url].clear
                        retry
                when ErrorPatterns::DUPLICATE
                        raise UniqueIndexViolation, $1
                else
                        raise $!
                end
        end
end
sort_options(options) click to toggle source
# File lib/ohm_util.rb, line 74
def self.sort_options(options)
        args = []

        args.concat(["BY", options[:by]]) if options[:by]
        args.concat(["GET", options[:get]]) if options[:get]
        args.concat(["LIMIT"] + options[:limit]) if options[:limit]
        args.concat(options[:order].split(" ")) if options[:order]
        args.concat(["STORE", options[:store]]) if options[:store]

        return args
end