Method: Sohm::Model#save

Defined in:
lib/sohm.rb

#saveObject

Persist the model attributes and update indices and unique indices. The ‘counter`s and `set`s are not touched during save.

Example:

class User < Sohm::Model
  attribute :name
end

u = User.new(:name => "John").save
u.kind_of?(User)
# => true


1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
# File 'lib/sohm.rb', line 1085

def save
  if serial_attributes_changed
    response = script(LUA_SAVE, 1, key,
      sanitize_attributes(serial_attributes).to_msgpack,
      cas_token,
      sanitize_attributes(attributes).to_msgpack)

    if response.is_a?(RuntimeError)
      if response.message =~ /cas_error/
        raise CasViolation
      else
        raise response
      end
    end

    @cas_token = response
    @serial_attributes_changed = false
  else
    redis.call("HSET", key, "_ndata",
               sanitize_attributes(attributes).to_msgpack)
  end

  if model.refresh_indices_inline
    refresh_indices
  end

  return self
end