module Shrine::ClassMethods

  1. lib/shrine.rb

Public Instance Aliases

[] -> Attachment
attachment -> Attachment

Attributes

logger [RW]

A logger instance.

opts [R]

Generic options for this class, plugins store their options here.

storages [RW]

A hash of storages with their symbol identifiers.

Public Instance methods

Attachment(name, **args)

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
[show source]
    # File lib/shrine.rb
 98 def Attachment(name, **args)
 99   self::Attachment.new(name, **args)
100 end
deprecation(message)

Prints a deprecation warning to the logger.

[show source]
    # File lib/shrine.rb
157 def deprecation(message)
158   Shrine.logger.warn "SHRINE DEPRECATION WARNING: #{message}"
159 end
find_storage(name)

Retrieves the storage under the given identifier (can be a Symbol or a String), raising Shrine::Error if the storage is missing.

[show source]
   # File lib/shrine.rb
88 def find_storage(name)
89   storages[name.to_sym] || storages[name.to_s] or fail Error, "storage #{name.inspect} isn't registered on #{self}"
90 end
inherited(subclass)

When inheriting Shrine, copy the instance variables into the subclass, and create subclasses of core classes.

[show source]
   # File lib/shrine.rb
48 def inherited(subclass)
49   subclass.instance_variable_set(:@opts, deep_dup(opts))
50   subclass.instance_variable_set(:@storages, storages.dup)
51 
52   file_class = Class.new(self::UploadedFile)
53   file_class.shrine_class = subclass
54   subclass.const_set(:UploadedFile, file_class)
55 
56   attachment_class = Class.new(self::Attachment)
57   attachment_class.shrine_class = subclass
58   subclass.const_set(:Attachment, attachment_class)
59 
60   attacher_class = Class.new(self::Attacher)
61   attacher_class.shrine_class = subclass
62   subclass.const_set(:Attacher, attacher_class)
63 end
plugin(plugin, *args, **kwargs, &block)

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
[show source]
   # File lib/shrine.rb
71 def plugin(plugin, *args, **kwargs, &block)
72   plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)
73   Plugins.load_dependencies(plugin, self, *args, **kwargs, &block)
74   self.include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods)
75   self.extend(plugin::ClassMethods) if defined?(plugin::ClassMethods)
76   self::UploadedFile.include(plugin::FileMethods) if defined?(plugin::FileMethods)
77   self::UploadedFile.extend(plugin::FileClassMethods) if defined?(plugin::FileClassMethods)
78   self::Attachment.include(plugin::AttachmentMethods) if defined?(plugin::AttachmentMethods)
79   self::Attachment.extend(plugin::AttachmentClassMethods) if defined?(plugin::AttachmentClassMethods)
80   self::Attacher.include(plugin::AttacherMethods) if defined?(plugin::AttacherMethods)
81   self::Attacher.extend(plugin::AttacherClassMethods) if defined?(plugin::AttacherClassMethods)
82   Plugins.configure(plugin, self, *args, **kwargs, &block)
83   plugin
84 end
upload(io, storage, **options)

Uploads the file to the specified storage. It delegates to Shrine#upload.

Shrine.upload(io, :store) #=> #<Shrine::UploadedFile>
[show source]
    # File lib/shrine.rb
107 def upload(io, storage, **options)
108   new(storage).upload(io, **options)
109 end
uploaded_file(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>
[show source]
    # File lib/shrine.rb
116 def uploaded_file(object)
117   case object
118   when String
119     uploaded_file(JSON.parse(object))
120   when Hash
121     object = JSON.parse(object.to_json) if object.keys.grep(Symbol).any? # deep stringify keys
122     self::UploadedFile.new(object)
123   when self::UploadedFile
124     object
125   else
126     fail ArgumentError, "cannot convert #{object.inspect} to a #{self}::UploadedFile"
127   end
128 end
warn(message)

Prints a warning to the logger.

[show source]
    # File lib/shrine.rb
152 def warn(message)
153   Shrine.logger.warn "SHRINE WARNING: #{message}"
154 end
with_file(io)

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 }
[show source]
    # File lib/shrine.rb
136 def with_file(io)
137   if io.respond_to?(:path)
138     yield io
139   elsif io.is_a?(UploadedFile)
140     io.download { |tempfile| yield tempfile }
141   else
142     Tempfile.create("shrine-file", binmode: true) do |file|
143       IO.copy_stream(io, file.path)
144       io.rewind
145 
146       yield file
147     end
148   end
149 end