Class: Paperclip::StyleMap::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/style_map/map.rb

Constant Summary collapse

DEFAULT_EXPIRATION =
3600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, attachment:) ⇒ Map

Returns a new instance of Map.

Parameters:

  • model (ActiveRecord::Base)
  • attachment (Paperclip::Attachment)


13
14
15
16
17
18
19
20
# File 'lib/paperclip/style_map/map.rb', line 13

def initialize(model:, attachment:)
  @model = model
  @attachment = attachment

  @attachment_name = attachment.name

  @style_names = Dux.enum @attachment.default_style, *@attachment.styles.keys
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



7
8
9
# File 'lib/paperclip/style_map/map.rb', line 7

def attachment
  @attachment
end

#attachment_nameObject (readonly)

Returns the value of attribute attachment_name.



8
9
10
# File 'lib/paperclip/style_map/map.rb', line 8

def attachment_name
  @attachment_name
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/paperclip/style_map/map.rb', line 6

def model
  @model
end

#style_namesObject (readonly)

Returns the value of attribute style_names.



9
10
11
# File 'lib/paperclip/style_map/map.rb', line 9

def style_names
  @style_names
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/paperclip/style_map/map.rb', line 33

def blank?
  !@attachment.exists? || processing?
end

#fetch(style_name, expiration: DEFAULT_EXPIRATION) ⇒ String? Also known as: []

Parameters:

  • style_name (String, Symbol)
  • expiration (ActiveSupport::Duration, Integer) (defaults to: DEFAULT_EXPIRATION)

Returns:

  • (String, nil)


25
26
27
28
29
# File 'lib/paperclip/style_map/map.rb', line 25

def fetch(style_name, expiration: DEFAULT_EXPIRATION)
  return nil if blank? || !valid_style?(style_name)

  @attachment.expiring_url(expiration, style_name)
end

#inspectObject



41
42
43
44
45
# File 'lib/paperclip/style_map/map.rb', line 41

def inspect
  inspected_styles = @style_names.map { |n| n.to_sym.inspect }.inspect

  "#<#{self.class.name} @model=#{@model.model_name} @attachment=#{@attachment_name} @style_names=#{inspected_styles}>"
end

#processing?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/paperclip/style_map/map.rb', line 37

def processing?
  @attachment.respond_to?(:processing?) && @attachment.processing?
end

#to_hObject



52
53
54
55
56
# File 'lib/paperclip/style_map/map.rb', line 52

def to_h
  @style_names.each_with_object({}.with_indifferent_access) do |style_name, urls|
    urls[style_name.to_s] = self[style_name]
  end
end

#valid_style?(name) ⇒ Boolean

Parameters:

  • name (String, Symbol)

Returns:

  • (Boolean)


48
49
50
# File 'lib/paperclip/style_map/map.rb', line 48

def valid_style?(name)
  @style_names.include? name
end