Class: Yao::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/yao/config.rb

Constant Summary collapse

HOOK_RENEW_CLIENT_KEYS =
i(tenant_name username password auth_url debug debug_record_response)

Instance Method Summary collapse

Instance Method Details

#_configuration_defaultsHash

Returns:

  • (Hash)


5
6
7
# File 'lib/yao/config.rb', line 5

def _configuration_defaults
  @_configuration_defaults ||= {}
end

#_configuration_hooksHash

Returns:

  • (Hash)


10
11
12
# File 'lib/yao/config.rb', line 10

def _configuration_hooks
  @_configuration_hooks ||= {}
end

#_configuration_hooks_queueArray

Returns:

  • (Array)


15
16
17
# File 'lib/yao/config.rb', line 15

def _configuration_hooks_queue
  @_configuration_hooks_queue ||= []
end

#configurationHash

Returns:

  • (Hash)


20
21
22
# File 'lib/yao/config.rb', line 20

def configuration
  @configuration ||= {}
end

#delay_hook=(v) ⇒ Array

Parameters:

  • v (Boolean)

Returns:

  • (Array)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yao/config.rb', line 28

def delay_hook=(v)
  @delay_hook = v
  if !v and !_configuration_hooks_queue.empty?
    _configuration_hooks_queue.each do |n, val|
      _configuration_hooks[n].call(val) if _configuration_hooks[n]
    end
    # Authorization process should have special hook
    # and should run last
    unless (_configuration_hooks_queue.map(&:first) & HOOK_RENEW_CLIENT_KEYS).empty?
      Yao::Auth.try_new
    end

    _configuration_hooks_queue.clear
  end
end

#delay_hook?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/yao/config.rb', line 45

def delay_hook?
  @delay_hook
end

#param(name, default, &hook) ⇒ Object

Parameters:

  • name (String)
  • default (String)
  • hook (Proc)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yao/config.rb', line 52

def param(name, default, &hook)
  raise("Duplicate definition of #{name}") if self.respond_to?(name)

  name = name.to_sym
  _configuration_defaults[name] = default
  _configuration_hooks[name] = hook if block_given?
  self.define_singleton_method(name) do |*a|
    case a.size
    when 0
      configuration[name] || _configuration_defaults[name]
    when 1
      set(name, a.first)
    else
      raise ArgumentError, "wrong number of arguments (#{a.size} for 0, 1)"
    end
  end
end

#set(name, value) ⇒ String

Parameters:

  • name (String)
  • value (String)

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
# File 'lib/yao/config.rb', line 73

def set(name, value)
  raise("Undefined config key #{name}") unless self.respond_to?(name)
  configuration[name.to_sym] = value
  if delay_hook?
    _configuration_hooks_queue.push([name, value])
  else
    _configuration_hooks[name].call(value) if _configuration_hooks[name]
  end
  value
end