Class: EagerBeaver::MethodHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/eager_beaver/method_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ MethodHandler

Returns a new instance of MethodHandler.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eager_beaver/method_handler.rb', line 10

def initialize(&block)
  block.call(self)

  raise "match must be given" \
    if match.nil?
  raise "match must be a lambda" \
    unless match.lambda?

  raise "handle must be given" \
    if handle.nil?
  raise "handle must be a lambda" \
    unless handle.lambda?

  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/eager_beaver/method_handler.rb', line 39

def method_missing(method_name, *args, &block)
  if /\A(?<attr_name>[a-zA-Z]\w*)=?\z/ =~ method_name
    code = %Q{
      attr_accessor :#{attr_name}
    }
    self.singleton_class.instance_eval code, __FILE__, __LINE__ + 1
    return self.send(method_name, *args, &block)
  end
  super
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



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

def handle
  @handle
end

#matchObject

Returns the value of attribute match.



7
8
9
# File 'lib/eager_beaver/method_handler.rb', line 7

def match
  @match
end

#missing_method_nameObject

Returns the value of attribute missing_method_name.



6
7
8
# File 'lib/eager_beaver/method_handler.rb', line 6

def missing_method_name
  @missing_method_name
end

#original_receiverObject

Returns the value of attribute original_receiver.



5
6
7
# File 'lib/eager_beaver/method_handler.rb', line 5

def original_receiver
  @original_receiver
end

Instance Method Details

#evaluate(inner) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/eager_beaver/method_handler.rb', line 31

def evaluate(inner)
  outer = lambda { |*args|
    args.shift
    inner.call(*args)
  }
  self.instance_eval &outer
end

#handles?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/eager_beaver/method_handler.rb', line 26

def handles?(method_name)
  self.missing_method_name = method_name.to_s
  return evaluate(match)
end