Module: ObjHash::ClassMethods

Defined in:
lib/objhash.rb

Instance Method Summary collapse

Instance Method Details

#objhash_configObject



16
17
18
19
20
21
# File 'lib/objhash.rb', line 16

def objhash_config
  @objhash_values ||= {
                       "created_at" => {:default => lambda { Time.now }},
                       "edited_at" => {:default => lambda { Time.now }}
                      }
end

#objhash_default_value(property) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/objhash.rb', line 23

def objhash_default_value(property)
  property = property.to_s

  raise "Unknown property #{property}" unless objhash_config.include?(property)

  if objhash_config[property][:default].is_a?(Proc)
    objhash_config[property][:default].call
  else
    objhash_config[property][:default]
  end
end

#property(name, args = {:default => nil, :validation => nil}) ⇒ Object

Create a new known property of the ObjHash it’s imagined these might have validators, defaults required etc associated with them in args



8
9
10
11
12
13
14
# File 'lib/objhash.rb', line 8

def property(name, args={:default => nil, :validation => nil})
  name = name.to_s

  raise "Already have a property #{name}" if objhash_config.include?(name)

  objhash_config[name] = {:default => nil, :validation => nil}.merge(args)
end