class Tengine::Core::SessionWrapper

Public Class Methods

new(source, options = {}) click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 8
def initialize(source, options = {})
  @options = options || {}
  @source = source
end

Public Instance Methods

[](key) click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 17
def [](key)
  @source.properties[key.to_s]
end
clear() click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 35
def clear
  @source.reload
  @source.clear
end
clear!() click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 39
def clear!
  clear
  @source.save!
end
reload() click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 30
def reload
  @source.reload
end
system_properties() click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 13
def system_properties
  @source.system_properties
end
system_update(*args, &block) click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 26
def system_update(*args, &block)
  __update__(:system_properties, *args, &block)
end
update(*args, &block) click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 21
def update(*args, &block)
  return if @options[:ignore_update]
  __update__(:properties, *args, &block)
end

Private Instance Methods

__find_and_modify__(target_name, values) click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 73
def __find_and_modify__(target_name, values)
  result = Tengine::Core::Session.where({
      :_id => @source.id, :lock_version => @source.lock_version
  }).find_and_modify({
      "$set" => { target_name => values, :lock_version => @source.lock_version + 1}
  }, new: true)
  result
end
__get_properties__(target_name, reload = false) click to toggle source

テストで同時に値を取得したことを再現するために、 データを取得するメソッドで待ち合わせするフックとなるようにメソッドに分けています

# File lib/tengine/core/session_wrapper.rb, line 68
def __get_properties__(target_name, reload = false)
  @source.reload if reload
  @source.send(target_name)
end
__update__(target_name, *args) { |values| ... } click to toggle source
# File lib/tengine/core/session_wrapper.rb, line 46
def __update__(target_name, *args, &block)
  if block_given?
    options = args.extract_options!
    retry_count = options[:retry] || 5
    idx = 1
    while idx <= retry_count
      values = ActiveSupport::HashWithIndifferentAccess.new(__get_properties__(target_name, idx > 0))
      yield(values)
      return if __find_and_modify__(target_name, values)
      idx += 1
    end
    raise Tengine::Core::OptimisticLock::RetryOverError, "retried #{retry_count} times but failed to update"
  else
    properties = args.first
    new_vals = __get_properties__(target_name).merge(properties.stringify_keys)
    @source.send("#{target_name}=", new_vals)
    @source.save!
  end
end