Class: Shrine::Storage::Sql
- Inherits:
-
Object
- Object
- Shrine::Storage::Sql
- Defined in:
- lib/shrine/storage/sql.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #clear! ⇒ Object
- #dataset ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(database:, table:) ⇒ Sql
constructor
A new instance of Sql.
- #open(id) ⇒ Object
- #upload(io, id, **options) ⇒ Object
- #url(id, **options) ⇒ Object
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
#database ⇒ Object (readonly)
Returns the value of attribute database.
10 11 12 |
# File 'lib/shrine/storage/sql.rb', line 10 def database @database end |
#table ⇒ Object (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 |
#dataset ⇒ Object
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
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, **) generated_id = store(io, id, **) 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, **) end |