Class: Saint::ClassApi::OptsPool

Inherits:
Object
  • Object
show all
Defined in:
lib/saint/api/class/opts.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (OptsPool) initialize(pool, table = nil, &proc)

Returns a new instance of OptsPool



8
9
10
11
12
13
# File 'lib/saint/api/class/opts.rb', line 8

def initialize pool, table = nil, &proc
  @pool = pool || Presto::Cache::Memory.new
  @pool = @pool.new(normalize_name(table)) if table
  @opts = {}
  self.instance_exec &proc
end

Instance Attribute Details

- (Object) opts (readonly)

Returns the value of attribute opts



6
7
8
# File 'lib/saint/api/class/opts.rb', line 6

def opts
  @opts
end

- (Object) pool (readonly)

Returns the value of attribute pool



6
7
8
# File 'lib/saint/api/class/opts.rb', line 6

def pool
  @pool
end

Instance Method Details

- (Object) [](opt)



38
39
40
41
# File 'lib/saint/api/class/opts.rb', line 38

def [] opt
  return unless item = @pool[opt]
  item['value'] || item['default_value']
end

- (Object) []=(opt, val)



43
44
45
46
# File 'lib/saint/api/class/opts.rb', line 43

def []= opt, val
  return unless item = @pool[opt]
  @pool.update opt => item.update('value' => val)
end

- (Object) opt(name, *type_and_or_opts)

add new opt to OptsManager.

Parameters:

  • name (Symbol)
  • type_and_or_opts (Hash)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/saint/api/class/opts.rb', line 22

def opt name, *type_and_or_opts
  type, opts = :string, {}
  type_and_or_opts.each { |a| a.is_a?(Hash) ? opts.update(a) : type = a }
  name = normalize_name(name)
  opts['name'] = name
  opts['type'] = type.to_s
  opts['details'] = opts.delete(:details)
  opts['default_value'] = opts.delete(:default)
  if options = opts.delete(:options)
    options = Hash[options.zip(options)] if options.is_a?(Array)
  end
  opts['options'] = options.is_a?(Hash) ? options : {}
  @pool[name] = opts
  @opts[name] = opts
end