Class: Configurate::Proxy

Inherits:
BasicObject
Defined in:
lib/configurate/proxy.rb

Overview

Proxy object to support nested settings

Cavehats: Since this object is always true, adding a ? at the end returns the value, if found, instead of the proxy object. So instead of if settings.foo.bar use if settings.foo.bar? to check for boolean values, if settings.foo.bar.nil? to check for nil values and of course you can do if settings.foo.bar.present? to check for empty values if you’re in Rails. Call #get to actually return the value, commonly when doing settings.foo.bar.get || “default”. Also don’t use this in case statements since Module#=== can’t be fooled, again call #get.

If a setting ends with = it’s too called directly, just like with ?.

Instance Method Summary collapse

Constructor Details

#initialize(lookup_chain) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • lookup_chain (#lookup)


17
18
19
20
# File 'lib/configurate/proxy.rb', line 17

def initialize lookup_chain
  @lookup_chain = lookup_chain
  @setting_path = SettingPath.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(setting, *args, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/configurate/proxy.rb', line 45

def method_missing setting, *args, &block
  return target.public_send(setting, *args, &block) if target_respond_to? setting

  @setting_path << setting
  
  return target(*args) if @setting_path.is_question_or_setter?
  
  self
end

Instance Method Details

#!Object



22
23
24
# File 'lib/configurate/proxy.rb', line 22

def !
  !target
end

#_proxy?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/configurate/proxy.rb', line 32

def _proxy?
  true
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/configurate/proxy.rb', line 36

def respond_to? method, include_private=false
  method == :_proxy? || target_respond_to?(method, include_private)
end

#send(*args, &block) ⇒ Object Also known as: public_send



40
41
42
# File 'lib/configurate/proxy.rb', line 40

def send *args, &block
  __send__(*args, &block)
end

#target(*args) ⇒ Object Also known as: get

Get the setting at the current path, if found. (see LookupChain#lookup)



57
58
59
60
61
# File 'lib/configurate/proxy.rb', line 57

def target *args
  return if @setting_path.empty?

  @lookup_chain.lookup @setting_path, *args
end