Class: Expectations::BlankSlate

Inherits:
Object
  • Object
show all
Defined in:
lib/expectations/blank_slate.rb

Overview

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

Recorder

Class Method Summary collapse

Class Method Details

.find_hidden_method(name) ⇒ Object



32
33
34
35
# File 'lib/expectations/blank_slate.rb', line 32

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 instance_eval or any method beginning with “__”.



22
23
24
25
26
27
28
29
30
# File 'lib/expectations/blank_slate.rb', line 22

def hide(name)
  method_name = RUBY_VERSION < '1.9' ? name.to_s : name.to_sym
  if instance_methods.include?(method_name) and
    name !~ /^(__|instance_eval|extend|is_a?|object_id)/
    @hidden_methods ||= {}
    @hidden_methods[name.to_sym] = instance_method(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.



39
40
41
42
43
44
45
46
47
# File 'lib/expectations/blank_slate.rb', line 39

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