Module: MethodNotMissing

Extended by:
MethodNotMissing
Included in:
MethodNotMissing, OmnipotentObject
Defined in:
lib/method_not_missing.rb,
lib/method_not_missing/version.rb

Defined Under Namespace

Modules: MethodSearcher Classes: OmnipotentObject

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/method_not_missing.rb', line 12

def method_missing (meth, *args)
  result = nil
  puts "Missing: #{meth}"
  args.each { |arg| arg.extend MethodNotMissing rescue nil }
  found = MethodSearcher.search_method(meth.to_s).detect do |method_body|
    begin
      puts "*"*80
      puts method_body
      puts "*"*80

      vars = method_body.scan(/@[a-zA-Z0-9_]+/)
      puts "Vars: #{vars}"
      vars.each do |var|
        # We want a class with << because that's a common method and
        # it can't be found online.
        object = Class.new(Array) { include MethodNotMissing }.new
        instance_variable_get(var) or
          instance_variable_set(var, object)
      end

      self.class.send(:eval, method_body)
      result = self.send(meth, *args) do
        yield
      end
      true
    rescue Exception => e # We wanna rescue stack overflows also
      puts "Error!"
      puts e.inspect
      false
    end
  end

  if found
    result
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/method_not_missing.rb', line 8

def self.included (base)
  base.extend self
end

Instance Method Details

#const_missing(name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/method_not_missing.rb', line 51

def const_missing(name)
  puts "Missing class #{name}"
  puts "making it up on the spot"
  klass = Class.new(OmnipotentObject)
  const_set(name, klass)
  klass
end