Last active
December 10, 2015 23:49
-
-
Save rebo/4512327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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 SimpleContext | |
| 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 | |
| @a_foo = Foo.new("Arthur") | |
| @another_foo = Foo.new("Marvin") | |
| bind_roles(:greeter => @a_foo, :goodbye_guy=> @another_foo) | |
| end | |
| def execute | |
| in_context do | |
| greeter(:call).say_hi | |
| goodbye_guy(:call).say_bye | |
| end | |
| greeter(:call).say_hi ## Raises error as greeter not available in context | |
| end | |
| end | |
| context = SimpleContext.new | |
| context.execute | |
| # => | |
| # >> Hello there! says Arthur | |
| # >> Goodbye! says Marvin | |
| # ~> Role:greeter is not available Current Context (RuntimeError) | |
| # ~> from -:44:in `execute' | |
| # ~> from -:50:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment