Class: CarrierWave::Blitline::ImageVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/blitline/image_version.rb

Overview

An instance of an ImageVersion for Blitline API.

 When the process() version is called in an Uploader class, we store the version
   name, and the block to create that version as an ImageVersion

 So, a 'small' image version that's resized to fit 200x200 looks like:

   ImageVersion.new("small") do
     process :resize_to_fit => [200, 200]
   end

NOTE: We need a 'default' image version too. This is created automatically, and
  the name for the version is 'nil'.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &block) ⇒ ImageVersion

Returns a new instance of ImageVersion.



24
25
26
27
28
29
# File 'lib/carrierwave/blitline/image_version.rb', line 24

def initialize(name = nil, &block)
  @name                = name
  @primary_function    = nil
  @secondary_functions = []
  instance_exec(&block) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/carrierwave/blitline/image_version.rb', line 22

def name
  @name
end

#primary_functionObject

Returns the value of attribute primary_function.



18
19
20
# File 'lib/carrierwave/blitline/image_version.rb', line 18

def primary_function
  @primary_function
end

#secondary_functionsObject

Returns the value of attribute secondary_functions.



20
21
22
# File 'lib/carrierwave/blitline/image_version.rb', line 20

def secondary_functions
  @secondary_functions
end

Instance Method Details

#primary_function_nameObject

Returns a String of the name of the primary function for this version

(default: "no_op")


56
57
58
# File 'lib/carrierwave/blitline/image_version.rb', line 56

def primary_function_name
  primary_function.nil? ? "no_op" : primary_function.name
end

#primary_function_paramsObject

Returns a Hash of the params of the primary function for this

version (default: {})


62
63
64
# File 'lib/carrierwave/blitline/image_version.rb', line 62

def primary_function_params
  primary_function.nil? ? {} : primary_function.params
end

#process(function_hash) ⇒ Object

Hijacks the process() method that’s called within a CarrierWave uploader.

Example:

 version :thumb do
   process :crop            => [10, 10, 200, 200]
   process :process_to_fill => [500, 500]
 end

This stores the crop function as the "primary" function, and the process_to_fill
  function as a "secondary" function.


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/carrierwave/blitline/image_version.rb', line 42

def process(function_hash)
  function_hash   = { function_hash => nil } unless function_hash.is_a?(Hash)
  function_name   = function_hash.keys.first
  function_params = function_hash.values.first
  function = Function.new(function_name, function_params)
  if primary_function.nil?
    self.primary_function    = function
  else
    secondary_functions << function
  end
end