Skip to content

Instantly share code, notes, and snippets.

@rebo
Created January 11, 2013 16:57
Show Gist options
  • Select an option

  • Save rebo/4512274 to your computer and use it in GitHub Desktop.

Select an option

Save rebo/4512274 to your computer and use it in GitHub Desktop.
#
# Simple Context Example:
#
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
require 'hybrid_dci'
Foo = Struct.new(:name) do
include HybridDCI::Data
end
class NestedContexts
include HybridDCI::Context
role :greeter do
contract :name
def say_hi
puts "Hello there! says #{name}"
end
end
role :goodbye_guy do
contract :name
def say_bye
puts "Goodbye! says #{name}"
end
end
def initialize(obja, objb)
bind_roles(:greeter => obja, :goodbye_guy=> objb)
end
def nest_execute
in_context do
greeter(:call).say_hi
goodbye_guy(:call).say_bye
NestedContexts.new( goodbye_guy , greeter ).execute
end
end
def execute
in_context do
greeter(:call).say_hi
goodbye_guy(:call).say_bye
end
end
end
a = Foo.new("bob")
b = Foo.new("john")
context = NestedContexts.new(a,b)
context.nest_execute
# => nil
# >> Hello there! says bob
# >> Goodbye! says john
# >> Hello there! says john
# >> Goodbye! says bob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment