Skip to content

AQAquamarine/aquasync_model

Repository files navigation

aquasync_model

Gem Version

A module to mixin Aquasync Model requirements.

Example

require 'aquasync_model'

class Book
  include Aquasync::Base

  field :name
end

Then it automatically mixin fields, callbacks, validators to satisfy Aquasync Model requirement.

Specs

#before_save
  ust are set when saved
  gid should be lowercase
validation
  does not allow nil for :gid
  does not allow nil for :deviceToken
  does not allow nil for :localTimestamp
  allows lowercase UUID values for :gid
  does not allow not-UUID value for :gid
  allows lowercase UUID values for :deviceToken
  does not allow not-UUID value for :deviceToken
#to_h
  does not include _id
  does not include ust

Fields

# should be UNIX timestamp
# @example 1406697904
field :ust, type: Integer
# should be UNIX timestamp
# @example 1406697904
field :localTimestamp, type: Integer
# should be UUIDv1
# @example 550e8400-e29b-41d4-a716-446655440000
field :gid, type: String
# should be UUIDv1
# @example 550e8400-e29b-41d4-a716-446655440000
field :deviceToken, type: String
# for paranoid deletion
# @example false
field :isDeleted, type: Boolean

Callbacks

before_validation do
  downcase_gid
  downcase_device_token
  set_ust
end

Validations

validates_presence_of :gid
validates_format_of :gid, with: /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
validates_presence_of :ust
validates_presence_of :deviceToken
validates_format_of :deviceToken, with: /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
validates_presence_of :localTimestamp

Aquasync DeltasAggregator methods

def aq_deltas(ust)
    where(:ust.gt => ust)
end

def aq_commit_deltas(deltas)
    deltas.each {|delta| commit_delta(delta) }
end

Aquasync DeltaPack methods

def to_h
  serializable_hash.delete_if {|key| key == "_id" or key == "ust"}
end