lib/surrounded.rb in surrounded-0.7.3 vs lib/surrounded.rb in surrounded-0.8.0
- old
+ new
@@ -1,47 +1,33 @@
require "surrounded/version"
require "surrounded/context"
+# This module should be added to objects which will enter
+# into context objects.
+#
+# Its main purpose is to keep a reference to the context
+# and to implement method_missing to handle the relationship
+# to other objects in the context.
module Surrounded
- def self.included(klass)
- klass.class_eval {
- extend Surrounded::Contextual
- }
- unless klass.is_a?(Class)
- def klass.extended(object)
- Surrounded.create_surroundings(object)
- end
- end
- end
- def self.extended(object)
- Surrounded.create_surroundings(object)
- end
+ private
- module Contextual
- def new(*args)
- instance = super
- Surrounded.create_surroundings(instance)
- instance
+ def store_context(ctxt, &block)
+ accessor = block.binding.eval('self')
+ if accessor.role_player?(self)
+ surroundings.unshift(ctxt)
end
end
- def store_context(ctxt)
- surroundings.unshift(ctxt)
+ def remove_context(&block)
+ accessor = block.binding.eval('self')
+ if accessor.role_player?(self)
+ surroundings.shift
+ end
end
- def remove_context
- surroundings.shift
- end
-
- private
-
- def self.create_surroundings(obj)
- obj.instance_variable_set(:@__surroundings__, [])
- end
-
def surroundings
- @__surroundings__
+ @__surroundings__ ||= []
end
def context
surroundings.first || NullContext.new
end