Module: Shrine::ClassMethods

Included in:
Shrine
Defined in:
lib/shrine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

A logger instance.



52
53
54
# File 'lib/shrine.rb', line 52

def logger
  @logger
end

#optsObject (readonly)

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



46
47
48
# File 'lib/shrine.rb', line 46

def opts
  @opts
end

#storagesObject

A hash of storages with their symbol identifiers.



49
50
51
# File 'lib/shrine.rb', line 49

def storages
  @storages
end

Instance Method Details

#Attachment(name, *args) ⇒ 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


111
112
113
# File 'lib/shrine.rb', line 111

def Attachment(name, *args)
  self::Attachment.new(name, *args)
end

#deprecation(message) ⇒ Object

Prints a deprecation warning to the logger.



169
170
171
# File 'lib/shrine.rb', line 169

def deprecation(message)
  Shrine.logger.warn "SHRINE DEPRECATION WARNING: #{message}"
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.



101
102
103
# File 'lib/shrine.rb', line 101

def find_storage(name)
  storages[name.to_sym] || storages[name.to_s] or fail Error, "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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shrine.rb', line 56

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) ⇒ Object

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


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shrine.rb', line 84

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

#upload(io, storage, context = {}) ⇒ Object

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

Shrine.upload(io, :store) #=> #<Shrine::UploadedFile>


120
121
122
# File 'lib/shrine.rb', line 120

def upload(io, storage, context = {})
  new(storage).upload(io, context)
end

#uploaded_file(object, &block) ⇒ 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>


129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/shrine.rb', line 129

def uploaded_file(object, &block)
  case object
  when String
    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

#warn(message) ⇒ Object

Prints a warning to the logger.



164
165
166
# File 'lib/shrine.rb', line 164

def warn(message)
  Shrine.logger.warn "SHRINE WARNING: #{message}"
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 }


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/shrine.rb', line 148

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