Sha256: 0951affb0e10e120f1a09b81db2c1686e1a3748cff3ad4f52cebf758ab8dfa23

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module CanTango
  module PermissionEngine
    class Compiler
      attr_reader :permission, :categories

      def initialize
      end

      def compile! permission
        @permission = permission
        self
      end

      def to_hashie
        Hashie::Mash.new(eval_statements)
      end

      def eval_statements
        {:can => can_eval, :cannot => cannot_eval}
      end

      def can_eval &block
        build_statements :can, &block
      end

      def cannot_eval &block
        build_statements :cannot, &block
      end

      protected

      # build the 'can' or 'cannot' statements to evaluate
      def build_statements method, &block
        return nil if !permission.static_rules.send(method)
        yield statements(method) if !statements(method).empty? && block
        statements(method)
      end

      def statements method
        valid_actions.map do |action|
          statements_string(method, :action => action)
        end.compact.join("\n")
      end

      # returns the targets the permission rule can manage
      # Example: permission.can[:manage]
      # returns ['Article', 'Comment']
      def get_targets method, action
        permission.static_rules.send(method).send(:[], action.to_s)
      end

      # TODO can and cannot should allow for multiple actions!
      # TODO: should be refactored to more like can([:read, :write], [Article, Comment])
      # Example: can(:manage, [Article, Comment])
      def statements_string method, options = {}
       action = options[:action]
       targets = get_targets method, action
       targets ? CanTango::PermissionEngine::Statements.new(method, action, targets).to_code : nil
      end

      def valid_actions
        [:manage, :read, :update, :create, :write]
      end
    end
  end
end
 

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cantango-0.8.0 lib/cantango/permission_engine/compiler.rb