module Shrine::Plugins::Base::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)

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, line 157
def attachment(name)
  self::Attachment.new(name)
end
deprecation (message)

Prints a deprecation warning to standard error.

[show source]
# File lib/shrine.rb, line 182
def deprecation(message)
  warn "SHRINE DEPRECATION WARNING: #{message}"
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, line 146
def find_storage(name)
  storages.each { |key, value| return value if key.to_s == name.to_s }
  raise Error, "storage #{name.inspect} isn't registered on #{self}"
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, line 101
def inherited(subclass)
  subclass.instance_variable_set(:@opts, opts.dup)
  subclass.opts.each do |key, value|
    if value.is_a?(Enumerable) && !value.frozen?
      subclass.opts[key] = value.dup
    end
  end
  subclass.instance_variable_set(:@storages, storages.dup)

  file_class = Class.new(self::UploadedFile)
  file_class.shrine_class = subclass
  subclass.const_set(:UploadedFile, file_class)

  attachment_class = Class.new(self::Attachment)
  attachment_class.shrine_class = subclass
  subclass.const_set(:Attachment, attachment_class)

  attacher_class = Class.new(self::Attacher)
  attacher_class.shrine_class = subclass
  subclass.const_set(:Attacher, attacher_class)
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, line 129
def plugin(plugin, *args, &block)
  plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)
  plugin.load_dependencies(self, *args, &block) if plugin.respond_to?(:load_dependencies)
  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)
  plugin.configure(self, *args, &block) if plugin.respond_to?(:configure)
  plugin
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, line 167
def uploaded_file(object, &block)
  case object
  when String
    deprecation("Giving a string to Shrine.uploaded_file is deprecated and won't be possible in Shrine 3. Use Attacher#uploaded_file instead.")
    uploaded_file(JSON.parse(object), &block)
  when Hash
    uploaded_file(self::UploadedFile.new(object), &block)
  when self::UploadedFile
    object.tap { |f| yield(f) if block_given? }
  else
    raise Error, "cannot convert #{object.inspect} to a #{self}::UploadedFile"
  end
end