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
-
.const(context, name) ⇒ Object
Used by:
attribute,counter,set,reference,collection. - .dict(arr) ⇒ Object
- .sort_options(options) ⇒ Object
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.() args = [] args.concat(["BY", [:by]]) if [:by] args.concat(["GET", [:get]]) if [:get] args.concat(["LIMIT"] + [:limit]) if [:limit] args.concat([:order].split(" ")) if [:order] args.concat(["STORE", [:store]]) if [:store] return args end |