lib/surrounded/context.rb in surrounded-0.9.1 vs lib/surrounded/context.rb in surrounded-0.9.2
- old
+ new
@@ -86,10 +86,18 @@
if role_const_defined?(name)
const_get(name)
end
end
+ # Allow alternative implementations for the role map
+ # This requires that the specified mapper klass have an
+ # initializer method called 'from_base' which accepts a
+ # class name used to initialize the base object
+ def role_mapper_class(mapper: RoleMap, base: ::Triad)
+ @role_mapper_class ||= mapper.from_base(base)
+ end
+
module InstanceMethods
# Check whether a given name is a role inside the context.
# The provided block is used to evaluate whether or not the caller
# is allowed to inquire about the roles.
def role?(name, &block)
@@ -109,11 +117,11 @@
end
private
def role_map
- @role_map ||= RoleMap.new
+ @role_map ||= role_mapper_class.new
end
def map_roles(role_object_array)
role_object_array.each do |role, object|
if self.respond_to?("map_role_#{role}")
@@ -187,18 +195,18 @@
end
def apply_behaviors
role_map.each do |role, mod_name, object|
player = apply_behavior(role, mod_name, object)
- player.send(:store_context, self) do; end
+ player.__send__(:store_context) do; end
end
end
def remove_behaviors
role_map.each do |role, mod_name, player|
if player.respond_to?(:remove_context, true)
- player.send(:remove_context) do; end
+ player.__send__(:remove_context) do; end
end
remove_behavior(role, mod_name, player)
end
end
@@ -221,11 +229,11 @@
def unwrap_methods
[:__getobj__]
end
def role_behavior_name(role)
- role.to_s.gsub(/(?:^|_)([a-z])/) { $1.upcase }.sub(/_\d+/,'')
+ RoleName.new(role)
end
def role_module_basename(mod)
mod.to_s.split('::').last
end
@@ -236,10 +244,14 @@
def role_const_defined?(name)
self.class.send(:role_const_defined?, name)
end
+ def role_mapper_class
+ self.class.send(:role_mapper_class)
+ end
+
def singularize_name(name)
if name.respond_to?(:singularize)
name.singularize
else
# good enough for now but should be updated with better rules
@@ -249,9 +261,31 @@
elsif string =~ /s\z/
string.sub!(/s\z/,'')
end
end
end
+ end
+ end
+
+ class RoleName
+ def initialize(string, suffix=nil)
+ @string = string.
+ to_s.
+ split(/_/).
+ map{|part|
+ part.capitalize
+ }.
+ join.
+ sub(/_\d+/,'') + suffix.to_s
+ end
+
+ def to_str
+ @string
+ end
+ alias to_s to_str
+
+ def to_sym
+ @string.to_sym
end
end
end
end
\ No newline at end of file