module Surrounded::Context::InstanceMethods

Public Instance Methods

rebind(**options_hash) click to toggle source
# File lib/surrounded/context.rb, line 122
def rebind(**options_hash)
  clear_instance_variables
  begin
    initialize(options_hash)
  rescue ArgumentError
    initialize(*options_hash.values)
  end
  self
end
role?(name, &block) click to toggle source

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.

# File lib/surrounded/context.rb, line 106
def role?(name, &block)
  return false unless role_map.role?(name)
  accessor = block.binding.eval('self')
  role_map.role_player?(accessor) && role_map.assigned_player(name)
end
role_player?(obj) click to toggle source

Check if a given object is a role player in the context.

# File lib/surrounded/context.rb, line 113
def role_player?(obj)
  role_map.role_player?(obj)
end
triggers() click to toggle source

Return a Set of all defined triggers

# File lib/surrounded/context.rb, line 118
def triggers
  self.class.triggers
end

Private Instance Methods

apply_behavior(role, behavior, object) click to toggle source
# File lib/surrounded/context.rb, line 169
def apply_behavior(role, behavior, object)
  if behavior && role_const_defined?(behavior)
    applicator = if self.respond_to?("apply_behavior_#{role}")
                    method("apply_behavior_#{role}")
                  elsif role_const(behavior).is_a?(Class)
                    method(:apply_class_behavior)
                  else
                    method(:apply_module_behavior)
                  end

    role_player = applicator.call(role_const(behavior), object)
    map_role(role, behavior, role_player)
  end
  role_player || object
end
apply_behaviors() click to toggle source
# File lib/surrounded/context.rb, line 215
def apply_behaviors
  role_map.each do |role, mod_name, object|
    player = apply_behavior(role, mod_name, object)
    if player.respond_to?(:store_context, true)
      player.__send__(:store_context) do; end
    end
  end
end
apply_class_behavior(klass, obj) click to toggle source
# File lib/surrounded/context.rb, line 193
def apply_class_behavior(klass, obj)
  wrapper_name = wrap_methods.find{|meth| klass.respond_to?(meth) }
  return obj if !wrapper_name
  klass.method(wrapper_name).call(obj)
end
apply_module_behavior(mod, obj) click to toggle source
# File lib/surrounded/context.rb, line 185
def apply_module_behavior(mod, obj)
  adder_name = module_extension_methods.find{|meth| obj.respond_to?(meth) }
  return obj unless adder_name

  obj.method(adder_name).call(mod)
  obj
end
clear_instance_variables() click to toggle source
# File lib/surrounded/context.rb, line 134
def clear_instance_variables
  instance_variables.each{|ivar| remove_instance_variable(ivar) }
end
map_role(role, mod_name, object) click to toggle source
# File lib/surrounded/context.rb, line 164
def map_role(role, mod_name, object)
  instance_variable_set("@#{role}", object)
  role_map.update(role, role_module_basename(mod_name), object)
end
map_role_collection(role, mod_name, collection) click to toggle source
# File lib/surrounded/context.rb, line 154
def map_role_collection(role, mod_name, collection)
  singular_role_name = singularize_name(role)
  singular_behavior_name = singularize_name(role_behavior_name(role))
  if collection.respond_to?(:each_with_index) && role_const_defined?(singular_behavior_name)
    collection.each_with_index do |item, index|
      map_role(:"#{singular_role_name}_#{index + 1}", singular_behavior_name, item)
    end
  end
end
map_roles(role_object_array) click to toggle source
# File lib/surrounded/context.rb, line 142
def map_roles(role_object_array)
  detect_collisions role_object_array
  role_object_array.to_a.each do |role, object|
    if self.respond_to?("map_role_#{role}")
      self.send("map_role_#{role}", object)
    else
      map_role(role, role_behavior_name(role), object)
      map_role_collection(role, role_behavior_name(role), object)
    end
  end
end
module_extension_methods() click to toggle source

List of possible methods to use to add behavior to an object from a module.

# File lib/surrounded/context.rb, line 234
def module_extension_methods
  [:cast_as, :extend]
end
module_removal_methods() click to toggle source

List of possible methods to use to remove behavior from an object with a module.

# File lib/surrounded/context.rb, line 244
def module_removal_methods
  [:uncast]
end
remove_behavior(role, behavior, object) click to toggle source
# File lib/surrounded/context.rb, line 199
def remove_behavior(role, behavior, object)
  if behavior && role_const_defined?(behavior)
    remover_name = (module_removal_methods + unwrap_methods).find do |meth|
      object.respond_to?(meth)
    end
  end

  role_player = if self.respond_to?("remove_behavior_#{role}")
                  self.send("remove_behavior_#{role}", role_const(behavior), object)
                elsif remover_name
                  object.send(remover_name)
                end

  role_player || object
end
remove_behaviors() click to toggle source
# File lib/surrounded/context.rb, line 224
def remove_behaviors
  role_map.each do |role, mod_name, player|
    if player.respond_to?(:remove_context, true)
      player.__send__(:remove_context) do; end
    end
    remove_behavior(role, mod_name, player)
  end
end
role_behavior_name(role) click to toggle source
# File lib/surrounded/context.rb, line 253
def role_behavior_name(role)
  RoleName.new(role)
end
role_const(name) click to toggle source
# File lib/surrounded/context.rb, line 261
def role_const(name)
  self.class.send(:role_const, name)
end
role_const_defined?(name) click to toggle source
# File lib/surrounded/context.rb, line 265
def role_const_defined?(name)
  self.class.send(:role_const_defined?, name)
end
role_map() click to toggle source
# File lib/surrounded/context.rb, line 138
def role_map
  @role_map ||= role_mapper_class.new
end
role_mapper_class() click to toggle source
# File lib/surrounded/context.rb, line 269
def role_mapper_class
  self.class.send(:role_mapper_class)
end
role_module_basename(mod) click to toggle source
# File lib/surrounded/context.rb, line 257
def role_module_basename(mod)
  mod.to_s.split('::').last
end
singularize_name(name) click to toggle source
# File lib/surrounded/context.rb, line 273
def singularize_name(name)
  if name.respond_to?(:singularize)
    name.singularize
  else
    # good enough for now but should be updated with better rules
    name.to_s.tap do |string|
      if string =~ /ies\z/
        string.sub!(/ies\z/,'y')
      elsif string =~ /s\z/
        string.sub!(/s\z/,'')
      end
    end
  end
end
unwrap_methods() click to toggle source

List of possible methods to use to remove behavior from an object with a wrapper.

# File lib/surrounded/context.rb, line 249
def unwrap_methods
  [:__getobj__]
end
wrap_methods() click to toggle source

List of possible methods to use to add behavior to an object from a wrapper.

# File lib/surrounded/context.rb, line 239
def wrap_methods
  [:new]
end