lib/gloo/verbs/tell.rb in gloo-0.6.1 vs lib/gloo/verbs/tell.rb in gloo-0.7.0
- old
+ new
@@ -9,25 +9,22 @@
class Tell < Gloo::Core::Verb
KEYWORD = 'tell'.freeze
KEYWORD_SHORT = '->'.freeze
TO = 'to'.freeze
+ OBJ_NOT_FOUND_ERR = 'Object was not found: '.freeze
+ UNKNOWN_MSG_ERR = 'Missing message!'.freeze
#
# Run the verb.
#
def run
- name = @tokens.second
- msg = @tokens.after_token( TO )
- pn = Gloo::Core::Pn.new name
- o = pn.resolve
+ setup_msg
+ return unless @msg
- if o
- Gloo::Exec::Dispatch.message msg, o, @params
- else
- $log.error "Could not send message to object. Bad path: #{name}"
- end
+ setup_target
+ dispatch_msg
end
#
# Get the Verb's keyword.
#
@@ -41,38 +38,41 @@
def self.keyword_shortcut
return KEYWORD_SHORT
end
# ---------------------------------------------------------------------
- # Help
+ # Private functions
# ---------------------------------------------------------------------
+ private
+
#
- # Get help for this verb.
+ # Lookup the message to send.
#
- def self.help
- return <<~TEXT
- TELL VERB
- NAME: tell
- SHORTCUT: ->
+ def setup_msg
+ @msg = @tokens.after_token( TO )
- DESCRIPTION
- Send a message to an object.
- Ask the object to perform an action.
+ $engine.err( UNKNOWN_MSG_ERR ) unless @msg
+ end
- SYNTAX
- tell <path.to.object> to <message>
+ #
+ # Setup the target of the message.
+ #
+ def setup_target
+ @obj_name = @tokens.second
+ pn = Gloo::Core::Pn.new @obj_name
+ @target_obj = pn.resolve
+ end
- PARAMETERS
- path.to.object - The object that we want to see.
- message - The message to send.
-
- RESULT
- The result depends on the message that is sent.
-
- ERRORS
- Errors depend on the message that is sent.
- TEXT
+ #
+ # Dispatch the message to the target object.
+ #
+ def dispatch_msg
+ if @target_obj
+ Gloo::Exec::Dispatch.message @msg, @target_obj, @params
+ else
+ $engine.err "#{OBJ_NOT_FOUND_ERR} #{@obj_name}"
+ end
end
end
end
end