Class: Copland::Interceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/interceptor.rb

Overview

Represents an interceptor associated with a service point, not a service. When the service point with which this interceptor is associated is instantiated, the #instantiate method of this object will be invoked to obtain the actual interceptor instance.

This wrapper object encapsulates the “before” and “after” lists, as well, which are used to determine the order in which the interceptors on the associated service point are invoked for each intercepted method call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, definition) ⇒ Interceptor

Create a new interceptor on the given owner service point, with the associated definition map. The map must include a value named “service”, which should be the name of the service point of the factory that will be used to instantiate the interceptor when needed. If “before” or “after” exist, they are interpreted to be the “before” and “after” attributes of this interceptor. (If either of them are strings, they will be converted into an array of one element; otherwise, they should be arrays.)

Any other elements in the definition map will be used as constructor parameters.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/copland/interceptor.rb', line 79

def initialize( owner, definition )
  @owner = owner

  unless definition[ "service" ] 
    raise MissingImplementationException,
      "interceptor for #{owner.full_name} needs 'service' element"
  end

  @point = owner.find_service_point( definition[ "service" ] )

  @before = [ *( definition[ "before" ] || [] ).dup ]
  @after = [ *( definition[ "after" ] || [] ).dup ]

  definition = definition.dup
  definition.delete "service"
  definition.delete "before"
  definition.delete "after"

  @construction_parms = definition

  schema = point.schema
  if schema.respond_to?( :validate )
    schema.validate @point, @owner, @construction_parms
  end
end

Instance Attribute Details

#afterObject (readonly)

The array of interceptor service point names (not services) that this interceptor should come after.



61
62
63
# File 'lib/copland/interceptor.rb', line 61

def after
  @after
end

#beforeObject (readonly)

The array of interceptor service point names (not services) that this interceptor should come before.



57
58
59
# File 'lib/copland/interceptor.rb', line 57

def before
  @before
end

#construction_parmsObject (readonly)

A hash of the constructor parameters that should be sent to the factory when instantiating the interceptor service. This attribute is the value of the hash prior to processing by any schema.



66
67
68
# File 'lib/copland/interceptor.rb', line 66

def construction_parms
  @construction_parms
end

#ownerObject (readonly)

The service point with which this interceptor is associated.



49
50
51
# File 'lib/copland/interceptor.rb', line 49

def owner
  @owner
end

#pointObject (readonly)

The service point of the factory that will return the interceptor instance on demand.



53
54
55
# File 'lib/copland/interceptor.rb', line 53

def point
  @point
end

Instance Method Details

#after?(interceptor) ⇒ Boolean

Returns true if self should be ordered after interceptor.

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/copland/interceptor.rb', line 130

def after?( interceptor )
  a = after.include?( interceptor.point.full_name )
  b = interceptor.before.include?( point.full_name )

  return a || b
end

#before?(interceptor) ⇒ Boolean

Returns true if self should be ordered before interceptor.

Returns:

  • (Boolean)


122
123
124
125
126
127
# File 'lib/copland/interceptor.rb', line 122

def before?( interceptor )
  a = before.include?( interceptor.point.full_name )
  b = interceptor.after.include?( point.full_name )

  return a || b
end

#instantiateObject

Return an instance of the interceptor service that is wrapped by this object. This is done by invoking the #create_instance method on the factory service. If the factory service point has a schema associated with it, it will be used to pre-process the parameters.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/copland/interceptor.rb', line 109

def instantiate
  @factory = @point.instance unless @factory

  parms = @construction_parms
  schema = @point.schema
  if schema.respond_to?( :process )
    parms = schema.process( point, owner, parms )
  end

  @factory.create_instance( @owner, parms )
end