Sha256: 33565fe5eef7a79ae9b248588b7af496114b1291c487c1c47774e179db25280e

Contents?: true

Size: 889 Bytes

Versions: 2

Compression:

Stored size: 889 Bytes

Contents

# frozen_string_literal: true

module IAmICan
  module Permission
    module Definition
      def have_permission *actions, obj: nil, remarks: nil
        return unless actions.tap(&:flatten!).first.class.in?([ Symbol, String ])
        objs = obj ? Array(obj) : [nil]
        permissions = actions.product(objs).map { |(p, o)| { action: p, **deconstruct_obj(o), remarks: remarks } }
        definition = _create_permissions(permissions)
        ResultOf.permissions definition, i_am_i_can, given: permissions.map { |pms| pms.values.compact.join('_').to_sym }
      end

      %i[ have_permissions has_permission has_permissions ].each { |aname| alias_method aname, :have_permission }

      def deconstruct_obj(obj)
        i_am_i_can.permission_model.deconstruct_obj(obj)
      end

      def self.extended(kls)
        kls.delegate :deconstruct_obj, to: kls
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i_am_i_can-4.4.1 lib/i_am_i_can/permission/definition.rb
i_am_i_can-4.4.0 lib/i_am_i_can/permission/definition.rb