Class: TagListProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_tags/tag_list_proxy.rb

Direct Known Subclasses

TagListCollection

Instance Method Summary collapse

Constructor Details

#initialize(owner, context) ⇒ TagListProxy

Returns a new instance of TagListProxy.



8
9
10
11
# File 'lib/scoped_tags/tag_list_proxy.rb', line 8

def initialize(owner, context)
  @owner, @context = owner, context
  @target = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

Forwards any missing method call to the target.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scoped_tags/tag_list_proxy.rb', line 75

def method_missing(method, *args)
  if reload
    unless @target.respond_to?(method)
      message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
      raise NoMethodError, message
    end

    if block_given?
      @target.send(method, *args)  { |*block_args| yield(*block_args) }
    else
      @target.send(method, *args)
    end
  end
end

Instance Method Details

#===(other) ⇒ Object

Forwards === explicitly to the target because the instance method removal above doesn’t catch it. Loads the target if needed.



35
36
37
38
# File 'lib/scoped_tags/tag_list_proxy.rb', line 35

def ===(other)
  reload
  other === @target
end

#inspectObject

Forwards the call to the target. Loads the target if needed.



58
59
60
61
# File 'lib/scoped_tags/tag_list_proxy.rb', line 58

def inspect
  reload
  @target.inspect
end

#proxy_contextObject

Returns the context of the proxy, same as context.



19
20
21
# File 'lib/scoped_tags/tag_list_proxy.rb', line 19

def proxy_context
  @context
end

#proxy_ownerObject

Returns the owner of the proxy.



14
15
16
# File 'lib/scoped_tags/tag_list_proxy.rb', line 14

def proxy_owner
  @owner
end

#proxy_respond_to?Object



3
# File 'lib/scoped_tags/tag_list_proxy.rb', line 3

alias_method :proxy_respond_to?, :respond_to?

#proxy_targetObject

Returns the target of the proxy, same as target.



24
25
26
# File 'lib/scoped_tags/tag_list_proxy.rb', line 24

def proxy_target
  @target
end

#reloadObject

Reloads the target and returns self on success.



41
42
43
44
45
# File 'lib/scoped_tags/tag_list_proxy.rb', line 41

def reload
  @target = nil
  load_target
  self unless @target.nil?
end

#respond_to?(*args) ⇒ Boolean

Does the proxy or its target respond to symbol?

Returns:

  • (Boolean)


29
30
31
# File 'lib/scoped_tags/tag_list_proxy.rb', line 29

def respond_to?(*args)
  proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args))
end

#send(method, *args) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/scoped_tags/tag_list_proxy.rb', line 63

def send(method, *args)
  if proxy_respond_to?(method)
    super
  else
    reload
    @target.send(method, *args)
  end
end

#targetObject

Returns the target of this proxy, same as proxy_target.



48
49
50
# File 'lib/scoped_tags/tag_list_proxy.rb', line 48

def target
  @target
end

#target=(target) ⇒ Object

Sets the target of this proxy to \target, and the loaded flag to true.



53
54
55
# File 'lib/scoped_tags/tag_list_proxy.rb', line 53

def target=(target)
  @target = target
end