Class: Aspera::PersistencyActionOnce
- Inherits:
-
Object
- Object
- Aspera::PersistencyActionOnce
- Defined in:
- lib/aspera/persistency_action_once.rb
Overview
Persist data on file system
Instance Method Summary collapse
- #data ⇒ Object
-
#initialize(options) ⇒ PersistencyActionOnce
constructor
A new instance of PersistencyActionOnce.
- #save ⇒ Object
Constructor Details
#initialize(options) ⇒ PersistencyActionOnce
Returns a new instance of PersistencyActionOnce.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/aspera/persistency_action_once.rb', line 16 def initialize() Log.log.debug{"persistency: #{options}"} raise 'options shall be Hash' unless .is_a?(Hash) raise 'mandatory :manager' if [:manager].nil? raise 'mandatory :data' if [:data].nil? raise 'mandatory :id (String)' unless [:id].is_a?(String) raise 'mandatory 1 element in :id' unless [:id].length >= 1 @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
#data ⇒ Object
43 44 45 |
# File 'lib/aspera/persistency_action_once.rb', line 43 def data return @persisted_object end |
#save ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/aspera/persistency_action_once.rb', line 35 def save if @delete_condition.call(@persisted_object) @manager.delete(@object_id) else @manager.put(@object_id, @persist_format.call(@persisted_object)) end end |