Sha256: 08c7d1c2d686bea0935e08e5c54aa2fbb38ecbab526bf76dbc31b46f02409df7

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

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

      class Base
        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
          puts "Permit Finder: #{finder}" if CanTango.debug?
          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.8.9.5 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.9.4 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.9.3 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.9.2 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.9.1 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.9 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.8.1 lib/cantango/permit_engine/builder/base.rb
cantango-0.8.8 lib/cantango/permit_engine/builder/base.rb