Class: BPL::Derivatives::TempfileService

Inherits:
Object
  • Object
show all
Defined in:
lib/bpl/derivatives/services/tempfile_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_object) ⇒ TempfileService

Returns a new instance of TempfileService.



11
12
13
14
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 11

def initialize(source_object)
  @source_object = source_object
  @source_file = nil
end

Instance Attribute Details

#source_fileObject (readonly)

Returns the value of attribute source_file.



9
10
11
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 9

def source_file
  @source_file
end

#source_objectObject (readonly)

Returns the value of attribute source_object.



9
10
11
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 9

def source_object
  @source_object
end

Class Method Details

.create(object, &block) ⇒ Object



5
6
7
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 5

def self.create(object, &block)
  new(object).tempfile(&block)
end

Instance Method Details

#default_tempfile(&_block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 32

def default_tempfile(&_block)
  Tempfile.open(filename_for_characterization) do |f|
    f.binmode
    if source_file.content.respond_to? :read
      f.write(source_file.content.read)
    else
      f.write(source_file.content)
    end
    source_file.content.rewind if source_file.content.respond_to? :rewind
    f.rewind
    yield(f)
  end
end

#tempfile(&block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/bpl/derivatives/services/tempfile_service.rb', line 24

def tempfile(&block)
  if source_file.respond_to? :to_tempfile
    source_file.send(:to_tempfile, &block)
  elsif source_file.has_content?
    default_tempfile(&block)
  end
end