Class: Cuttlebone::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/cuttlebone/controller.rb

Overview

NOTE: we don’t really want to pollute this proxy with a lot of private (internal) methods to leave space for <<context>>‘s real methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, context) ⇒ Controller

Returns a new instance of Controller.



8
9
10
11
12
13
# File 'lib/cuttlebone/controller.rb', line 8

def initialize session, context
  @session    = session
  @context    = context
  @definition = Cuttlebone.definitions.find { |d| d.match(context) }
  raise Cuttlebone::InvalidContextError, context, "No definiton was found for #{context.inspect}!" unless @definition
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



57
58
59
60
# File 'lib/cuttlebone/controller.rb', line 57

def method_missing method_name, *args, &block
  return(@context.send(method_name, *args, &block)) if @context.respond_to?(method_name)
  return(super)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/cuttlebone/controller.rb', line 6

def context
  @context
end

#definitionObject (readonly)

Returns the value of attribute definition.



6
7
8
# File 'lib/cuttlebone/controller.rb', line 6

def definition
  @definition
end

#sessionObject (readonly)

Returns the value of attribute session.



6
7
8
# File 'lib/cuttlebone/controller.rb', line 6

def session
  @session
end

Instance Method Details

#add(context) ⇒ Object



45
46
47
# File 'lib/cuttlebone/controller.rb', line 45

def add context
  __save_next_action! :add, context
end

#dropObject



41
42
43
# File 'lib/cuttlebone/controller.rb', line 41

def drop
  __save_next_action! :drop
end

#output(*texts) ⇒ Object



53
54
55
# File 'lib/cuttlebone/controller.rb', line 53

def output *texts
  @output += texts.map(&:to_s)
end

#process(command) ⇒ Object

Processes a command on its context’s domain.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cuttlebone/controller.rb', line 18

def process command
  @next_action = nil
  @output = []

  return([:self, self, [], nil]) if command.empty?
  block, arguments = definition.proc_for(command)

  instance_exec(*arguments, &block)
  action, context = @next_action || [ :self, self ]

  return([ action, context, @output, nil ])
rescue Cuttlebone::DoubleActionError
  raise
rescue => e
  return([ :self, self, [], %{Cuttlebone::Controller: #{e.message} (#{e.class})} ])
end

#promptObject



35
36
37
38
39
# File 'lib/cuttlebone/controller.rb', line 35

def prompt
  return(instance_exec(&@definition.prompt) || '')
rescue => e
  %{error: #{e.message} (#{e.class.name})}
end

#replace(context) ⇒ Object



49
50
51
# File 'lib/cuttlebone/controller.rb', line 49

def replace context
  __save_next_action! :replace, context
end