Sha256: 585003ff143e0a39a4a8268a9c18497bbe2ff7642c0034308bc6f2d90278b9aa

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'triad'
require 'forwardable'
module Surrounded
  module Context
    class RoleMap
      extend Forwardable

      class << self
        # Get the role map container and provide an alternative if desired
        # Ex: RoleMap.from_base(SomeCustomContainer)
        def from_base(klass=::Triad)
          unless const_defined?(:Container)
            role_mapper = Class.new(self)
            role_mapper.container_class=(klass)
            Surrounded::Exceptions.define(role_mapper, exceptions: :ItemNotPresent, namespace: klass)
            const_set(:Container, role_mapper)
          end
          const_get(:Container)
        end

        def container_class=(klass)
          @container_class = klass
        end
      end

      def_delegators :container, :update, :each, :values, :keys

      def container
        @container ||= self.class.instance_variable_get(:@container_class).new
      end

      # Check if a role exists in the map
      def role?(role)
        keys.include?(role)
      end

      # Check if an object is playing a role in this map
      def role_player?(object)
        !values(object).empty?
      rescue self.container.class::ItemNotPresent
        false
      end

      # Get the object playing the given role
      def assigned_player(role)
        values(role).first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-1.1.0 lib/surrounded/context/role_map.rb