Class: DirtyPipeline::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/dirty_pipeline/storage.rb

Overview

Storage structure {

status: :errored,
state: {
  field: "value",
},
errors: {
  "<event_id>": {
    error: "RuPost::API::Error",
    error_message: "Timeout error",
    created_at: 2018-01-01T13:22Z
  },
},
events: {
  <event_id>: {
    transition: "Create",
    args: ...,
    changes: ...,
    created_at: ...,
    updated_at: ...,
    attempts_count: 2,
  },
  <event_id>: {...},
}

}

Defined Under Namespace

Classes: InvalidPipelineStorage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, field) ⇒ Storage

Returns a new instance of Storage.



32
33
34
35
36
37
38
# File 'lib/dirty_pipeline/storage.rb', line 32

def initialize(subject, field)
  @subject = subject
  @field = field
  @store = subject.send(@field).to_h
  reset if @store.empty?
  raise InvalidPipelineStorage, store unless valid_store?
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



30
31
32
# File 'lib/dirty_pipeline/storage.rb', line 30

def field
  @field
end

#storeObject (readonly) Also known as: to_h

Returns the value of attribute store.



30
31
32
# File 'lib/dirty_pipeline/storage.rb', line 30

def store
  @store
end

#subjectObject (readonly)

Returns the value of attribute subject.



30
31
32
# File 'lib/dirty_pipeline/storage.rb', line 30

def subject
  @subject
end

Instance Method Details

#commit!(event) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/dirty_pipeline/storage.rb', line 49

def commit!(event)
  store["status"] = event.destination     if event.destination
  store["state"].merge!(event.changes)    unless event.changes.to_h.empty?
  store["errors"][event.id] = event.error unless event.error.to_h.empty?
  store["events"][event.id] = event.data  unless event.data.to_h.empty?
  save!
end

#find_event(event_id) ⇒ Object



57
58
59
60
# File 'lib/dirty_pipeline/storage.rb', line 57

def find_event(event_id)
  return unless (found_event = store.dig("events", event_id))
  Event.new(data: found_event, error: store.dig("errors", event_id))
end

#reset!Object



40
41
42
43
# File 'lib/dirty_pipeline/storage.rb', line 40

def reset!
  reset
  save!
end

#statusObject



45
46
47
# File 'lib/dirty_pipeline/storage.rb', line 45

def status
  store["status"]
end