Module: DuckPuncher::Registration

Included in:
DuckPuncher
Defined in:
lib/duck_puncher/registration.rb

Overview

Note:

When updating this file please update comment regarding this module in duck_puncher.rb

Instance Method Summary collapse

Instance Method Details

#deregister(*targets) ⇒ Object

Remove extensions for a given class or list of classes



32
33
34
35
# File 'lib/duck_puncher/registration.rb', line 32

def deregister(*targets)
  targets.each &Ducks.list.method(:delete)
  targets.each &decorators.method(:delete)
end

#register(target, *mods, &block) ⇒ Object

Register an extension with a target class When given a block, the block is used to create an anonymous module

Parameters:

  • target (Class, Module, Object)

    constant or instance to extend

  • mods (Array<Module>)

    modules to extend or mix into the target. The last argument can be a hash of options to customize the extension

  • :only (Hash)

    a customizable set of options

  • :method (Hash)

    a customizable set of options

  • :before (Hash)

    a customizable set of options

  • :after (Hash)

    a customizable set of options



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/duck_puncher/registration.rb', line 12

def register(target, *mods, &block)
  options = mods.last.is_a?(Hash) ? mods.pop : {}
  mods << Module.new(&block) if block
  target = DuckPuncher.lookup_constant target
  Ducks.list[target] = Set.new [] unless Ducks.list.key?(target)
  mods = Array(mods).each do |mod|
    duck = UniqueDuck.new Duck.new(target, mod, options)
    Ducks.list[target] << duck
  end
  [target, *mods]
end

#register!(*args, &block) ⇒ Object

Register an extension and then immediately activate it See #register for accepted arguments



26
27
28
29
# File 'lib/duck_puncher/registration.rb', line 26

def register!(*args, &block)
  register *args, &block
  call args.first
end