Class: Ratonvirus::Storage::Multi

Inherits:
Base
  • Object
show all
Defined in:
lib/ratonvirus/storage/multi.rb

Overview

Multi storage allows the developers to configure multiple storage backends for the application at the same time. For instance, in case the scanner is used for both: scanning the Active Storage resources as well as scanning file paths, they are handled with separate storages.

To configure the Multi-storage with two backends, use the following: Ratonvirus.storage = :multi, [:filepath, :active_storage]

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#asset_path, #asset_remove, #initialize

Constructor Details

This class inherits a constructor from Ratonvirus::Storage::Base

Instance Method Details

#accept?(resource) ⇒ Boolean

Check if any of the storages accept the resource.

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/ratonvirus/storage/multi.rb', line 59

def accept?(resource)
  storage_for(resource) do |_storage|
    return true
  end

  false
end

#changed?(record, attribute) ⇒ Boolean

Fetch the resource from the record using the attribute and check if any storages accept that resource. If an accepting storage is found, only check ‘changed?` against that storage. Otherwise, call the `changed?` method passing both given parameters for all storages in order and return in case one of them reports the resource to be changed.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/ratonvirus/storage/multi.rb', line 48

def changed?(record, attribute)
  resource = record.public_send(attribute)

  storage_for(resource) do |storage|
    return storage.changed?(record, attribute)
  end

  false
end

#process(resource, &block) ⇒ Object

Processing of the resource is handled by the first storage in the list that returns ‘true` for `accept?(resource)`. Any consequent storages are skipped.



35
36
37
38
39
40
41
# File 'lib/ratonvirus/storage/multi.rb', line 35

def process(resource, &block)
  return unless block_given?

  storage_for(resource) do |storage|
    storage.process(resource, &block)
  end
end

#setupObject

Setup the @storages array with the initialized storage instances.



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

def setup
  @storages = []

  return unless config[:storages].is_a?(Array)

  config[:storages].each do |storage|
    if storage.is_a?(Array)
      type = storage[0]
      storage_config = storage[1]
    else
      type = storage
    end

    cls = Ratonvirus.backend_class("Storage", type)
    @storages << cls.new(storage_config || {})
  end
end