Sha256: d9e698dfd45cfc09a57bc0082a0462bc599689d217a0d7d55ecc4316c7816c9d

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'sugar-high/class_ext'

module CanTango
  module PermitEngine
    class Factory
      include ClassExt

      attr_accessor :ability, :builders

      # 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

      def build!
        @builders ||= builders.inject([]) do |res, builder|
          res << create_builder(builder).build
          res
        end.flatten.compact
      end

      def create_builder builder
        clazz = builder_class(builder)
        clazz.constantize.new(ability)
      end

      def builder_class builder
        return "CanTango::PermitEngine::Builder::SpecialPermits" if builder == :special
        "CanTango::PermitEngine::#{builder.to_s.camelize}Permit::Builder"
      end

      def builders
        [:role_group, :role, :user, :account, :special]
      end

      def options
        ability.options
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cantango-0.8.0 lib/cantango/permit_engine/factory.rb