Class: ConfigatronBlankSlate

Inherits:
Object
  • Object
show all
Defined in:
lib/configatron/configatron_blank_slate.rb

Overview

This version of BlankSlate was taken from Builder and modified by Rob Sanheim to remove hooks into the various built-in callbacks (ie method_added). Not sure if we need that complexity (yet) in configatron’s use case.

BlankSlate provides an abstract base class with no predefined methods (except for _\send_ and _\id_). BlankSlate is useful as a base class when writing classes that depend upon method_missing (e.g. dynamic proxies).

Direct Known Subclasses

Configatron, Configatron::Store

Constant Summary collapse

CONFIGATRON_WHITELIST =

These methods are used by configatron internals, so we have to whitelist them. We could probably do some alias magic to get them to be proper __foo style methods , but this is okay for now.

%w[instance_eval methods instance_variable_get is_a? class]

Class Method Summary collapse

Class Method Details

.find_hidden_method(name) ⇒ Object



39
40
41
42
# File 'lib/configatron/configatron_blank_slate.rb', line 39

def find_hidden_method(name)
  @hidden_methods ||= {}
  @hidden_methods[name] || superclass.find_hidden_method(name)
end

.hide(name) ⇒ Object

Hide the method named name in the BlankSlate class. Don’t hide methods in CONFIGATRON_WHITELIST or any method beginning with “__”.



30
31
32
33
34
35
36
37
# File 'lib/configatron/configatron_blank_slate.rb', line 30

def hide(name)
  if instance_methods.include?(name.to_s) and
    name !~ /^(__|#{CONFIGATRON_WHITELIST.join("|")})/
    @hidden_methods ||= {}
    @hidden_methods[name.to_sym] = instance_method(name)
    undef_method name
  end
end

.reveal(name) ⇒ Object

Redefine a previously hidden method so that it may be called on a blank slate object.



46
47
48
49
50
51
52
53
54
# File 'lib/configatron/configatron_blank_slate.rb', line 46

def reveal(name)
  bound_method = nil
  unbound_method = find_hidden_method(name)
  fail "Don't know how to reveal method '#{name}'" unless unbound_method
  define_method(name) do |*args|
    bound_method ||= unbound_method.bind(self)
    bound_method.call(*args)
  end
end