module Shrine::Plugins::Entity::AttachmentMethods

Public Class Methods

new(name, **options) click to toggle source

Defines instance methods on initialization.

Calls superclass method
# File lib/shrine/plugins/entity.rb, line 13
def initialize(name, **options)
  super

  define_entity_methods(name)
end

Public Instance Methods

included(klass) click to toggle source

Defines class methods on inclusion.

Calls superclass method
# File lib/shrine/plugins/entity.rb, line 20
def included(klass)
  super

  attachment = self

  klass.send(:define_singleton_method, :"#{@name}_attacher") do |**options|
    attachment.send(:class_attacher, **options)
  end
end

Private Instance Methods

attacher(record, **options) click to toggle source

Returns the class attacher instance with loaded entity. It’s not memoized because the entity object could be frozen.

# File lib/shrine/plugins/entity.rb, line 62
def attacher(record, **options)
  attacher = class_attacher(**options)
  attacher.load_entity(record, @name)
  attacher
end
class_attacher(**options) click to toggle source

Creates an instance of the corresponding attacher class with set name.

# File lib/shrine/plugins/entity.rb, line 70
def class_attacher(**options)
  attacher = shrine_class::Attacher.new(**@options, **options)
  attacher.instance_variable_set(:@name, @name)
  attacher
end
define_entity_methods(name) click to toggle source

Defines ‘#<name>`, `#<name>_url`, and `#<name>_attacher` methods.

Calls superclass method
# File lib/shrine/plugins/entity.rb, line 33
def define_entity_methods(name)
  super if defined?(super)

  attachment = self

  # Returns the attached file.
  if shrine_class::Attacher.instance_method(:get).arity == 0
    define_method :"#{name}" do
      send(:"#{name}_attacher").get
    end
  else # derivatives
    define_method :"#{name}" do |*args|
      send(:"#{name}_attacher").get(*args)
    end
  end

  # Returns the URL to the attached file.
  define_method :"#{name}_url" do |*args, **options|
    send(:"#{name}_attacher").url(*args, **options)
  end

  # Returns an attacher instance.
  define_method :"#{name}_attacher" do |**options|
    attachment.send(:attacher, self, **options)
  end
end