Class: Dis::Layer
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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#delayed? ⇒ Boolean
Returns true if the layer is a delayed layer.
-
#delete(type, key) ⇒ Object
Deletes a file from the store.
-
#existing(type, keys) ⇒ Object
Returns all the given keys that exist in the layer.
-
#exists?(type, key) ⇒ Boolean
Returns true if a object with the given key exists.
-
#get(type, key) ⇒ Object
Retrieves a file from the store.
-
#immediate? ⇒ Boolean
Returns true if the layer isn’t a delayed layer.
-
#initialize(connection, options = {}) ⇒ Layer
constructor
A new instance of Layer.
-
#name ⇒ Object
Returns a name for the layer.
-
#public? ⇒ Boolean
Returns true if the layer is public.
-
#readonly? ⇒ Boolean
Returns true if the layer is read only.
-
#store(type, key, file) ⇒ Object
Stores a file.
-
#writeable? ⇒ Boolean
Returns true if the layer is writeable.
Methods included from Logging
Constructor Details
#initialize(connection, options = {}) ⇒ Layer
Returns a new instance of Layer.
52 53 54 55 56 57 58 59 |
# File 'lib/dis/layer.rb', line 52 def initialize(connection, = {}) = .merge() @connection = connection @delayed = [:delayed] @readonly = [:readonly] @public = [:public] @path = [:path] end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
50 51 52 |
# File 'lib/dis/layer.rb', line 50 def connection @connection end |
Instance Method Details
#delayed? ⇒ Boolean
Returns true if the layer is a delayed layer.
62 63 64 |
# File 'lib/dis/layer.rb', line 62 def delayed? @delayed end |
#delete(type, key) ⇒ Object
Deletes a file from the store.
layer.delete("documents", key)
Returns true if the file was deleted, or false if it could not be found. Raises an error if the layer is readonly.
145 146 147 148 149 150 151 |
# File 'lib/dis/layer.rb', line 145 def delete(type, key) raise Dis::Errors::ReadOnlyError if readonly? debug_log("Delete #{type}/#{key} from #{name}") do delete!(type, key) end end |
#existing(type, keys) ⇒ Object
Returns all the given keys that exist in the layer.
layer.existing("documents", keys)
108 109 110 111 112 113 114 |
# File 'lib/dis/layer.rb', line 108 def existing(type, keys) return [] if keys.empty? list = directory(type, keys.first).files.map(&:key) keys.select { |key| list.include?(key_component(type, key)) } end |
#exists?(type, key) ⇒ Boolean
Returns true if a object with the given key exists.
layer.exists?("documents", key)
119 120 121 122 123 124 125 |
# File 'lib/dis/layer.rb', line 119 def exists?(type, key) if directory(type, key)&.files&.head(key_component(type, key)) true else false end end |
#get(type, key) ⇒ Object
Retrieves a file from the store.
layer.get("documents", key)
130 131 132 133 134 135 136 137 |
# File 'lib/dis/layer.rb', line 130 def get(type, key) dir = directory(type, key) return unless dir debug_log("Get #{type}/#{key} from #{name}") do dir.files.get(key_component(type, key)) end end |
#immediate? ⇒ Boolean
Returns true if the layer isn’t a delayed layer.
67 68 69 |
# File 'lib/dis/layer.rb', line 67 def immediate? !delayed? end |
#name ⇒ Object
Returns a name for the layer.
layer.name # => "Fog::Storage::Local::Real/development"
156 157 158 |
# File 'lib/dis/layer.rb', line 156 def name "#{connection.class.name}/#{path}" end |
#public? ⇒ Boolean
Returns true if the layer is public.
72 73 74 |
# File 'lib/dis/layer.rb', line 72 def public? @public end |
#readonly? ⇒ Boolean
Returns true if the layer is read only.
77 78 79 |
# File 'lib/dis/layer.rb', line 77 def readonly? @readonly end |
#store(type, key, file) ⇒ Object
Stores a file.
key = Digest::SHA1.file(file.path).hexdigest
layer.store("documents", key, path)
The key 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.
97 98 99 100 101 102 103 |
# File 'lib/dis/layer.rb', line 97 def store(type, key, file) raise Dis::Errors::ReadOnlyError if readonly? debug_log("Store #{type}/#{key} to #{name}") do store!(type, key, file) end end |
#writeable? ⇒ Boolean
Returns true if the layer is writeable.
82 83 84 |
# File 'lib/dis/layer.rb', line 82 def writeable? !readonly? end |