Skip to content

Instantly share code, notes, and snippets.

@rebo
Forked from runefs/gist:4338821
Created December 19, 2012 20:04
Show Gist options
  • Save rebo/4339983 to your computer and use it in GitHub Desktop.
Save rebo/4339983 to your computer and use it in GitHub Desktop.
require 'alias_dci'
class A
include AliasDCI::DataObject
def foo
"a"
end
end
class C
include AliasDCI::Context # No need to also include DataObject in a context, it is one anyway
role :y do
contract :foo
end
role :z do
def foo
"z"
end
end
def initialize(some_t, some_k)
assign_named_roles(:y => some_t, :z => some_k)
end
def print
in_context do
puts "foo of y " + (r_y.foo.to_s) # explictly using r_rolename as the label as role y, requires instance #foo.
puts "foo of z " + (z.foo.to_s)
end
end
def foo
"context"
end
def rebind(a)
assign_named_roles(:y => a, :z => self) # initialize is only really called on Class#new, its kinda Ruby's constructor method
print
assign_named_roles(:y => self, :z => a)
end
end
def do_it(some_t, some_k)
c = C.new(some_t, some_k)
c.print
c.rebind(some_t)
c.print
end
a = A.new
do_it(a, a)
# => nil
# outputs :
# >> foo of y a
# >> foo of z z
#each object is only playing one role
# >> foo of y a
# >> foo of z z
#again one role for each object
# >> foo of y context
# >> foo of z z
#matches this :
#foo of y a
#foo of z z
#foo of y a
#foo of z z
#foo of y context
#foo of z z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment