Last active
August 29, 2015 14:01
-
-
Save sweetpi/492d63290260823cef6c to your computer and use it in GitHub Desktop.
providing actions
This file contains 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
class AnswerActionHandler extends env.actions.ActionHandler | |
constructor: (@framework) -> | |
executeAction: (simulate) => | |
if simulate | |
return Promise.resolve(__("would log 42")) | |
else | |
env.logger.info "42" | |
return Promise.resolve(__("logged 42")) |
This file contains 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
class AnswerActionProvider extends env.actions.ActionProvider | |
constructor: (@framework) -> # nop | |
# ### executeAction() | |
### | |
This function handles action in the form of `the answer` | |
### | |
parseAction: (input, context) => | |
# `input` is the string that could be an action that should be handled by the provider. | |
# With the matcher you can easily check if the input matches some strings: | |
m = M(input, context) | |
.match("the ", optional: yes) | |
.match("answer") | |
# If we have a match | |
if m.hadMatch() | |
match = m.getFullMatch() | |
# then return a info object with some infos | |
# and a corresponding ActionHandler | |
return { | |
token: match | |
nextInput: input.substring(match.length) | |
actionHandler: new AnswerActionHandler(@framework) | |
} | |
else | |
return null |
This file contains 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
class YourPlugin extends env.plugins.Plugin | |
init: (app, @framework, config) => | |
# [...] | |
@framework.ruleManager.addActionProvider(new AnswerActionProvider(@framework)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment