Module: Module::Cluster::InstanceController::HookController::HookControllerInterface

Included in:
Module::Cluster::InstanceController::HookController
Defined in:
lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb

Overview

Interface implementation for

{::Module::Cluster::InstanceController::HookController Module::Cluster::InstanceController::HookController}. 
Implementation provided separately for ease of overloading.

Defined Under Namespace

Classes: FrameStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameSymbol, String (readonly)

Name of hook controller.

Returns:

  • (Symbol, String)

    Name.



412
413
414
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 412

def name
  @name
end

#parent_instance_controllerModule::Cluster::InstanceController (readonly)



425
426
427
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 425

def parent_instance_controller
  @parent_instance_controller
end

#Reference to instance controller for which this hook controller operates.(toinstancecontroller) ⇒ Module::Cluster::InstanceController (readonly)



425
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 425

attr_reader :parent_instance_controller

#stackArray (readonly)

Each hook controller maintains a stack of event hooks to iterate at each include/extend/subclass event.

Returns:

  • (Array)

    Stack of event hooks.



399
400
401
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 399

def stack
  @stack
end

#Stack of event hooks.(ofeventhooks.) ⇒ Array (readonly)

Each hook controller maintains a stack of event hooks to iterate at each include/extend/subclass event.

Returns:

  • (Array)

    Stack of event hooks.



399
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 399

attr_reader :stack

Instance Method Details

#action {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController

Declare that action should be performed at event hook.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



159
160
161
162
163
164
165
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 159

def action( & block )
  
  action_at_index( -1, nil, nil, nil, false, & block )
  
  return self
  
end

#action_at_index(index, cluster_name, contexts, cascade_to, explicit_set = false) {|hooked_instance| ... } ⇒ Integer

Used by ChainProxy to insert modules before/after other modules.

Parameters:

  • index

    Index where include should occur.

  • cluster_name

    Name of cluster for which include event is to occur.

  • contexts

    Contexts for which event hooks should occur.

  • cascade_to

    Contexts for which event hooks should cascade.

  • explicit_set (defaults to: false)

    Whether insert is an implicit position or an explicitly requested position.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:

  • (Integer)

    Index after inserts.



668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 668

def action_at_index( index, cluster_name, contexts, cascade_to, explicit_set = false, & block )
  
  new_frame = self.class::FrameStruct.new( @instance, cluster_name, contexts, cascade_to, block )
  
  @stack.insert( index, new_frame )

  unless index < 0
    index += 1
  end
  
  return index
  
end

#before_extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy

Declare that chained actions should be inserted into the event stack after the location

in the same event stack where provided module(s) are specified to be extended.

Parameters:

  • module

    Module that insert should be after.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 324

def after_extend( *modules, & block )

  after_module_index = highest_index( :extend, *modules ) + 1
  
  proxy = chain_proxy
  
  proxy.index( after_module_index )
  
  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#before_include {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy

Declare that chained actions should be inserted into the event stack after the location

in the same event stack where provided module(s) are specified to be included.

Parameters:

  • module

    Module that insert should be after.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 290

def after_include( *modules, & block )

  after_module_index = highest_index( :include, *modules ) + 1

  proxy = chain_proxy

  proxy.index( after_module_index )
  
  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#before_include_or_extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy Also known as: after_extend_or_include

Declare that chained actions should be inserted into the event stack after the location

in the same event stack where provided module(s) are specified to be included or extended.

Parameters:

  • module

    Module that insert should be after.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 359

def after_include_or_extend( *modules, & block )
  
  after_module_index = highest_index( :include_or_extend, *modules ) + 1
  
  proxy = chain_proxy
  
  proxy.index( after_module_index )
  
  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#before_extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy

Declare that chained actions should be inserted into the event stack prior to the location

in the same event stack where provided module(s) are specified to be extended.

Parameters:

  • module

    Module that insert should be prior to.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 185

def before_extend( *modules, & block )
  
  before_module_index = lowest_index( :extend, *modules )
  
  proxy = chain_proxy
  
  proxy.index( before_module_index )
  
  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#before_include {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy

Declare that chained actions should be inserted into the event stack prior to the location

in the same event stack where provided module(s) are specified to be included.

Parameters:

  • module

    Module that insert should be prior to.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 219

def before_include( *modules, & block )
  
  before_module_index = lowest_index( :include, *modules )
  
  proxy = chain_proxy
  
  proxy.index( before_module_index )

  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#before_include_or_extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController::ChainProxy Also known as: before_extend_or_include

Declare that chained actions should be inserted into the event stack prior to the location

in the same event stack where provided module(s) are specified to be included or extended.

Parameters:

  • module

    Module that insert should be prior to.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 254

