Class: Paperclip::Style

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

Overview

The Style class holds the definition of a thumbnail style, applying whatever processing is required to normalize the definition and delaying the evaluation of block parameters until useful context is available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition, attachment) ⇒ Style

Creates a Style object. name is the name of the attachment, definition is the style definition from has_attached_file, which can be string, array or hash



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/paperclip/style.rb', line 14

def initialize name, definition, attachment
  @name = name
  @attachment = attachment
  if definition.is_a? Hash
    @geometry = definition.delete(:geometry)
    @format = definition.delete(:format)
    @processors = definition.delete(:processors)
    @convert_options = definition.delete(:convert_options) # adding to support individual convert options per style
    @other_args = definition
  else
    @geometry, @format = [definition, nil].flatten[0..1]
    @convert_options = ""
    @other_args = {}
  end
  @format  = nil if @format.blank?
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



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

def attachment
  @attachment
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object

Supports getting and setting style properties with hash notation to ensure backwards-compatibility eg. @attachment.styles[:geometry]@ will still work



76
77
78
79
80
81
82
# File 'lib/paperclip/style.rb', line 76

def [](key)
  if [:name, :convert_options, :whiny, :processors, :geometry, :format].include?(key)
    send(key)
  elsif defined? @other_args[key]
    @other_args[key]
  end
end

#[]=(key, value) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/paperclip/style.rb', line 84

def []=(key, value)
  if [:name, :convert_options, :whiny, :processors, :geometry, :format].include?(key)
    send("#{key}=".intern, value)
  else
    @other_args[key] = value
  end
end

#convert_optionsObject



49
50
51
52
# File 'lib/paperclip/style.rb', line 49

def convert_options
  #attachment.send(:extra_options_for, name)
  @convert_options
end

#geometryObject

returns the geometry string for this style if a proc has been supplied, we call it here



56
57
58
# File 'lib/paperclip/style.rb', line 56

def geometry
  @geometry.respond_to?(:call) ? @geometry.call(attachment.instance) : @geometry
end

#processor_optionsObject

Supplies the hash of options that processors expect to receive as their second argument Arguments other than the standard geometry, format etc are just passed through from initialization and any procs are called here, just before post-processing.



63
64
65
66
67
68
69
70
71
72
# File 'lib/paperclip/style.rb', line 63

def processor_options
  args = {}
  @other_args.each do |k,v|
    args[k] = v.respond_to?(:call) ? v.call(attachment) : v
  end
  [:processors, :geometry, :format, :whiny, :convert_options].each do |k|
    (arg = send(k)) && args[k] = arg
  end
  args
end

#processorsObject

retrieves from the attachment the processors defined in the has_attached_file call (which method (in the attachment) will call any supplied procs) There is an important change of interface here: a style rule can set its own processors by default we behave as before, though.



35
36
37
# File 'lib/paperclip/style.rb', line 35

def processors
  @processors || attachment.processors
end

#whinyObject

retrieves from the attachment the whiny setting



40
41
42
# File 'lib/paperclip/style.rb', line 40

def whiny
  attachment.whiny
end

#whiny?Boolean

returns true if we’re inclined to grumble

Returns:

  • (Boolean)


45
46
47
# File 'lib/paperclip/style.rb', line 45

def whiny?
  !!whiny
end