Class: EchoUploads::WritableFile

Inherits:
Object
  • Object
show all
Defined in:
lib/echo_uploads/writable_file.rb

Overview

Used by ‘EchoUploads::PrmFileWriting#echo_uploads_write_prm_file`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, options) ⇒ WritableFile

Takes an EchoUploads::File.



24
25
26
27
28
29
# File 'lib/echo_uploads/writable_file.rb', line 24

def initialize(, options)
  tmp_name = SecureRandom.hex 10
  @tempfile = Tempfile.new 'echo_uploads', Rails.root.join('tmp')
  @metadata = 
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/echo_uploads/writable_file.rb', line 31

def method_missing(meth, *args)
  if @tempfile.respond_to? meth
    @tempfile.send meth, *args
  else
    super meth, *args
  end
end

Instance Attribute Details

#tempfileObject (readonly)

Returns the value of attribute tempfile.



39
40
41
# File 'lib/echo_uploads/writable_file.rb', line 39

def tempfile
  @tempfile
end

Instance Method Details

#closeObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/echo_uploads/writable_file.rb', line 4

def close
  # Using ActionDispatch::Http::UploadedFile is ugly. We use it because
  # EchoUploads::File#persist! expects that. In version 1 of this gem, we should
  # refactor. Everything should be more modular in general.
  uploaded_file = ActionDispatch::Http::UploadedFile.new(
    tempfile: @tempfile,
    filename: @metadata.original_filename
  )
  ActiveRecord::Base.transaction do
    # Duping the EchoUploads::File and destroy the prior one. This ensure that the
    # old data is cleaned out if necessary.
     = @metadata.dup
    @metadata.destroy
    .file = uploaded_file
    .persist! .owner_attr, @options
    @tempfile.close!
  end
end