Class: ReeActions::MethodPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/ree_lib/packages/ree_actions/package/ree_actions/method_plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, is_class_method, target) ⇒ MethodPlugin

Returns a new instance of MethodPlugin.



8
9
10
11
12
# File 'lib/ree_lib/packages/ree_actions/package/ree_actions/method_plugin.rb', line 8

def initialize(method_name, is_class_method, target)
  @method_name = method_name
  @is_class_method = is_class_method
  @target = target
end

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/ree_lib/packages/ree_actions/package/ree_actions/method_plugin.rb', line 4

def self.active?
  true  # Always active when registered
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ree_lib/packages/ree_actions/package/ree_actions/method_plugin.rb', line 14

def call
  # Only wrap :call method for classes that include ReeActions::DSL
  return nil unless @method_name == :call
  return nil unless ree_actions_class?

  Proc.new do |instance, next_layer, *args, **kwargs, &block|
    # ReeActions call signature: call(user_access, attrs, **opts, &block)
    # First arg is user_access, second is attrs (the one to cast)
    user_access, attrs, *rest_args = args

    if instance.class.const_defined?(:ActionCaster)
      caster = instance.class.const_get(:ActionCaster)

      unless caster.respond_to?(:cast)
        raise ArgumentError.new("ActionCaster does not respond to `cast` method")
      end

      attrs = begin
        caster.cast(attrs)
      rescue ReeMapper::TypeError, ReeMapper::CoercionError => e
        raise ReeActions::ParamError, e.message
      end
    end

    # Call next layer with cast attrs
    next_layer.call(user_access, attrs, *rest_args, **kwargs, &block)
  end
end