README.md in surrounded-0.5.1 vs README.md in surrounded-0.6.0
- old
+ new
@@ -242,21 +242,25 @@
You might find that useful for dynamically defining user interfaces.
Sometimes I'd rather not use this DSL, however. I want to just write regular methods.
-We can do that too. You'll need to opt in to this by specifying `set_methods_as_triggers` for the context class.
+We can do that too. You'll need to opt in to this by specifying `trigger :your_method_name` for the methods you want to use.
```ruby
class MyEnvironment
# other stuff from above is still here...
-
- set_methods_as_triggers
def shove_it
employee.quit
end
+ trigger :shove_it
+
+ # or in Ruby 2.x
+ trigger def shove_it
+ employee.quit
+ end
role :employee do
def quit
say("I'm sick of this place, #{boss.name}!")
stomp
@@ -267,11 +271,11 @@
end
```
This will allow you to write methods like you normally would. They are aliased internally with a prefix and the method name that you use is rewritten to add and remove the context for the objects in this context. The public API of your class remains the same, but the extra feature of wrapping your method is handled for you.
-This will treat all instance methods defined on your context the same way, so be aware of that.
+This works like Ruby's `public`,`protected`, and `private` keywords in that you can send symbols of method names to it. But `trigger` does not alter the parsing of the document like those core keywords do. In other words, you can't merely type `trigger` on one line, and have methods added afterward be treated as trigger methods.
## Where roles exist
By using `Surrounded::Context` you are declaring a relationship between the objects inside playing your defined roles.
@@ -365,11 +369,11 @@
of your context.
Here's how it works:
```ruby
-class ActiviatingAccount
+class ActivatingAccount
extend Surrounded::Context
apply_roles_on(:trigger) # this is the default
# apply_roles_on(:initialize) # set this to apply behavior from the start
@@ -421,16 +425,21 @@
extend Surrounded::Context
apply_roles_on(:trigger) # this is the default
# apply_roles_on(:initialize) # set this to apply behavior from the start
- set_methods_as_triggers # allows you to skip the 'trigger' dsl
-
# set the default role type only for this class
self.default_role_type = :module # also :wrap, :wrapper, or :interface
+ # shortcut initialization code
initialize(:activator, :account)
+ # or handle it yourself
+ def initialize(activator, account)
+ # this must be done to handle the mapping of roles to objects
+ # pass an array of arrays with role name symbol and the object for that role
+ map_roles([[:activator, activator],[:account, account]])
+ end
role :activator do # module by default
def some_behavior; end
end
@@ -461,9 +470,18 @@
end
trigger :some_trigger_method do
activator.some_behavior # behavior always available
end
+
+ trigger def some_other_trigger
+ activator.some_behavior # behavior always available
+ end
+
+ def regular_non_trigger
+ activator.some_behavior # behavior always available with the following line
+ end
+ trigger :regular_non_trigger # turns the method into a trigger
end
```
## Dependencies