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(options) ⇒ 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)

  • :ids

    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



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

def initialize(options)
  Log.log.debug("persistency: #{options}")
  raise "options shall be Hash" unless options.is_a?(Hash)
  raise "mandatory :manager" if options[:manager].nil?
  raise "mandatory :data" if options[:data].nil?
  raise "mandatory :ids (Array)" unless options[:ids].is_a?(Array)
  raise "mandatory 1 element in :ids" unless options[:ids].length >= 1
  @manager=options[:manager]
  @persisted_object=options[:data]
  @object_ids=options[:ids]
  # by default , at save time, file is deleted if data is nil
  @delete_condition=options[:delete] || lambda{|d|d.empty?}
  @persist_format=options[:format] || lambda {|h| JSON.generate(h)}
  persist_parse=options[:parse] || lambda {|t| JSON.parse(t)}
  persist_merge=options[:merge] || lambda {|current,file| current.concat(file).uniq rescue current}
  value=@manager.get(@object_ids)
  persist_merge.call(@persisted_object,persist_parse.call(value)) unless value.nil?
end

Instance Method Details

#saveObject



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

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