Module: SafeSpec

Included in:
RSpec::Mocks::Methods
Defined in:
lib/safe_spec.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



9
10
11
12
13
14
15
16
17
18
# File 'lib/safe_spec.rb', line 9

def method_missing(meth, *args, &block)
  method_regex = /^safe_(.+)/
  if meth.to_s =~ method_regex
    assert_valid_methods!(args.first)
    delegate_to = method_regex.match(meth.to_s)[1]
    self.send(delegate_to, *args, &block)
  else
    super
  end
end

Instance Method Details

#assert_valid_methods!(sym_or_hash) ⇒ Object



2
3
4
5
6
7
# File 'lib/safe_spec.rb', line 2

def assert_valid_methods!(sym_or_hash)
  methods_to_stub = Hash === sym_or_hash ? sym_or_hash.keys : [sym_or_hash]
  methods_to_stub.each do |meth|
    raise NoMethodError, "#{meth} was not found in the method list on #{self}" unless self.respond_to?(meth)
  end
end