Class: Dis::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/dis/layer.rb

Overview

Dis Layer

Represents a layer of storage. It’s a wrapper around Fog::Storage, any provider supported by Fog should be usable.

Options

  • :delayed - Delayed layers will be processed outside of the request cycle by ActiveJob.

  • :readonly - Readonly layers can only be read from, not written to.

  • :public - Objects stored in public layers will have the public readable flag set if supported by the storage provider.

  • :path - Directory name to use for the store. For Amazon S3, this will be the name of the bucket.

Examples

This creates a local storage layer. It’s a good idea to have a local layer first, this provides you with a cache on disk that will be faster than reading from the cloud.

Dis::Layer.new(
  Fog::Storage.new({
    provider: 'Local',
    local_root: Rails.root.join('db', 'dis')
  }),
  path: Rails.env
)

This creates a delayed layer on Amazon S3. ActiveJob will kick in and and transfer content from one of the immediate layers later at it’s leisure.

Dis::Layer.new(
  Fog::Storage.new({
    provider:              'AWS',
    aws_access_key_id:     YOUR_AWS_ACCESS_KEY_ID,
    aws_secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY
  }),
  path: "my_bucket",
  delayed: true
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options = {}) ⇒ Layer

Returns a new instance of Layer.



50
51
52
53
54
55
56
57
# File 'lib/dis/layer.rb', line 50

def initialize(connection, options = {})
  options     = default_options.merge(options)
  @connection = connection
  @delayed    = options[:delayed]
  @readonly   = options[:readonly]
  @public     = options[:public]
  @path       = options[:path]
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



48
49
50
# File 'lib/dis/layer.rb', line 48

def connection
  @connection
end

Instance Method Details

#delayed?Boolean

Returns true if the layer is a delayed layer.

Returns:

  • (Boolean)


60
61
62
# File 'lib/dis/layer.rb', line 60

def delayed?
  @delayed
end

#delete(type, hash) ⇒ Object

Deletes a file from the store.

layer.delete("documents", hash)

Returns true if the file was deleted, or false if it could not be found. Raises an error if the layer is readonly.



127
128
129
130
# File 'lib/dis/layer.rb', line 127

def delete(type, hash)
  raise Dis::Errors::ReadOnlyError if readonly?
  delete!(type, hash)
end

#exists?(type, hash) ⇒ Boolean

Returns true if a object with the given hash exists.

layer.exists?("documents", hash)

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
# File 'lib/dis/layer.rb', line 103

def exists?(type, hash)
  if directory(type, hash) &&
     directory(type, hash).files.head(key_component(type, hash))
    true
  else
    false
  end
end

#get(type, hash) ⇒ Object

Retrieves a file from the store.

layer.get("documents", hash)


115
116
117
118
119
# File 'lib/dis/layer.rb', line 115

def get(type, hash)
  dir = directory(type, hash)
  return unless dir
  dir.files.get(key_component(type, hash))
end

#immediate?Boolean

Returns true if the layer isn’t a delayed layer.

Returns:

  • (Boolean)


65
66
67
# File 'lib/dis/layer.rb', line 65

def immediate?
  !delayed?
end

#nameObject

Returns a name for the layer.

layer.name # => "Fog::Storage::Local::Real/development"


135
136
137
# File 'lib/dis/layer.rb', line 135

def name
  "#{connection.class.name}/#{path}"
end

#public?Boolean

Returns true if the layer isn’t a delayed layer.

Returns:

  • (Boolean)


70
71
72
# File 'lib/dis/layer.rb', line 70

def public?
  @public
end

#readonly?Boolean

Returns true if the layer is read only.

Returns:

  • (Boolean)


75
76
77
# File 'lib/dis/layer.rb', line 75

def readonly?
  @readonly
end

#store(type, hash, file) ⇒ Object

Stores a file.

hash = Digest::SHA1.file(file.path).hexdigest
layer.store("documents", hash, path)

Hash must be a hex digest of the file content. If an object with the supplied hash already exists, no action will be performed. In other words, no data will be overwritten if a hash collision occurs.

Returns an instance of Fog::Model, or raises an error if the layer is readonly.



95
96
97
98
# File 'lib/dis/layer.rb', line 95

def store(type, hash, file)
  raise Dis::Errors::ReadOnlyError if readonly?
  store!(type, hash, file)
end

#writeable?Boolean

Returns true if the layer is writeable.

Returns:

  • (Boolean)


80
81
82
# File 'lib/dis/layer.rb', line 80

def writeable?
  !readonly?
end