module Shrine::ClassMethods

  1. lib/shrine.rb

Public Instance Aliases

[] -> attachment

Attributes

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
106 def attachment(name, *args)
107   self::Attachment.new(name, *args)
108 end
deprecation (message)

Prints a deprecation warning to standard error.

[show source]
    # File lib/shrine.rb
151 def deprecation(message)
152   warn "SHRINE DEPRECATION WARNING: #{message}"
153 end
find_storage (name)

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

[show source]
   # File lib/shrine.rb
95 def find_storage(name)
96   storages.each { |key, value| return value if key.to_s == name.to_s }
97   raise Error, "storage #{name.inspect} isn't registered on #{self}"
98 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
50 def inherited(subclass)
51   subclass.instance_variable_set(:@opts, opts.dup)
52   subclass.opts.each do |key, value|
53     if value.is_a?(Enumerable) && !value.frozen?
54       subclass.opts[key] = value.dup
55     end
56   end
57   subclass.instance_variable_set(:@storages, storages.dup)
58 
59   file_class = Class.new(self::UploadedFile)
60   file_class.shrine_class = subclass
61   subclass.const_set(:UploadedFile, file_class)
62 
63   attachment_class = Class.new(self::Attachment)
64   attachment_class.shrine_class = subclass
65   subclass.const_set(:Attachment, attachment_class)
66 
67   attacher_class = Class.new(self::Attacher)
68   attacher_class.shrine_class = subclass
69   subclass.const_set(:Attacher, attacher_class)
70 end
plugin (plugin, *args, &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
78 def plugin(plugin, *args, &block)
79   plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)
80   plugin.load_dependencies(self, *args, &block) if plugin.respond_to?(:load_dependencies)
81   self.include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods)
82   self.extend(plugin::ClassMethods) if defined?(plugin::ClassMethods)
83   self::UploadedFile.include(plugin::FileMethods) if defined?(plugin::FileMethods)
84   self::UploadedFile.extend(plugin::FileClassMethods) if defined?(plugin::FileClassMethods)
85   self::Attachment.include(plugin::AttachmentMethods) if defined?(plugin::AttachmentMethods)
86   self::Attachment.extend(plugin::AttachmentClassMethods) if defined?(plugin::AttachmentClassMethods)
87   self::Attacher.include(plugin::AttacherMethods) if defined?(plugin::AttacherMethods)
88   self::Attacher.extend(plugin::AttacherClassMethods) if defined?(plugin::AttacherClassMethods)
89   plugin.configure(self, *args, &block) if plugin.respond_to?(:configure)
90   plugin
91 end
uploaded_file (object, &block)

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, &block)
117   case object
118   when String
119     uploaded_file(JSON.parse(object), &block)
120   when Hash
121     uploaded_file(self::UploadedFile.new(object), &block)
122   when self::UploadedFile
123     object.tap { |f| yield(f) if block_given? }
124   else
125     raise Error, "cannot convert #{object.inspect} to a #{self}::UploadedFile"
126   end
127 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
135 def with_file(io)
136   if io.respond_to?(:path)
137     yield io
138   elsif io.is_a?(UploadedFile)
139     io.download { |tempfile| yield tempfile }
140   else
141     Tempfile.create("shrine-file", binmode: true) do |file|
142       IO.copy_stream(io, file.path)
143       io.rewind
144 
145       yield file
146     end
147   end
148 end