Module: PEROBS::Delegator

Defined in:
lib/perobs/Delegator.rb

Overview

This Delegator module provides the methods to turn the PEROBS::Array and PEROBS::Hash into proxies for Array and Hash.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Proxy all calls to unknown methods to the data object.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/perobs/Delegator.rb', line 37

def method_missing(method_sym, *args, &block)
  if self.class::READERS.include?(method_sym) ||
     Enumerable.instance_methods.include?(method_sym)
    # If any element of this class is read, we register this object as
    # being read with the cache.
    @store.cache.cache_read(self)
    @data.send(method_sym, *args, &block)
  elsif self.class::REWRITERS.include?(method_sym)
    # Re-writers don't introduce any new elements. We just mark the object
    # as written in the cache and call the class' method.
    @store.cache.cache_write(self)
    @data.send(method_sym, *args, &block)
  elsif (alias_sym = self.class::ALIASES[method_sym])
    @store.cache.cache_write(self)
    send(alias_sym, *args, &block)
  else
    # Any method we don't know about must cause an error. A new class
    # method needs to be added to the right bucket first.
    raise NoMethodError.new("undefined method '#{method_sym}' for " +
                            "#{self.class}")
  end
end

Instance Method Details

#==(obj) ⇒ Object

Equivalent to Class::== This method is just a reader but also part of BasicObject. Hence BasicObject::== would be called instead of method_missing.



70
71
72
73
# File 'lib/perobs/Delegator.rb', line 70

def ==(obj)
  @store.cache.cache_read(self)
  @data == obj
end

#respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/perobs/Delegator.rb', line 60

def respond_to?(method_sym, include_private = false)
  self.class::READERS.include?(method_sym) ||
    Enumerable.instance_methods.include?(method_sym) ||
    self.class::REWRITERS.include?(method_sym) ||
    super
end