class DeltaPackBuilder

Has a responsibility to build a DeltaPack hash. @author kaiinui @example

builder = DeltaPackBuilder.new
builder.push_documents(Book.all)
builder.push_documents(Author.all)
builder.delta_pack

@note Please implement #_name and to_h to Models. (DeltaPackBuilder requirements.)

Attributes

delta_pack[RW]

delta_pack [Hash]

Public Class Methods

new() click to toggle source
# File lib/delta_pack/delta_pack_builder.rb, line 16
def initialize
  self.delta_pack = {}
  self.delta_pack["_id"] = uuid
  self.delta_pack["latestUST"] = 0
end

Public Instance Methods

json() click to toggle source

returns built DeltaPack represented in JSON @return [String] JSON string

# File lib/delta_pack/delta_pack_builder.rb, line 53
def json
  delta_pack.to_json
end
latest_ust() click to toggle source

returns latestUST of current DeltaPack @return [Integer]

# File lib/delta_pack/delta_pack_builder.rb, line 47
def latest_ust
  self.delta_pack["latestUST"]
end
pretty_json() click to toggle source

returns built DeltaPack represented in pretty printed JSON @return [String] pretty printed JSON

# File lib/delta_pack/delta_pack_builder.rb, line 59
def pretty_json
  JSON.pretty_generate(delta_pack)
end
push(document) click to toggle source

pushes a document into a DeltaPack. @param document [Aquasync::Base] a resource which inherits Aquasync::Base

# File lib/delta_pack/delta_pack_builder.rb, line 24
def push(document)
  name = document._name
  initialize_array(name)
  self.delta_pack[name].push document.to_h
  update_ust(document)
end
push_documents(documents) click to toggle source

pushes documents into a DeltaPack. @param documents [Array] resources which inherits Aquasync::Base

# File lib/delta_pack/delta_pack_builder.rb, line 33
def push_documents(documents)
  documents.each {|d| push(d)}
end
update_ust(document) click to toggle source

updates latestUST @param document [Aquasync::Base]

# File lib/delta_pack/delta_pack_builder.rb, line 39
def update_ust(document)
  if document.ust > latest_ust
    self.delta_pack["latestUST"] = document.ust
  end
end

Private Instance Methods

initialize_array(name) click to toggle source

@param [String] name model name

# File lib/delta_pack/delta_pack_builder.rb, line 66
def initialize_array(name)
  self.delta_pack[name] = [] unless self.delta_pack[name]
end
uuid() click to toggle source

@return [String] UUIDv1

# File lib/delta_pack/delta_pack_builder.rb, line 71
def uuid
  SimpleUUID::UUID.new.to_guid
end