module Shrine::ClassMethods
Attributes
A logger instance.
Generic options for this class, plugins store their options here.
A hash of storages with their symbol identifiers.
Public Instance Methods
Source
# File lib/shrine.rb 102 def Attachment(name, **) 103 self::Attachment.new(name, **) 104 end
Generates an instance of Shrine::Attachment to be included in the model class. Example:
class Photo include Shrine::Attachment(:image) # creates a Shrine::Attachment object end
Source
# File lib/shrine.rb 161 def deprecation(message) 162 Shrine.logger.warn "SHRINE DEPRECATION WARNING: #{message}" 163 end
Prints a deprecation warning to the logger.
Source
# File lib/shrine.rb 92 def find_storage(name) 93 storages[name.to_sym] || storages[name.to_s] or fail MissingStorage, "storage #{name.inspect} isn't registered on #{self}" 94 end
Retrieves the storage under the given identifier (can be a Symbol or a String), raising Shrine::Error if the storage is missing.
Source
# File lib/shrine.rb 52 def inherited(subclass) 53 subclass.instance_variable_set(:@opts, deep_dup(opts)) 54 subclass.instance_variable_set(:@storages, storages.dup) 55 56 file_class = Class.new(self::UploadedFile) 57 file_class.shrine_class = subclass 58 subclass.const_set(:UploadedFile, file_class) 59 60 attachment_class = Class.new(self::Attachment) 61 attachment_class.shrine_class = subclass 62 subclass.const_set(:Attachment, attachment_class) 63 64 attacher_class = Class.new(self::Attacher) 65 attacher_class.shrine_class = subclass 66 subclass.const_set(:Attacher, attacher_class) 67 end
When inheriting Shrine, copy the instance variables into the subclass, and create subclasses of core classes.
Source
# File lib/shrine.rb 75 def plugin(plugin, ...) 76 plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol) 77 Plugins.load_dependencies(plugin, self, ...) 78 self.include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods) 79 self.extend(plugin::ClassMethods) if defined?(plugin::ClassMethods) 80 self::UploadedFile.include(plugin::FileMethods) if defined?(plugin::FileMethods) 81 self::UploadedFile.extend(plugin::FileClassMethods) if defined?(plugin::FileClassMethods) 82 self::Attachment.include(plugin::AttachmentMethods) if defined?(plugin::AttachmentMethods) 83 self::Attachment.extend(plugin::AttachmentClassMethods) if defined?(plugin::AttachmentClassMethods) 84 self::Attacher.include(plugin::AttacherMethods) if defined?(plugin::AttacherMethods) 85 self::Attacher.extend(plugin::AttacherClassMethods) if defined?(plugin::AttacherClassMethods) 86 Plugins.configure(plugin, self, ...) 87 plugin 88 end
Load a new plugin into the current class. A plugin can be a module which is used directly, or a symbol representing a registered plugin which will be required and then loaded.
Shrine.plugin MyPlugin Shrine.plugin :my_plugin
Source
# File lib/shrine.rb 111 def upload(io, storage, **) 112 new(storage).upload(io, **) 113 end
Uploads the file to the specified storage. It delegates to Shrine#upload.
Shrine.upload(io, :store) #=> #<Shrine::UploadedFile>
Source
# File lib/shrine.rb 120 def uploaded_file(object) 121 case object 122 when String 123 uploaded_file(JSON.parse(object)) 124 when Hash 125 object = JSON.parse(object.to_json) if object.keys.grep(Symbol).any? # deep stringify keys 126 self::UploadedFile.new(object) 127 when self::UploadedFile 128 object 129 else 130 fail ArgumentError, "cannot convert #{object.inspect} to a #{self}::UploadedFile" 131 end 132 end
Instantiates a Shrine::UploadedFile from a hash, and optionally yields the returned object.
data = { "storage" => "cache", "id" => "abc123.jpg", "metadata" => {} } Shrine.uploaded_file(data) #=> #<Shrine::UploadedFile>
Source
# File lib/shrine.rb 156 def warn(message) 157 Shrine.logger.warn "SHRINE WARNING: #{message}" 158 end
Prints a warning to the logger.
Source
# File lib/shrine.rb 140 def with_file(io) 141 if io.respond_to?(:path) 142 yield io 143 elsif io.is_a?(UploadedFile) 144 io.download { |tempfile| yield tempfile } 145 else 146 Tempfile.create("shrine-file", binmode: true) do |file| 147 IO.copy_stream(io, file.path) 148 io.rewind 149 150 yield file 151 end 152 end 153 end
Temporarily converts an IO-like object into a file. If the input IO object is already a file, it simply yields it to the block, otherwise it copies IO content into a Tempfile object which is then yielded and afterwards deleted.
Shrine.with_file(io) { |file| file.path }