Class: DeepStore::DAO

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_store/dao.rb

Constant Summary collapse

Result =
Class.new(OpenStruct)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ DAO

Returns a new instance of DAO.



7
8
9
10
11
# File 'lib/deep_store/dao.rb', line 7

def initialize(data = {})
  @adapter = data.fetch(:adapter, DeepStore.adapter)
  @bucket  = data.fetch(:bucket, DeepStore.settings.bucket)
  @codec   = data.fetch(:codec)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/deep_store/dao.rb', line 3

def adapter
  @adapter
end

#bucketObject (readonly)

Returns the value of attribute bucket.



3
4
5
# File 'lib/deep_store/dao.rb', line 3

def bucket
  @bucket
end

#codecObject (readonly)

Returns the value of attribute codec.



3
4
5
# File 'lib/deep_store/dao.rb', line 3

def codec
  @codec
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



3
4
5
# File 'lib/deep_store/dao.rb', line 3

def resource_class
  @resource_class
end

Instance Method Details

#delete(key) ⇒ Object



32
33
34
# File 'lib/deep_store/dao.rb', line 32

def delete(key)
  adapter.delete_object(bucket: bucket, key: key)
end

#expand_path(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deep_store/dao.rb', line 36

def expand_path(path)
  keys   = []
  marker = nil

  while (objects = remote_objects_for(path, marker)).any?
    keys.concat(objects.map(&:key))
    marker = objects.last.key
  end

  keys.uniq
end

#get(key) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/deep_store/dao.rb', line 19

def get(key)
  stream = Tempfile.new
  object = adapter.get_object(bucket: bucket, key: key, response_target: stream)
  Result.new(object: object, stream: codec.decode(stream))
rescue Aws::S3::Errors::NoSuchKey
  raise DeepStore::Errors::RecordNotFound
end

#head(key) ⇒ Object



13
14
15
16
17
# File 'lib/deep_store/dao.rb', line 13

def head(key)
  adapter.head_object(bucket: bucket, key: key)
rescue Aws::S3::Errors::NoSuchKey
  raise DeepStore::Errors::RecordNotFound
end

#put(key, stream) ⇒ Object



27
28
29
30
# File 'lib/deep_store/dao.rb', line 27

def put(key, stream)
  encoded_stream = codec.encode(stream)
  adapter.put_object(bucket: bucket, key: key, body: encoded_stream)
end