lib/surrounded/context/role_map.rb in surrounded-0.9.10 vs lib/surrounded/context/role_map.rb in surrounded-0.9.11
- old
+ new
@@ -1,38 +1,36 @@
require 'triad'
-require 'surrounded/context_errors'
+require 'forwardable'
module Surrounded
module Context
- class InvalidRole < ::Triad::ItemNotPresent; end
-
class RoleMap
+ extend Forwardable
class << self
def from_base(klass=::Triad)
- role_mapper = Class.new(::Surrounded::Context::RoleMap)
- num = __LINE__; role_mapper.class_eval %{
- def container
- @container ||= #{klass}.new
- end
- }, __FILE__, num
- %w{ update each values keys }.each do |meth|
- num = __LINE__; role_mapper.class_eval %{
- def #{meth}(*args, &block)
- container.send(:#{meth}, *args, &block)
- end
- }, __FILE__, num
- end
+ role_mapper = Class.new(self)
+ Surrounded::Exceptions.define(role_mapper, exceptions: :ItemNotPresent, namespace: klass)
+ role_mapper.container_class=(klass)
+ role_mapper.def_delegators :container, :update, :each, :values, :keys
role_mapper
end
+
+ def container_class=(klass)
+ @container_class = klass
+ end
end
+ def container
+ @container ||= self.class.instance_variable_get(:@container_class).new
+ end
+
def role?(role)
keys.include?(role)
end
def role_player?(object)
!values(object).empty?
- rescue ::Triad::ItemNotPresent
+ rescue ::StandardError
false
end
def assigned_player(role)
values(role).first
\ No newline at end of file