Class: Shrine::Storage::Sql

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database:, table:) ⇒ Sql

Returns a new instance of Sql.



12
13
14
15
# File 'lib/shrine/storage/sql.rb', line 12

def initialize(database:, table:)
  @database = database
  @table    = table
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



10
11
12
# File 'lib/shrine/storage/sql.rb', line 10

def database
  @database
end

#tableObject (readonly)

Returns the value of attribute table.



10
11
12
# File 'lib/shrine/storage/sql.rb', line 10

def table
  @table
end

Instance Method Details

#clear!Object



39
40
41
42
43
# File 'lib/shrine/storage/sql.rb', line 39

def clear!
  dataset = self.dataset
  dataset = yield dataset if block_given?
  dataset.delete
end

#datasetObject



45
46
47
# File 'lib/shrine/storage/sql.rb', line 45

def dataset
  database[table]
end

#delete(id) ⇒ Object



32
33
34
# File 'lib/shrine/storage/sql.rb', line 32

def delete(id)
  find(id).delete
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/shrine/storage/sql.rb', line 28

def exists?(id)
  !find(id).empty?
end

#open(id) ⇒ Object



22
23
24
25
26
# File 'lib/shrine/storage/sql.rb', line 22

def open(id, **)
  StringIO.new(content(id))
rescue Sequel::NoMatchingRow
  fail Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end

#upload(io, id, **options) ⇒ Object



17
18
19
20
# File 'lib/shrine/storage/sql.rb', line 17

def upload(io, id, **options)
  generated_id = store(io, id, **options)
  id.replace(generated_id.to_s + File.extname(id))
end

#url(id, **options) ⇒ Object



36
37
# File 'lib/shrine/storage/sql.rb', line 36

def url(id, **options)
end