def safe_alias_method_chain(method_name, ext)
class_eval do
method_name_with_ext, method_name_without_ext = Helpers.compose_chaining_symbols(method_name, ext)
instance_variable = Helpers.escape_method_name(method_name_with_ext)
if (method_defined?(method_name_with_ext) || private_method_defined?(method_name_with_ext))
raise(MetaProgramming::AliasMethodChainError, "#{method_name_without_ext} already exists. Circular references not permitted.") if (method_defined?(method_name_without_ext) || private_method_defined?(method_name_without_ext))
raise(MetaProgramming::AliasMethodChainError, "#{method_name_with_ext} already chained. Rechaining not permitted") if eigenclass.instance_variable_defined?("@#{instance_variable}")
if (method_defined?(method_name.to_sym) || private_method_defined?(method_name.to_sym))
alias_method method_name_without_ext, method_name.to_sym
alias_method method_name.to_sym, method_name_with_ext
case method_access_level(method_name_without_ext)
when :public then public(method_name.to_sym)
when :protected then protected(method_name.to_sym)
when :private then private(method_name.to_sym)
end
else
define_method(method_name_without_ext) {|*args| }
alias_method method_name.to_sym, method_name_with_ext
end
eigenclass.instance_variable_set("@#{instance_variable}", true)
end
end
end