class Configurate::Proxy

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 +?+.

Constants

COMMON_KEY_NAMES

Public Class Methods

new(lookup_chain) click to toggle source

@param lookup_chain [#lookup]

# File lib/configurate/proxy.rb, line 19
def initialize lookup_chain
  @lookup_chain = lookup_chain
  @setting_path = SettingPath.new
end

Public Instance Methods

!() click to toggle source
# File lib/configurate/proxy.rb, line 24
def !
  !target
end
_proxy?() click to toggle source
# File lib/configurate/proxy.rb, line 48
def _proxy?
  true
end
get(*args)
Alias for: target
method_missing(setting, *args, &block) click to toggle source

rubocop:disable Style/MethodMissingSuper we handle all calls rubocop:disable Style/MissingRespondToMissing we override respond_to? instead

# File lib/configurate/proxy.rb, line 72
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.question_action_or_setter?

  self
end
public_send(*args, &block)
Alias for: send
respond_to?(method, include_private=false) click to toggle source
# File lib/configurate/proxy.rb, line 52
def respond_to? method, include_private=false
  method == :_proxy? || target_respond_to?(method, include_private)
end
send(*args, &block) click to toggle source
# File lib/configurate/proxy.rb, line 56
def send *args, &block
  __send__(*args, &block)
end
Also aliased as: public_send
singleton_class() click to toggle source
# File lib/configurate/proxy.rb, line 61
def singleton_class
  target.singleton_class
rescue ::TypeError
  class << self
    self
  end
end
target(*args) click to toggle source

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

# File lib/configurate/proxy.rb, line 85
def target *args
  return if @setting_path.empty?

  @lookup_chain.lookup @setting_path, *args
end
Also aliased as: get

Private Instance Methods

proxy?(obj) click to toggle source
# File lib/configurate/proxy.rb, line 105
def proxy? obj
  obj.respond_to?(:_proxy?) && obj._proxy?
end
target_or_object(obj) click to toggle source
# File lib/configurate/proxy.rb, line 109
def target_or_object obj
  proxy?(obj) ? obj.target : obj
end
target_respond_to?(setting, include_private=false) click to toggle source
# File lib/configurate/proxy.rb, line 96
def target_respond_to? setting, include_private=false
  return false if COMMON_KEY_NAMES.include? setting

  value = target
  return false if proxy? value

  value.respond_to? setting, include_private
end