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.



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

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

Instance Attribute Details

#enter_callbacksObject (readonly)

Returns the value of attribute enter_callbacks.



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

def enter_callbacks
  @enter_callbacks
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#leave_callbacksObject (readonly)

Returns the value of attribute leave_callbacks.



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

def leave_callbacks
  @leave_callbacks
end

#stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

Instance Method Details

#current_step(ctx) ⇒ Object



91
92
93
94
# File 'lib/core/scene.rb', line 91

def current_step(ctx)
  scene_data = ctx.session[:telegem_scene]
  scene_data[:step] if scene_data
end

#enter(ctx, step_name = nil, **initial_data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/core/scene.rb', line 39

def enter(ctx, step_name = nil, **initial_data)
  step_name ||= @default_step
  

  ctx.session[:telegem_scene] = {
    id: @id.to_s,
    step: step_name.to_s,
    data: initial_data,
    entered_at: Time.now.to_i,
    timeout: @timeout,
    waiting_for_response: false,
    last_question: nil
  }
  
  ctx.instance_variable_set(:@current_scene, self)

  @enter_callbacks.each { |cb| cb.call(ctx) }
  

  execute_step(ctx, step_name)
end

#leave(ctx, reason = :manual) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/core/scene.rb', line 80

def leave(ctx, reason = :manual)
  scene_data = ctx.session.delete(:telegem_scene)
  ctx.instance_variable_set(:@current_scene, nil)
  

  @leave_callbacks.each { |cb| cb.call(ctx, reason, scene_data[:data]) }
  
  scene_data[:data]
end

#on_enter(&callback) ⇒ Object



23
24
25
26
# File 'lib/core/scene.rb', line 23

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

#on_leave(&callback) ⇒ Object



28
29
30
31
# File 'lib/core/scene.rb', line 28

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

#process(ctx) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/core/scene.rb', line 62

def process(ctx)
  scene_data = ctx.session[:telegem_scene]
  return unless scene_data
  

  if Time.now.to_i - scene_data[:entered_at] > scene_data[:timeout]
    leave(ctx, :timeout)
    return
  end
  
  if scene_data[:waiting_for_response] && ctx.message&.text
    process_response(ctx, scene_data)
  else

    execute_step(ctx, scene_data[:step])
  end
end

#scene_data(ctx) ⇒ Object



97
98
99
# File 'lib/core/scene.rb', line 97

def scene_data(ctx)
  ctx.session[:telegem_scene]&.[](:data) || {}
end

#step(name, &action) ⇒ Object



18
19
20
21
# File 'lib/core/scene.rb', line 18

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

#timeout(seconds) ⇒ Object



33
34
35
36
# File 'lib/core/scene.rb', line 33

def timeout(seconds)
  @timeout = seconds
  self
end