Sha256: d54c84b2b5228daa101cf78201ce54bdd93d6e04d743b07275db25c46e676d9b

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

module CanTango
  class PermitEngine < Engine
    module Builder
      class CreatePermitError < StandardError; end;

      class Base
        include CanTango::Helpers::Debug

        attr_accessor :ability

        # creates the factory for the ability
        # note that the ability contains the roles and role groups of the user (or account)
        # @param [Permits::Ability] the ability
        def initialize ability
          @ability = ability
        end

        protected

        def finder
          ns::Finder
        end

        def ns
          self.class.to_s.gsub(/::Builder/, '').constantize
        end

        # Tries to create a new permit for the given name
        # If no permit Class can be found, it should return nil
        # @param [Symbol] the name
        # @return the permit Class or nil if not found
        def create_permit name
          begin
            permit_clazz(name).new ability
          rescue RuntimeError => e
            # puts "Error instantiating Permit instance for #{name}, cause: #{e}" if CanTango.debug?
            nil
          end
        end

        def permit_clazz name
          debug "Permit Finder: #{finder}"
          finder.new(subject, name).get_permit
        end

        # delegate to ability
        [:options, :role_groups, :roles, :subject, :user, :user_account].each do |name|
          define_method name do
            ability.send(name)
          end
        end

        def available_role_groups
          CanTango.config.role_groups
        end

        def available_roles
          CanTango.config.roles
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cantango-0.9.4.7 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4.6 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4.5 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4.3 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4.2 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4.1 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.4 lib/cantango/permit_engine/builder/base.rb
cantango-0.9.3.2 lib/cantango/permit_engine/builder/base.rb