Class: Refile::Memory::Backend

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: nil, hasher: Refile::RandomHasher.new) ⇒ Backend

Returns a new instance of Backend.



9
10
11
12
13
# File 'lib/refile/memory.rb', line 9

def initialize(max_size: nil, hasher: Refile::RandomHasher.new)
  @hasher = hasher
  @max_size = max_size
  @store = {}
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



7
8
9
# File 'lib/refile/memory.rb', line 7

def directory
  @directory
end

Instance Method Details

#clear!(confirm = nil) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
# File 'lib/refile/memory.rb', line 49

def clear!(confirm = nil)
  raise ArgumentError, "are you sure? this will remove all files in the backend, call as `clear!(:confirm)` if you're sure you want to do this" unless confirm == :confirm
  @store = {}
end

#delete(id) ⇒ Object



29
30
31
# File 'lib/refile/memory.rb', line 29

def delete(id)
  @store.delete(id)
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/refile/memory.rb', line 45

def exists?(id)
  @store.has_key?(id)
end

#get(id) ⇒ Object



25
26
27
# File 'lib/refile/memory.rb', line 25

def get(id)
  Refile::File.new(self, id)
end

#open(id) ⇒ Object



33
34
35
# File 'lib/refile/memory.rb', line 33

def open(id)
  StringIO.new(@store[id])
end

#read(id) ⇒ Object



37
38
39
# File 'lib/refile/memory.rb', line 37

def read(id)
  @store[id]
end

#size(id) ⇒ Object



41
42
43
# File 'lib/refile/memory.rb', line 41

def size(id)
  @store[id].bytesize if exists?(id)
end

#upload(uploadable) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/refile/memory.rb', line 15

def upload(uploadable)
  Refile.verify_uploadable(uploadable, @max_size)

  id = @hasher.hash(uploadable)

  @store[id] = uploadable.read

  Refile::File.new(self, id)
end