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

- (Style) initialize(name, definition, attachment)

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
30
31
32
33
# 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)
    @source_file_options = definition.delete(:source_file_options)
    @other_args = definition
  elsif definition.is_a? String
    @geometry = definition
    @format = nil
    @other_args = {}
  else
    @geometry, @format = [definition, nil].flatten[0..1]
    @other_args = {}
  end
  @format = default_format if @format.blank?
end

Instance Attribute Details

- (Object) attachment (readonly)

Returns the value of attribute attachment



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

def attachment
  @attachment
end

- (Object) format (readonly)

Returns the value of attribute format



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

def format
  @format
end

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

Instance Method Details

- (Object) [](key)

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



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

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

- (Object) []=(key, value)



94
95
96
97
98
99
100
# File 'lib/paperclip/style.rb', line 94

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

- (Object) convert_options



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

def convert_options
  @convert_options.respond_to?(:call) ? @convert_options.call(attachment.instance) :
    (@convert_options || attachment.send(:extra_options_for, name))
end

- (Object) default_format

defaults to default format (nil by default)



103
104
105
106
# File 'lib/paperclip/style.rb', line 103

def default_format
  base = attachment.options[:default_format]
  base.respond_to?(:call) ? base.call(attachment, name) : base
end

- (Object) geometry

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



66
67
68
# File 'lib/paperclip/style.rb', line 66

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

- (Object) processor_options

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.



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

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

- (Object) processors

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. if a proc has been supplied, we call it here



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

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

- (Object) source_file_options



59
60
61
62
# File 'lib/paperclip/style.rb', line 59

def source_file_options
  @source_file_options.respond_to?(:call) ? @source_file_options.call(attachment.instance) :
    (@source_file_options || attachment.send(:extra_source_file_options_for, name))
end

- (Object) whiny

retrieves from the attachment the whiny setting



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

def whiny
  attachment.whiny
end

- (Boolean) whiny?

returns true if we're inclined to grumble

Returns:

  • (Boolean)


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

def whiny?
  !!whiny
end