Skip to content

Multiple session objects

printercu edited this page Nov 18, 2017 · 1 revision

Sometimes you may want to have several session objects. For example, in group chat one session object tracks data which is about the chat and other session tracks user-specific data:

protected

# Default `#session` tracks user-specific data.
def session_key
  "#{bot.username}:#{from['id']}" if from
end

# It defines one more session object.
# This one will hold same data for all updates in group chat.
def chat_session
  @_chat_session ||= self.class.build_session(chat && "#{bot.username}:#{chat['id']}")
end

# Note that you need to save it yourself, like it done in Session module:
def process_action(*)
  super
ensure
  chat_session.commit if @_chat_session
end
Clone this wiki locally