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, 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.


142
143
144
145
146
# File 'lib/dis/layer.rb', line 142

def delete(type, key)
  raise Dis::Errors::ReadOnlyError if readonly?

  delete!(type, key)
end

#existing(type, keys) ⇒ Object

Returns all the given keys that exist in the layer.

layer.existing("documents", keys)

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

def existing(type, keys)
  return [] if keys.empty?

  list = []
  directory(type, keys.first).files.each do |file|
    list << file.key
  end

  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)

Returns:

  • (Boolean)

118
119
120
121
122
123
124
# File 'lib/dis/layer.rb', line 118

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)

129
130
131
132
133
134
# File 'lib/dis/layer.rb', line 129

def get(type, key)
  dir = directory(type, key)
  return unless dir

  dir.files.get(key_component(type, key))
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"

151
152
153
# File 'lib/dis/layer.rb', line 151

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

#public?Boolean

Returns true if the layer is public.

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, 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.


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

def store(type, key, file)
  raise Dis::Errors::ReadOnlyError if readonly?

  store!(type, key, 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