Class: Dacs::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/dacs/schema.rb

Instance Method Summary collapse

Instance Method Details

#default_value(key) ⇒ Object



27
28
29
30
# File 'lib/dacs/schema.rb', line 27

def default_value(key)
  assert_key_defined!(key)
  key_def(key.to_s)[:default]
end

#defaultsObject



32
33
34
35
36
37
# File 'lib/dacs/schema.rb', line 32

def defaults
  key_defs.inject({}) { |h, key_def|
    h[key_def[:name]] = key_def[:default] if key_def.key?(:default)
    h
  }
end

#defined?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dacs/schema.rb', line 23

def defined?(key)
  !!key_def(key)
end

#key(name, options = {}) ⇒ Object

Add a key definition



5
6
7
# File 'lib/dacs/schema.rb', line 5

def key(name, options={})
  key_defs << { :name => name.to_s }.merge(options)
end

#keysObject



9
10
11
# File 'lib/dacs/schema.rb', line 9

def keys
  key_defs.map{|d| d[:name]}
end

#optional?(key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/dacs/schema.rb', line 19

def optional?(key)
  !required?(key.to_s)
end

#required?(key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/dacs/schema.rb', line 13

def required?(key)
  assert_key_defined!(key)
  kd = key_def(key.to_s)
  kd && !kd.key?(:default)
end