def before_include_or_extend( *modules, & block )

  before_module_index = lowest_index( :include_or_extend, *modules )

  proxy = chain_proxy

  proxy.index( before_module_index )

  if block_given?
    proxy.action( & block )
  end
  
  return proxy
  
end

#chain_proxyModule::Cluster::InstanceController::HookController::ChainProxy

Get Chain Proxy instance used by this Hook Controller.



693
694
695
696
697
698
699
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 693

def chain_proxy

  @chain_proxy ||= self.class::ChainProxy.new( self )
  
  return @chain_proxy.reset_state
  
end

#extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController

Declare that modules should be extended at event hook.

Parameters:

  • module

    Module to extend at event hook.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



82
83
84
85
86
87
88
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 82

def extend( *modules, & block )

  extend_at_index( -1, nil, nil, nil, false, *modules, & block )

  return self
  
end

#extend_and_include {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController

Declare that modules should be extended and included at event hook. Order is reversed from #include_and_extend.

Parameters:

  • module

    Module to extend and include at event hook.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



135
136
137
138
139
140
141
142
143
144
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 135

def extend_and_include( *modules, & block )
  
  modules.each do |this_module|
    extend( this_module, & block )
    include( this_module, & block )
  end
  
  return self
  
end

#extend_and_include_at_index(index, cluster_name, contexts, cascade_to, explicit_set = false, *modules) {|hooked_instance| ... } ⇒ Integer

Used by ChainProxy to insert modules before/after other modules.

Parameters:

  • index

    Index where extend and include and extend should occur.

  • cluster_name

    Name of cluster for which extend and include and extend event is to occur.

  • contexts

    Contexts for which event hooks should occur.

  • cascade_to

    Contexts for which event hooks should cascade.

  • explicit_set (defaults to: false)

    Whether insert is an implicit position or an explicitly requested position.

  • modules

    Modules to extend and include.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #extend and include and extendd and #extended.

Returns:

  • (Integer)

    Index after inserts.



632
633
634
635
636
637
638
639
640
641
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 632

def extend_and_include_at_index( index, cluster_name, contexts, cascade_to, explicit_set = false, *modules, & block )
  
  modules.each do |this_module|
    extend_at_index( index, cluster_name, contexts, cascade_to, explicit_set, this_module, & block )
    include_at_index( index, cluster_name, contexts, cascade_to, explicit_set, this_module, & block )
  end
  
  return self
  
end

#extend_at_index(index, cluster_name, contexts, cascade_to, explicit_set = false, *modules) {|hooked_instance| ... } ⇒ Integer

Used by ChainProxy to insert modules before/after other modules.

Parameters:

  • index

    Index where extend should occur.

  • cluster_name

    Name of cluster for which extend event is to occur.

  • contexts

    Contexts for which event hooks should occur.

  • cascade_to

    Contexts for which event hooks should cascade.

  • explicit_set (defaults to: false)

    Whether insert is an implicit position or an explicitly requested position.

  • modules

    Modules to extend.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #extendd and #extended.

Returns:

  • (Integer)

    Index after inserts.



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 524

def extend_at_index( index, cluster_name, contexts, cascade_to, explicit_set = false, *modules, & block )

  modules.each do |this_module|
    
    should_insert = true
    
    if @extend_modules.has_key?( this_module )

      if explicit_set
        # if we have an existing index, insert at this one instead
        existing_index = @stack.index { |this_frame | this_frame.action == :extend and 
                                                      this_frame.module == this_module }
        @stack.delete_at( existing_index )
        if index > 0 and existing_index < index
          index -= 1
        end
      else
        should_insert = false
      end

    end
    
    if should_insert
      this_stack_frame = self.class::FrameStruct.new( @instance, 
                                                      cluster_name, 
                                                      contexts ? contexts.empty? ? nil : contexts.dup : nil, 
                                                      cascade_to ? cascade_to.empty? ? nil : cascade_to.dup : nil, 
                                                      block, 
                                                      this_module, 
                                                      :extend )
      @stack.insert( index, this_stack_frame )
      unless index < 0
        index += 1
      end
      @extend_modules[ this_module ] = true
    end

  end
  
  return index
  
end

#highest_index(include_or_extend) ⇒ Integer

Determine the highest index for provided module(s).

Parameters:

  • include_or_extend

    :include, :extend or :include_and_extend

  • module

    Modules for which highest-index is being determined.

Returns:

  • (Integer)

    Highest index.



741
742
743
744
745
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 741

def highest_index( include_or_extend, *modules )

  return indexes( include_or_extend, *modules )[ -1 ]
  
end

#include {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController

Declare that modules should be included at event hook.

