Class: ArchiveStorage::Adapters::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/adapters/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Memory

Returns a new instance of Memory.



12
13
14
15
# File 'lib/archive_storage/adapters/memory.rb', line 12

def initialize(config)
  @config = config
  @objects = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/archive_storage/adapters/memory.rb', line 10

def config
  @config
end

Instance Method Details

#copy_from(source_adapter, source_key, target_key) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/archive_storage/adapters/memory.rb', line 42

def copy_from(source_adapter, source_key, target_key)
   = source_adapter.head(source_key)
  write(
    target_key,
    source_adapter.read(source_key),
    content_type: .content_type,
    metadata: . || {}
  )
end

#delete(key) ⇒ Object



70
71
72
73
# File 'lib/archive_storage/adapters/memory.rb', line 70

def delete(key)
  @objects.delete(key)
  true
end

#download_to(key, path) ⇒ Object



38
39
40
# File 'lib/archive_storage/adapters/memory.rb', line 38

def download_to(key, path)
  ::File.binwrite(path, read(key))
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/archive_storage/adapters/memory.rb', line 66

def exists?(key)
  @objects.key?(key)
end

#head(key) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/archive_storage/adapters/memory.rb', line 52

def head(key)
  object = object_for(key)
  body = object.fetch(:body)

  Metadata.new(
    byte_size: body.bytesize,
    content_type: object[:content_type],
    etag: Digest::MD5.hexdigest(body),
    checksum: Digest::MD5.hexdigest(body),
    checksum_algorithm: "md5",
    metadata: object[:metadata] || {}
  )
end

#read(key) ⇒ Object



34
35
36
# File 'lib/archive_storage/adapters/memory.rb', line 34

def read(key)
  object_for(key).fetch(:body)
end

#upload(key, file, content_type: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/archive_storage/adapters/memory.rb', line 17

def upload(key, file, content_type: nil)
  body = read_upload_body(file)
  write(
    key,
    body,
    content_type: content_type || detect_content_type(file)
  )
end

#url(key, **_options) ⇒ Object

Raises:



75
76
77
78
79
# File 'lib/archive_storage/adapters/memory.rb', line 75

def url(key, **_options)
  raise NotFoundError, "object #{key.inspect} not found" unless exists?(key)

  "memory://#{config.name}/#{key}"
end

#write(key, body, content_type: nil, metadata: {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/archive_storage/adapters/memory.rb', line 26

def write(key, body, content_type: nil, metadata: {})
  @objects[key] = {
    body: body.to_s.b,
    content_type: content_type,
    metadata: 
  }
end