Module: Ohm::Utils

Defined in:
lib/ohm.rb

Overview

Instead of monkey patching Kernel or trying to be clever, it's best to confine all the helper methods in a Utils module.

Class Method Summary collapse

Class Method Details

.const(context, name) ⇒ Object

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

66
67
68
69
70
71
72
# File 'lib/ohm.rb', line 66

def self.const(context, name)
  case name
  when Symbol, String
    context.const_get(name)
  else name
  end
end

.dict(arr) ⇒ Object


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

def self.dict(arr)
  Hash[*arr]
end

.sort_options(options) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ohm.rb', line 78

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