Module: Shrine::ClassMethods
- Included in:
- Shrine
- Defined in:
- lib/shrine.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
A logger instance.
-
#opts ⇒ Object
readonly
Generic options for this class, plugins store their options here.
-
#storages ⇒ Object
A hash of storages with their symbol identifiers.
Instance Method Summary collapse
-
#Attachment(name) ⇒ Object
(also: #attachment, #[])
Generates an instance of Shrine::Attachment to be included in the model class.
-
#deprecation(message) ⇒ Object
Prints a deprecation warning to the logger.
-
#find_storage(name) ⇒ Object
Retrieves the storage under the given identifier (can be a Symbol or a String), raising Shrine::Error if the storage is missing.
-
#inherited(subclass) ⇒ Object
When inheriting Shrine, copy the instance variables into the subclass, and create subclasses of core classes.
-
#plugin(plugin) ⇒ Object
Load a new plugin into the current class.
-
#upload(io, storage) ⇒ Object
Uploads the file to the specified storage.
-
#uploaded_file(object) ⇒ Object
Instantiates a Shrine::UploadedFile from a hash, and optionally yields the returned object.
-
#warn(message) ⇒ Object
Prints a warning to the logger.
-
#with_file(io) ⇒ Object
Temporarily converts an IO-like object into a file.
Instance Attribute Details
#logger ⇒ Object
A logger instance.
48 49 50 |
# File 'lib/shrine.rb', line 48 def logger @logger end |
#opts ⇒ Object (readonly)
Generic options for this class, plugins store their options here.
42 43 44 |
# File 'lib/shrine.rb', line 42 def opts @opts end |
#storages ⇒ Object
A hash of storages with their symbol identifiers.
45 46 47 |
# File 'lib/shrine.rb', line 45 def storages @storages end |
Instance Method Details
#Attachment(name) ⇒ Object Also known as: attachment, []
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
102 103 104 |
# File 'lib/shrine.rb', line 102 def Attachment(name, **) self::Attachment.new(name, **) end |
#deprecation(message) ⇒ Object
Prints a deprecation warning to the logger.
161 162 163 |
# File 'lib/shrine.rb', line 161 def deprecation() Shrine.logger.warn "SHRINE DEPRECATION WARNING: #{}" end |
#find_storage(name) ⇒ Object
Retrieves the storage under the given identifier (can be a Symbol or a String), raising Shrine::Error if the storage is missing.
92 93 94 |
# File 'lib/shrine.rb', line 92 def find_storage(name) storages[name.to_sym] || storages[name.to_s] or fail MissingStorage, "storage #{name.inspect} isn't registered on #{self}" end |
#inherited(subclass) ⇒ Object
When inheriting Shrine, copy the instance variables into the subclass, and create subclasses of core classes.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/shrine.rb', line 52 def inherited(subclass) subclass.instance_variable_set(:@opts, deep_dup(opts)) subclass.instance_variable_set(:@storages, storages.dup) file_class = Class.new(self::UploadedFile) file_class.shrine_class = subclass subclass.const_set(:UploadedFile, file_class) = Class.new(self::Attachment) .shrine_class = subclass subclass.const_set(:Attachment, ) attacher_class = Class.new(self::Attacher) attacher_class.shrine_class = subclass subclass.const_set(:Attacher, attacher_class) end |
#plugin(plugin) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/shrine.rb', line 75 def plugin(plugin, ...) plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol) Plugins.load_dependencies(plugin, self, ...) self.include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods) self.extend(plugin::ClassMethods) if defined?(plugin::ClassMethods) self::UploadedFile.include(plugin::FileMethods) if defined?(plugin::FileMethods) self::UploadedFile.extend(plugin::FileClassMethods) if defined?(plugin::FileClassMethods) self::Attachment.include(plugin::AttachmentMethods) if defined?(plugin::AttachmentMethods) self::Attachment.extend(plugin::AttachmentClassMethods) if defined?(plugin::AttachmentClassMethods) self::Attacher.include(plugin::AttacherMethods) if defined?(plugin::AttacherMethods) self::Attacher.extend(plugin::AttacherClassMethods) if defined?(plugin::AttacherClassMethods) Plugins.configure(plugin, self, ...) plugin end |
#upload(io, storage) ⇒ Object
Uploads the file to the specified storage. It delegates to Shrine#upload.
Shrine.upload(io, :store) #=> #<Shrine::UploadedFile>
111 112 113 |
# File 'lib/shrine.rb', line 111 def upload(io, storage, **) new(storage).upload(io, **) end |
#uploaded_file(object) ⇒ Object
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>
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/shrine.rb', line 120 def uploaded_file(object) case object when String uploaded_file(JSON.parse(object)) when Hash object = JSON.parse(object.to_json) if object.keys.grep(Symbol).any? # deep stringify keys self::UploadedFile.new(object) when self::UploadedFile object else fail ArgumentError, "cannot convert #{object.inspect} to a #{self}::UploadedFile" end end |
#warn(message) ⇒ Object
Prints a warning to the logger.
156 157 158 |
# File 'lib/shrine.rb', line 156 def warn() Shrine.logger.warn "SHRINE WARNING: #{}" end |
#with_file(io) ⇒ Object
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 }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/shrine.rb', line 140 def with_file(io) if io.respond_to?(:path) yield io elsif io.is_a?(UploadedFile) io.download { |tempfile| yield tempfile } else Tempfile.create("shrine-file", binmode: true) do |file| IO.copy_stream(io, file.path) io.rewind yield file end end end |