Class: Dis::Layer

Inherits:
Object
  • Object
show all
Includes:
Logging
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

Methods included from Logging

#debug_log, #logger

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



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.

Returns:

  • (Boolean)


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.



148
149
150
151
152
153
154
# File 'lib/dis/layer.rb', line 148

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
115
116
117
# File 'lib/dis/layer.rb', line 108

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)


122
123
124
125
126
127
128
# File 'lib/dis/layer.rb', line 122

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)


133
134
135
136
137
138
139
140
# File 'lib/dis/layer.rb', line 133

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.

Returns:

  • (Boolean)


67
68
69
# File 'lib/dis/layer.rb', line 67

def immediate?
  !delayed?
end

#nameObject

Returns a name for the layer.

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


159
160
161
# File 'lib/dis/layer.rb', line 159

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

#public?Boolean

Returns true if the layer is public.

Returns:

  • (Boolean)


72
73
74
# File 'lib/dis/layer.rb', line 72

def public?
  @public
end

#readonly?Boolean

Returns true if the layer is read only.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


82
83
84
# File 'lib/dis/layer.rb', line 82

def writeable?
  !readonly?
end