Module: ReeActions::DSL::ClassMethods

Includes:
Ree::Contracts::ArgContracts, Ree::Contracts::Core
Defined in:
lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb

Instance Method Summary collapse

Instance Method Details

#action(name, &proc) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb', line 22

def action(name, &proc)
  dsl = Ree::ObjectDsl.new(
    Ree.container.packages_facade, name, self, :fn
  )

  dsl.instance_exec(&proc) if block_given?
  dsl.tags(["action"])
  dsl.freeze(false)
  dsl.object.set_as_compiled(false)

  Ree.container.compile(dsl.package, name)
end

#method_added(method_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb', line 35

def method_added(method_name)
  return super if method_name != :call

  if @__original_call_defined
    remove_instance_variable(:@__original_call_defined)
    return
  end

  @__original_call_defined = true

  alias_method(:__original_call, :call)

  define_method :call do |user_access, attrs, &proc|
    if self.class.const_defined?(:ActionCaster)
      caster = self.class.const_get(:ActionCaster)

      if !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

    __original_call(user_access, attrs, &proc)
  end

  nil
end