Class: Putter::Follower

Inherits:
BasicObject
Defined in:
lib/putter/follower.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, options = {}) ⇒ Follower

Returns a new instance of Follower.



5
6
7
8
9
10
11
12
13
14
# File 'lib/putter/follower.rb', line 5

def initialize(obj, options={})
  @object = obj
  @proxy = MethodProxy.new
  begin
    @object.singleton_class.send(:prepend, proxy)
  rescue ::NoMethodError
    ::Kernel.raise ::Putter::BasicObjectError
  end
  _set_options(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/putter/follower.rb', line 16

def method_missing(method, *args, &blk)
  if _add_method?(method)
    add_method(method)
  end

  if blk
    @object.send(method, *args, &blk)
  else
    @object.send(method, *args)
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/putter/follower.rb', line 3

def object
  @object
end

#proxied_methodsObject (readonly)

Returns the value of attribute proxied_methods.



3
4
5
# File 'lib/putter/follower.rb', line 3

def proxied_methods
  @proxied_methods
end

#proxyObject (readonly)

Returns the value of attribute proxy.



3
4
5
# File 'lib/putter/follower.rb', line 3

def proxy
  @proxy
end

Instance Method Details

#_add_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/putter/follower.rb', line 39

def _add_method?(method)
  return true if _is_whitelisted_method?(method)
  return false if _is_ignored_method?(method)
  return false if @proxy.instance_methods.include?(method)
  return @proxy_all_methods || proxied_methods.include?(method.to_s)
end

#_is_ignored_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/putter/follower.rb', line 46

def _is_ignored_method?(method)
  ::Putter.configuration.ignore_methods_from.each do |klass|
    return true if klass.methods.include?(method.to_sym)
    return true if klass.instance_methods.include?(method.to_sym)
  end
  return false
end

#_is_whitelisted_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/putter/follower.rb', line 54

def _is_whitelisted_method?(method)
  ::Putter.configuration.methods_whitelist.map(&:to_sym).include?(method.to_sym)
end

#_set_label(label) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/putter/follower.rb', line 58

def _set_label(label)
  if !label.nil?
    @label = label
  elsif @object.class == ::Class
    @label = @object.name
  else
    @label = @object.class.name + " instance"
  end
end

#_set_methods(methods) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/putter/follower.rb', line 68

def _set_methods(methods)
  if methods.nil?
    @proxy_all_methods = true
  else
    @proxied_methods = methods.map(&:to_s)
    @proxy_all_methods = false
  end
end

#_set_options(options) ⇒ Object



77
78
79
80
# File 'lib/putter/follower.rb', line 77

def _set_options(options)
  _set_label(options[:label])
  _set_methods(options[:methods])
end

#add_method(method) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/putter/follower.rb', line 28

def add_method(method)
  @proxy.instance_exec(@label) do |label, print_results|
    define_method(method) do |*proxy_args, &blk|
      args_string = proxy_args.to_s
      result = super *proxy_args, &blk
      ::Putter.configuration.print_strategy.call label, method, args_string, result
      result
    end
  end
end