Class: Intercept::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/intercept/worker.rb

Overview

Base class of the intercept module. A worker object is responsible to intercept and modify state of registering class’s objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registration_method, interceptor_map = {}, decorator_map = {}) ⇒ Worker



17
18
19
20
21
22
23
24
# File 'lib/intercept/worker.rb', line 17

def initialize(registration_method, interceptor_map = {}, decorator_map = {})
  @interceptor_map = interceptor_map
  @decorator_map = decorator_map

  if registration_method != 'intercept' && registration_method != :intercept
    define_singleton_method(registration_method) { |message| delivering_message(message) }
  end
end

Instance Attribute Details

#decorator_mapObject (readonly)

Returns the value of attribute decorator_map.



12
13
14
# File 'lib/intercept/worker.rb', line 12

def decorator_map
  @decorator_map
end

#interceptor_mapObject (readonly)

Returns the value of attribute interceptor_map.



12
13
14
# File 'lib/intercept/worker.rb', line 12

def interceptor_map
  @interceptor_map
end

Instance Method Details

#intercept(message) ⇒ Object

This method is called when we want to intercept and modify an object. ‘registration_method’ parameter of initialize is an alias to this method.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/intercept/worker.rb', line 29

def intercept(message)
  interceptor_map.each do |field, strategy|
    if message.respond_to?(field) && message.respond_to?("#{field}=")
      message.public_send("#{field}=", strategy.process(message.public_send field))
    end
  end

  decorator_map.each do |field, strategy|
    if message.respond_to?(field) && message.respond_to?("#{field}=")
      message.public_send("#{field}=", strategy.decorate(message.public_send field))
    end
  end
end