Class: Putter::Follower

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MethodCreator

#add_putter_method_to_proxy

Constructor Details

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

Returns a new instance of Follower.



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

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



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

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.



5
6
7
# File 'lib/putter/follower.rb', line 5

def object
  @object
end

#proxied_methodsObject (readonly)

Returns the value of attribute proxied_methods.



5
6
7
# File 'lib/putter/follower.rb', line 5

def proxied_methods
  @proxied_methods
end

#proxyObject (readonly)

Returns the value of attribute proxy.



5
6
7
# File 'lib/putter/follower.rb', line 5

def proxy
  @proxy
end

Instance Method Details

#_add_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/putter/follower.rb', line 36

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)


43
44
45
46
47
48
49
# File 'lib/putter/follower.rb', line 43

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)


51
52
53
# File 'lib/putter/follower.rb', line 51

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

#_set_label(label) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/putter/follower.rb', line 55

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



65
66
67
68
69
70
71
72
# File 'lib/putter/follower.rb', line 65

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



74
75
76
77
# File 'lib/putter/follower.rb', line 74

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

#add_method(method) ⇒ Object



30
31
32
33
34
# File 'lib/putter/follower.rb', line 30

def add_method(method)
  data = ProxyMethodData.new(method, @label)

  add_putter_method_to_proxy(@proxy, :instance_exec, data)
end