Class: Nochmal::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nochmal/adapters/base.rb

Overview

base-class for storage-adapters

currently, it provides a comon interface and some support for STI

Direct Known Subclasses

ActiveStorage, Carrierwave

Instance Method Summary collapse

Constructor Details

#initialize(from: nil, to: nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
# File 'lib/nochmal/adapters/base.rb', line 9

def initialize(from: nil, to: nil)
  @from = from
  @to = to

  @types = {}
  @uploaders = {}

  validate
end

Instance Method Details

#attachment_types_for(_model) ⇒ Object



31
32
33
# File 'lib/nochmal/adapters/base.rb', line 31

def attachment_types_for(_model)
  raise "Return an Array of attachment type (e.g. avatar, embeds) this in your adapter-subclass"
end

#blob(_attachment) ⇒ Object



44
45
46
# File 'lib/nochmal/adapters/base.rb', line 44

def blob(_attachment)
  raise "Return the data of the attachment in your adapter-subclass"
end

#collection(_model, _type) ⇒ Object



35
36
37
# File 'lib/nochmal/adapters/base.rb', line 35

def collection(_model, _type)
  raise "Return a Scope or Enumerable of Records with attachments of a type in your adapter-subclass"
end

#countObject



81
82
83
# File 'lib/nochmal/adapters/base.rb', line 81

def count
  { status: :ok }
end

#empty_collection?(model, type) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/nochmal/adapters/base.rb', line 39

def empty_collection?(model, type)
  model.count.zero? || # no records
    collection(model, type).count.zero? # no uploads of a type
end

#from_storage_service(service = @to) ⇒ Object



25
# File 'lib/nochmal/adapters/base.rb', line 25

def from_storage_service(service = @to); end

#general_notesObject

called after all reuploading/listing/counting



57
# File 'lib/nochmal/adapters/base.rb', line 57

def general_notes; end

#item_completed(_item, _type, _status) ⇒ Object

called after reuploading/listing/counting each record/attachment



69
# File 'lib/nochmal/adapters/base.rb', line 69

def item_completed(_item, _type, _status); end

#list(attachment) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/nochmal/adapters/base.rb', line 85

def list(attachment)
  filename = blob(attachment)

  Output.attachment(filename.try(:key) || filename)

  { status: :noop }
end

#model_completed(_model) ⇒ Object

called after each model (class)



63
# File 'lib/nochmal/adapters/base.rb', line 63

def model_completed(_model); end

#models_with_attachmentsObject



27
28
29
# File 'lib/nochmal/adapters/base.rb', line 27

def models_with_attachments
  raise "Return an Array of model-classes in your adapter-subclass"
end

#reupload(_attachment, _type) ⇒ Object

actions



73
74
75
76
77
78
79
# File 'lib/nochmal/adapters/base.rb', line 73

def reupload(_attachment, _type)
  raise <<~ERROR
    Upload the attachment (of a certain type) NOCHMAL!!! in your adapter subclass

    Please return a Hash with a least a :status-key. If everything is ok, I suggest :ok as value.
  ERROR
end

#setup(_reupload_mode) ⇒ Object

called before doing any action or even lookup



51
# File 'lib/nochmal/adapters/base.rb', line 51

def setup(_reupload_mode); end

#teardownObject

called after outputing the final notes, before returning from the last method



54
# File 'lib/nochmal/adapters/base.rb', line 54

def teardown; end

#to_storage_service(service = @from) ⇒ Object



23
# File 'lib/nochmal/adapters/base.rb', line 23

def to_storage_service(service = @from); end

#type_completed(_model, _type) ⇒ Object

called after handling each type (uploader/attachment-type)



66
# File 'lib/nochmal/adapters/base.rb', line 66

def type_completed(_model, _type); end

#type_notes(_model, _type) ⇒ Object

called before uploading a type



60
# File 'lib/nochmal/adapters/base.rb', line 60

def type_notes(_model, _type); end

#validateObject



19
20
21
# File 'lib/nochmal/adapters/base.rb', line 19

def validate
  true
end