Class: Aspera::PersistencyActionOnce

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/persistency_action_once.rb

Overview

Persist data on file system

Instance Method Summary collapse

Constructor Details

#initialize(manager:, data:, id:, delete: nil, parse: nil, format: nil, merge: nil) ⇒ PersistencyActionOnce

Returns a new instance of PersistencyActionOnce.

Parameters:

  • :manager

    Mandatory Database

  • :data

    Mandatory object to persist, must be same object from begin to end (assume array by default)

  • :id

    Mandatory identifiers

  • :delete

    Optional delete persistency condition

  • :parse

    Optional parse method (default to JSON)

  • :format

    Optional dump method (default to JSON)

  • :merge

    Optional merge data from file to current data



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aspera/persistency_action_once.rb', line 17

def initialize(manager:, data:, id:, delete: nil, parse: nil, format: nil, merge: nil)
  Aspera.assert(!manager.nil?)
  Aspera.assert(!data.nil?)
  Aspera.assert_type(id, String)
  Aspera.assert(!id.empty?)
  @manager = manager
  @persisted_object = data
  @object_id = id
  # by default , at save time, file is deleted if data is nil
  @delete_condition = delete || lambda{|d|d.empty?}
  @persist_format = format || lambda {|h| JSON.generate(h)}
  persist_parse = parse || lambda {|t| JSON.parse(t)}
  persist_merge = merge || lambda {|current, file| current.concat(file).uniq rescue current}
  value = @manager.get(@object_id)
  persist_merge.call(@persisted_object, persist_parse.call(value)) unless value.nil?
end

Instance Method Details

#dataObject



42
43
44
# File 'lib/aspera/persistency_action_once.rb', line 42

def data
  return @persisted_object
end

#saveObject



34
35
36
37
38
39
40
# File 'lib/aspera/persistency_action_once.rb', line 34

def save
  if @delete_condition.call(@persisted_object)
    @manager.delete(@object_id)
  else
    @manager.put(@object_id, @persist_format.call(@persisted_object))
  end
end