Module: Dry::Operation::ClassContext

Included in:
Dry::Operation
Defined in:
lib/dry/operation/class_context.rb,
lib/dry/operation/class_context/prepend_manager.rb,
lib/dry/operation/class_context/steps_method_prepender.rb

Overview

Dry::Operation class context

Defined Under Namespace

Modules: MethodAddedHook Classes: PrependManager, StepsMethodPrepender

Constant Summary collapse

DEFAULT_METHODS_TO_PREPEND =

Default methods to be prepended unless changed via #operate_on

[:call].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.directly_inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
# File 'lib/dry/operation/class_context.rb', line 46

def self.directly_inherited(klass)
  klass.extend(MethodAddedHook)
  klass.instance_variable_set(
    :@_prepend_manager,
    PrependManager.new(klass: klass, methods_to_prepend: DEFAULT_METHODS_TO_PREPEND)
  )
end

.indirectly_inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
59
60
# File 'lib/dry/operation/class_context.rb', line 55

def self.indirectly_inherited(klass)
  klass.instance_variable_set(
    :@_prepend_manager,
    klass.superclass.instance_variable_get(:@_prepend_manager).for_subclass(klass)
  )
end

Instance Method Details

#inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
42
43
# File 'lib/dry/operation/class_context.rb', line 36

def inherited(klass)
  super
  if klass.superclass == Dry::Operation
    ClassContext.directly_inherited(klass)
  else
    ClassContext.indirectly_inherited(klass)
  end
end

#operate_on(*methods) ⇒ Object

Configures the instance methods to be prepended

The given methods will be prepended with a wrapper that calls Dry::Operation#steps before calling the original method.

This method must be called before defining any of the methods to be prepended or before prepending any other method.

Parameters:

  • methods (Array<Symbol>)

    methods to prepend

Raises:



22
23
24
# File 'lib/dry/operation/class_context.rb', line 22

def operate_on(*methods)
  @_prepend_manager.register(*methods)
end

#skip_prependingObject

Skips prepending any method

This method must be called before any method is prepended.

Raises:



31
32
33
# File 'lib/dry/operation/class_context.rb', line 31

def skip_prepending
  @_prepend_manager.void
end