Class: CarrierWave::Uploader::Versions::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/uploader/versions.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
11
12
# File 'lib/carrierwave/uploader/versions.rb', line 7

def initialize(name)
  @name = name
  @options = {}
  @blocks = []
  @klass = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/carrierwave/uploader/versions.rb', line 73

def method_missing(name, *args)
  super
rescue NoMethodError => e
  raise e.exception <<~ERROR
    #{e.message}
    If you're trying to configure a version, do it inside a block like `version(:thumb) { self.#{name} #{args.map(&:inspect).join(', ')} }`.
  ERROR
end

Instance Method Details

#build(superclass) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/carrierwave/uploader/versions.rb', line 20

def build(superclass)
  return @klass if @klass
  @klass = Class.new(superclass)
  superclass.const_set("#{@name.to_s.camelize}VersionUploader", @klass)

  @klass.version_names += [@name]
  @klass.versions = {}
  @klass.processors = []
  @klass.version_options = @options
  @klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # Define the enable_processing method for versions so they get the
    # value from the parent class unless explicitly overwritten
    def self.enable_processing(value=nil)
      self.enable_processing = value if value
      if defined?(@enable_processing) && !@enable_processing.nil?
        @enable_processing
      else
        superclass.enable_processing
      end
    end

    # Regardless of what is set in the parent uploader, do not enforce the
    # move_to_cache config option on versions because it moves the original
    # file to the version's target file.
    #
    # If you want to enforce this setting on versions, override this method
    # in each version:
    #
    # version :thumb do
    #   def move_to_cache
    #     true
    #   end
    # end
    #
    def move_to_cache
      false
    end

    # Need to rely on the parent version's identifier, as versions don't have its own one.
    def identifier
      parent_version.identifier
    end
  RUBY
  @blocks.each { |block| @klass.class_eval(&block) }
  @klass
end

#configure(options, &block) ⇒ Object



14
15
16
17
18
# File 'lib/carrierwave/uploader/versions.rb', line 14

def configure(options, &block)
  @options.merge!(options)
  @blocks << block if block
  @klass = nil
end

#deep_dupObject



67
68
69
70
71
# File 'lib/carrierwave/uploader/versions.rb', line 67

def deep_dup
  other = dup
  other.instance_variable_set(:@blocks, @blocks.dup)
  other
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/carrierwave/uploader/versions.rb', line 82

def respond_to_missing?(*)
  super
end