module Shrine::Attachment::InstanceMethods

  1. lib/shrine/attachment.rb

Public Class methods

new (name, **options)

Instantiates an attachment module for a given attribute name, which can then be included to a model class. Second argument will be passed to an attacher module.

[show source]
   # File lib/shrine/attachment.rb
27 def initialize(name, **options)
28   @name    = name.to_sym
29   @options = options
30 
31   define_attachment_methods!
32 end

Public Instance methods

attachment_name ()

Returns name of the attachment this module provides.

[show source]
   # File lib/shrine/attachment.rb
67 def attachment_name
68   @name
69 end
build_attacher (object, options)

Creates an instance of the corresponding Attacher subclass.

[show source]
   # File lib/shrine/attachment.rb
62 def build_attacher(object, options)
63   shrine_class::Attacher.new(object, @name, @options.merge(options))
64 end
define_attachment_methods! ()

Defines attachment methods for the specified attachment name. These methods will be added to any model that includes this module.

[show source]
   # File lib/shrine/attachment.rb
36 def define_attachment_methods!
37   attachment = self
38   name = attachment_name
39 
40   define_method :"#{name}_attacher" do |**options|
41     if !instance_variable_get(:"@#{name}_attacher") || options.any?
42       instance_variable_set(:"@#{name}_attacher", attachment.build_attacher(self, options))
43     else
44       instance_variable_get(:"@#{name}_attacher")
45     end
46   end
47 
48   define_method :"#{name}=" do |value|
49     send(:"#{name}_attacher").assign(value)
50   end
51 
52   define_method :"#{name}" do
53     send(:"#{name}_attacher").get
54   end
55 
56   define_method :"#{name}_url" do |*args|
57     send(:"#{name}_attacher").url(*args)
58   end
59 end
inspect ()

Returns class name with attachment name included.

Shrine[:image].inspect #=> "#<Shrine::Attachment(image)>"
[show source]
   # File lib/shrine/attachment.rb
86 def inspect
87   "#<#{self.class.inspect}(#{attachment_name})>"
88 end
options ()

Returns options that are to be passed to the Attacher.

[show source]
   # File lib/shrine/attachment.rb
72 def options
73   @options
74 end
shrine_class ()

Returns the Shrine class that this attachment's class is namespaced under.

[show source]
   # File lib/shrine/attachment.rb
92 def shrine_class
93   self.class.shrine_class
94 end
to_s ()

Returns class name with attachment name included.

Shrine[:image].to_s #=> "#<Shrine::Attachment(image)>"
[show source]
   # File lib/shrine/attachment.rb
79 def to_s
80   "#<#{self.class.inspect}(#{attachment_name})>"
81 end