Sha256: 0f01213989c4fb9c0bfa82c04fd8e39ace59a2f3319b9457822147a3f8ab3796

Contents?: true

Size: 766 Bytes

Versions: 1

Compression:

Stored size: 766 Bytes

Contents

require "singleton"

module Miau
  class PolicyStorage
    include Singleton

    attr_reader :policies

    def initialize
      @policies = {}
    end

    def run(klass, action, user, resource)
      policy = policy(klass, user, resource)
      raise Miau::NotDefinedError unless policy.respond_to?(action)
      policy.send(action)
    end

    private

    def policy(klass, user, resource)
      result = @policies[klass]
      if result
        result.user = user
        result.resource = resource
        return result
      end

      create_policy(klass, user, resource)
    end

    def create_policy(klass, user, resource)
      str = "#{klass}Policy"
      result = str.constantize.new(user, resource)
      @policies[klass] = result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
miau-0.1.0 lib/miau/storage.rb