Module: Ohm::Versioned

Defined in:
lib/ohm/versioned.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object


3
4
5
# File 'lib/ohm/versioned.rb', line 3

def self.included(model)
  model.attribute :_version, ->(x) { x.to_i }
end

Instance Method Details

#saveObject


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ohm/versioned.rb', line 7

def save
  current_version = new? ? 0 : redis.call("HGET", key, :_version).to_i

  if current_version != _version
    raise Ohm::VersionConflict.new(attributes)
  end

  self._version = current_version + 1

  super
end