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


71
72
73
74
75
76
77
# File 'lib/ohm.rb', line 71

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

.dict(arr) ⇒ Object



79
80
81
# File 'lib/ohm.rb', line 79

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

.sort_options(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ohm.rb', line 83

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