Class: Telegem::Core::Scene

Inherits:
Object
  • Object
show all
Defined in:
lib/core/scene.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, default_step: :start, &block) ⇒ Scene

Returns a new instance of Scene.



6
7
8
9
10
11
12
13
# File 'lib/core/scene.rb', line 6

def initialize(id, default_step: :start, &block)
  @id = id
  @steps = {}
  @enter_callbacks = []
  @leave_callbacks = []
  @default_step = default_step
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#enter_callbacksObject (readonly)

Returns the value of attribute enter_callbacks.



4
5
6
# File 'lib/core/scene.rb', line 4

def enter_callbacks
  @enter_callbacks
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/core/scene.rb', line 4

def id
  @id
end

#leave_callbacksObject (readonly)

Returns the value of attribute leave_callbacks.



4
5
6
# File 'lib/core/scene.rb', line 4

def leave_callbacks
  @leave_callbacks
end

#stepsObject (readonly)

Returns the value of attribute steps.



4
5
6
# File 'lib/core/scene.rb', line 4

def steps
  @steps
end

Instance Method Details

#current_step(ctx) ⇒ Object



67
68
69
# File 'lib/core/scene.rb', line 67

def current_step(ctx)
  ctx.scene[:step] if ctx.scene && ctx.scene[:id] == @id 
end

#enter(ctx, step_name = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core/scene.rb', line 30

def enter(ctx, step_name = nil)
  Async do
    step_name ||= @default_step
    ctx.scene = { id: @id, step: step_name.to_sym }
    @enter_callbacks.each { |cb| await(cb.call(ctx)) }
    await process_step(ctx, step_name)
  rescue => e
    ctx.logger.error("Error entering scene #{@id}: #{e.message}") 
    raise 
  end
end

#leave(ctx) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/core/scene.rb', line 42

def leave(ctx) 
  Async do 
    @leave_callbacks.each { |cb| await(cb.call(ctx)) }
    ctx.scene = nil 
  rescue => e
    ctx.logger.error("Error leaving scene #{@id}: #{e.message}")
    raise 
  end
end

#on_enter(&callback) ⇒ Object



20
21
22
23
# File 'lib/core/scene.rb', line 20

def on_enter(&callback)
  @enter_callbacks << callback
  self
end

#on_leave(&callback) ⇒ Object



25
26
27
28
# File 'lib/core/scene.rb', line 25

def on_leave(&callback)
  @leave_callbacks << callback
  self
end

#process_step(ctx, step_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/core/scene.rb', line 52

def process_step(ctx, step_name)
  Async do 
    step = @steps[step_name.to_sym] 
    raise "Unknown step #{step_name} in scene #{@id}" unless step 

    result = step.call(ctx)
    result = await(result) if result.is_a?(Async::Task)
    ctx.scene[:step] = step_name.to_sym
    result
  rescue => e
    ctx.logger.error("Error processing step #{step_name} in scene #{@id}: #{e.message}")
    raise 
  end
end

#reset(ctx) ⇒ Object



71
72
73
74
# File 'lib/core/scene.rb', line 71

def reset(ctx)
  ctx.scene = { id: @id, step: @default_step }
  self
end

#step(name, &action) ⇒ Object



15
16
17
18
# File 'lib/core/scene.rb', line 15

def step(name, &action)
  @steps[name.to_sym] = action
  self
end

#to_sObject



76
77
78
# File 'lib/core/scene.rb', line 76

def to_s
  "#<Scene #{@id}>"
end