Parameters:

  • module

    Module to include at event hook.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



57
58
59
60
61
62
63
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 57

def include( *modules, & block )
  
  include_at_index( -1, nil, nil, nil, false, *modules, & block )

  return self
  
end

#include_and_extend {|hooked_instance| ... } ⇒ Module::Cluster::InstanceController::HookController

Declare that modules should be included and extended at event hook. See also #extend_and_include.

Parameters:

  • module

    Module to include and extend at event hook.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:



107
108
109
110
111
112
113
114
115
116
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 107

def include_and_extend( *modules, & block )
  
  modules.each do |this_module|
    include( this_module, & block )
    extend( this_module, & block )
  end
  
  return self
  
end

#include_and_extend_at_index(index, cluster_name, contexts, cascade_to, explicit_set = false, *modules) {|hooked_instance| ... } ⇒ Integer

Used by ChainProxy to insert modules before/after other modules.

Parameters:

  • index

    Index where include and extend should occur.

  • cluster_name

    Name of cluster for which include and extend event is to occur.

  • contexts

    Contexts for which event hooks should occur.

  • cascade_to

    Contexts for which event hooks should cascade.

  • explicit_set (defaults to: false)

    Whether insert is an implicit position or an explicitly requested position.

  • modules

    Modules to include and extend.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #include and extendd and #extended.

Returns:

  • (Integer)

    Index after inserts.



594
595
596
597
598
599
600
601
602
603
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 594

def include_and_extend_at_index( index, cluster_name, contexts, cascade_to, explicit_set = false, *modules, & block )
  
  modules.each do |this_module|
    index = include_at_index( index, cluster_name, contexts, cascade_to, explicit_set, this_module, & block )
    index = extend_at_index( index, cluster_name, contexts, cascade_to, explicit_set, this_module, & block )
  end
  
  return index
  
end

#include_at_index(index, cluster_name, contexts, cascade_to, explicit_set = false, *modules) {|hooked_instance| ... } ⇒ Integer

Used by ChainProxy to insert modules before/after other modules.

Parameters:

  • index

    Index where include should occur.

  • cluster_name

    Name of cluster for which include event is to occur.

  • contexts

    Contexts for which event hooks should occur.

  • cascade_to

    Contexts for which event hooks should cascade.

  • explicit_set (defaults to: false)

    Whether insert is an implicit position or an explicitly requested position.

  • modules

    Modules to include.

Yields:

  • (hooked_instance)

    Block for event hook action.

Yield Parameters:

  • hooked_instance

    Instance for which event hook is occurring. Equivalent to parameter for #included and #extended.

Returns:

  • (Integer)

    Index after inserts.



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 454

def include_at_index( index, cluster_name, contexts, cascade_to, explicit_set = false, *modules, & block )
  
  modules.each do |this_module|
    
    should_insert = true
    
    if @include_modules.has_key?( this_module )

      if explicit_set
        # if we have an existing index, insert at this one instead
        existing_index = @stack.index { |this_frame | this_frame.action == :include and 
                                                      this_frame.module == this_module }
        @stack.delete_at( existing_index )
        if existing_index < index
          index -= 1
        end
      else
        should_insert = false
      end

    end
    
    if should_insert
      this_stack_frame = self.class::FrameStruct.new( @instance, 
                                                      cluster_name, 
                                                      contexts ? contexts.empty? ? nil : contexts.dup : nil, 
                                                      cascade_to ? cascade_to.empty? ? nil : cascade_to.dup : nil, 
                                                      block, 
                                                      this_module, 
                                                      :include )
      @stack.insert( index, this_stack_frame )
      unless index < 0
        index += 1
      end
      @include_modules[ this_module ] = true
    end
    
  end
  
  return index
  
end

#initialize(name, parent_instance_controller) ⇒ Object

Parameters:

  • name

    Name of this Hook Controller.

  • parent_instance_controller

    Instance controller for which this Hook Controller is operative.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 25

def initialize( name, parent_instance_controller )
  
  @name = name
  
  @parent_instance_controller = parent_instance_controller
      
  @instance = @parent_instance_controller.instance
  
  @stack = [ ]
  
  @extend_modules = { }
  @include_modules = { }
  
end

#lowest_index(include_or_extend) ⇒ Integer

Determine the lowest index for provided module(s).

Parameters:

  • include_or_extend

    :include, :extend or :include_and_extend

  • module

    Modules for which lowest-index is being determined.

Returns:

  • (Integer)

    Lowest index.



718
719
720
721
722
# File 'lib/module/cluster/instance_controller/hook_controller/hook_controller_interface.rb', line 718

def lowest_index( include_or_extend, *modules )
  
  return indexes( include_or_extend, *modules )[ 0 ]
  
end