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(manager:, data:, id:, delete: nil, parse: nil, format: nil, merge: nil) ⇒ PersistencyActionOnce
constructor
A new instance of PersistencyActionOnce.
- #save ⇒ Object
Constructor Details
#initialize(manager:, data:, id:, delete: nil, parse: nil, format: nil, merge: nil) ⇒ PersistencyActionOnce
Returns a new instance of PersistencyActionOnce.
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
#data ⇒ Object
42 43 44 |
# File 'lib/aspera/persistency_action_once.rb', line 42 def data return @persisted_object end |
#save ⇒ Object
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 |