Class: Cogy::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cogy/context.rb

Overview

Context represents a particular invocation request of a Command performed by a user. It holds state like the given arguments, options etc. In other words, it provides the context in which a Command should be invoked.

A Context essentially is an HTTP request performed by ‘cogy:cogy` (github.com/skroutz/cogy-bundle) on behalf of the user. You can think of it as the equivalent of the ActionPack’s ‘Request` object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, args, opts, handle, env) ⇒ Context

Note:

By ‘user’ we refer to the user who invoked the command in chat.

Returns a new instance of Context.

Parameters:

  • command (Command)

    the Cogy::Command to be invoked

  • args (Array)

    the arguments as provided by the user

  • opts (Hash)

    the options as provided by the user

  • handle (String)

    the chat handle of the user

  • env (Hash)

    the Cog Relay environment

See Also:



37
38
39
40
41
42
43
44
45
# File 'lib/cogy/context.rb', line 37

def initialize(command, args, opts, handle, env)
  @command = command
  @args = args
  @opts = opts
  @handle = handle
  @env = env

  define_arg_helpers
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


15
16
17
# File 'lib/cogy/context.rb', line 15

def args
  @args
end

#commandCommand (readonly)

Returns:



12
13
14
# File 'lib/cogy/context.rb', line 12

def command
  @command
end

#envHash (readonly)

Returns:

  • (Hash)


24
25
26
# File 'lib/cogy/context.rb', line 24

def env
  @env
end

#handleString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/cogy/context.rb', line 21

def handle
  @handle
end

#optsHash (readonly)

Returns:

  • (Hash)


18
19
20
# File 'lib/cogy/context.rb', line 18

def opts
  @opts
end

Instance Method Details

#invokeObject

Invokes the command pointed by #command.

Returns:

  • (Object)

    the result of the command. This is what will get printed back to the user that invoked the command and is effectively the return value of the command body.



52
53
54
# File 'lib/cogy/context.rb', line 52

def invoke
  instance_eval(&command.handler)
end