Sha256: 27858171a21decbcc1ba10358b7ae74da481c90f73087a3913fed916cf1fc158

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Abilities
  class Definitions

    def initialize(actor, &block)
      @actor = actor
      Proxy.new(actor, self, &block)
    end

    def add(actions, subjects, behavior, &block)
      actions = [actions] unless actions.is_a? Array
      subjects = [subjects] unless subjects.is_a? Array
      subjects.each do |subject|
        actions.each do |action|
          (all[find_subject_class(subject)] ||= {})[action.to_s] = block_given? ? block : behavior
        end
      end
    end

    def can?(action, subject)
      if actions = all[find_subject_class(subject)]
        if behavior = actions[action.to_s]
          if behavior.is_a? Proc
            @actor.instance_exec subject, &behavior
          else
            behavior
          end
        else
          false
        end
      else
        false
      end
    end

    def cannot?(*args)
      !can?(*args)
    end

    protected

    def all
      @all ||= {}
    end

    def find_subject_class(subject)
      if subject.is_a? Class
        subject.name
      else
        subject.class.name
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
abilities-0.0.1 lib/abilities/definitions.rb