Class: Saviour::BaseUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/base_uploader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BaseUploader

Returns a new instance of BaseUploader.



6
7
8
9
# File 'lib/saviour/base_uploader.rb', line 6

def initialize(opts = {})
  @data = opts.fetch(:data, {})
  @stash = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/saviour/base_uploader.rb', line 11

def method_missing(name, *args, &block)
  if @data.key?(name)
    @data[name]
  else
    super
  end
end

Class Method Details

.after_upload(&block) ⇒ Object



106
107
108
# File 'lib/saviour/base_uploader.rb', line 106

def after_upload(&block)
  after_upload_hooks.push(block)
end

.after_upload_hooksObject



110
111
112
# File 'lib/saviour/base_uploader.rb', line 110

def after_upload_hooks
  @after_upload ||= []
end

.process(name = nil, opts = {}, type = :memory, &block) ⇒ Object



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

def process(name = nil, opts = {}, type = :memory, &block)
  if block_given?
    processors.push(method_or_block: name || block, type: type)
  else
    processors.push(method_or_block: name || block, type: type, opts: opts)
  end
end

.process_with_file(name = nil, opts = {}, &block) ⇒ Object



94
95
96
# File 'lib/saviour/base_uploader.rb', line 94

def process_with_file(name = nil, opts = {}, &block)
  process(name, opts, :file, &block)
end

.processorsObject



76
77
78
# File 'lib/saviour/base_uploader.rb', line 76

def processors
  @processors ||= []
end

.storageObject



80
81
82
83
84
# File 'lib/saviour/base_uploader.rb', line 80

def storage
  @storage ||= Config.storage

  @storage.respond_to?(:call) ? @storage.call : @storage
end

.store_dir(name = nil, &block) ⇒ Object



98
99
100
# File 'lib/saviour/base_uploader.rb', line 98

def store_dir(name = nil, &block)
  store_dirs.push(name || block)
end

.store_dirsObject



72
73
74
# File 'lib/saviour/base_uploader.rb', line 72

def store_dirs
  @store_dirs ||= []
end

.with_storage(storage) ⇒ Object



102
103
104
# File 'lib/saviour/base_uploader.rb', line 102

def with_storage(storage)
  @storage = storage
end

Instance Method Details

#_process_as_contents(contents, filename) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/saviour/base_uploader.rb', line 23

def _process_as_contents(contents, filename)
  raise ConfigurationError, "Please use `store_dir` in the uploader" unless store_dir

  catch(:halt_process) do
    if Config.processing_enabled
      contents, filename = Uploader::ProcessorsRunner.new(self).run!(contents, filename)
    end

    path = ::File.join(store_dir, filename)

    [contents, path]
  end
end

#_process_as_file(file, filename) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/saviour/base_uploader.rb', line 37

def _process_as_file(file, filename)
  raise ConfigurationError, "Please use `store_dir` in the uploader" unless store_dir

  catch(:halt_process) do
    if Config.processing_enabled
      file, filename = Uploader::ProcessorsRunner.new(self).run_as_file!(file, filename)
    end

    path = ::File.join(store_dir, filename)

    [file, path]
  end
end

#halt_processObject



51
52
53
# File 'lib/saviour/base_uploader.rb', line 51

def halt_process
  throw(:halt_process)
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/saviour/base_uploader.rb', line 19

def respond_to_missing?(name, *)
  @data.key?(name) || super
end

#stash(hash) ⇒ Object



55
56
57
# File 'lib/saviour/base_uploader.rb', line 55

def stash(hash)
  @stash.deep_merge!(hash)
end

#stashedObject



59
60
61
# File 'lib/saviour/base_uploader.rb', line 59

def stashed
  @stash
end

#storageObject



67
68
69
# File 'lib/saviour/base_uploader.rb', line 67

def storage
  self.class.storage
end

#store_dirObject



63
64
65
# File 'lib/saviour/base_uploader.rb', line 63

def store_dir
  @store_dir ||= Uploader::StoreDirExtractor.new(self).store_dir
end