Class: Persistable::CloudStorageAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/persistable/cloud_storage_adapter.rb

Overview

CloudStorageAdapter powered by the fog gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CloudStorageAdapter

Creates a new ‘CloudStorageAdapter`

Parameters:

  • options (Hash) (defaults to: {})

    ‘hash` with options for this adapter

Options Hash (options):

  • :provider (Symbol)

    One of the providers given by / fog gem

  • :directory (String)

    The directory to save the files to, e.g. file system directory, amazon s3 bucket, …

  • :provider_options (Hash)

    Provider options needed for / fog gem storage providers



22
23
24
25
26
27
28
29
# File 'lib/persistable/cloud_storage_adapter.rb', line 22

def initialize(options = {})
  @options = options
  available_providers = Fog::Storage.providers
  available_providers.include?(provider) or raise ArgumentError, "Unknown Fog::Storage provider. Should be on of \
   #{available_providers.inspect}, given provider"
  directory_name or raise ArgumentError, "Directory to save file should be given."
  check_connection
end

Instance Attribute Details

#optionsHash (readonly)

Returns Options used during the object initialization.

Returns:

  • (Hash)

    Options used during the object initialization



11
12
13
# File 'lib/persistable/cloud_storage_adapter.rb', line 11

def options
  @options
end

Instance Method Details

#delete(persistable) ⇒ @see #write

Deletes file named ‘persistable#persistance_key` from storage

Parameters:

  • persistable (@see #write)

Returns:



58
59
60
61
62
63
# File 'lib/persistable/cloud_storage_adapter.rb', line 58

def delete(persistable)
  if file = directory.files.head(persistable.persistence_key)
    return file.destroy
  end
  false
end

#read(persistable) ⇒ @see #write

Reads the data on the cloud and write it on the ‘persistance_data` property of `persistable` object

Parameters:

  • persistable (@see #write)

Returns:



46
47
48
49
50
51
52
# File 'lib/persistable/cloud_storage_adapter.rb', line 46

def read(persistable)
  if file = directory.files.get(persistable.persistence_key)
    persistable.persistence_data = StringIO.new(file.body)
    return true
  end
  false
end

#write(persistable) ⇒ Boolean

Write the data given by ‘#persisence_data` method to the cloud

Parameters:

  • persistable (#persistance_key, #persistance_data)

    Any object responding to the methods needed

Returns:

  • (Boolean)

    Returns the success status of this operation



35
36
37
38
39
40
# File 'lib/persistable/cloud_storage_adapter.rb', line 35

def write(persistable)
  if persistable.persistence_data
    return directory.files.create(:key => persistable.persistence_key, :body => persistable.persistence_data)
  end
  return false
end