Sha256: 96a0316ace250e36d55324e62bca696581dc2114724db3200be2baeb34f9c082

Contents?: true

Size: 1.74 KB

Versions: 32

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true
package_require("ree_mapper/errors/type_error")
package_require("ree_mapper/errors/coercion_error")

module ReeActions
  module DSL
    def self.included(base)
      base.extend(ClassMethods)
      base.include(ReeMapper::DSL)
      base.include(Ree::Inspectable)
    end

    def self.extended(base)
      base.extend(ClassMethods)
      base.include(ReeMapper::DSL)
    end

    module ClassMethods
      include Ree::Contracts::Core
      include Ree::Contracts::ArgContracts

      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

      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
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb