Class: Dragonfly::TempObject

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/temp_object.rb

Overview

A TempObject is used for HOLDING DATA. It’s the thing that is passed between the datastore, the processor and the encoder, and is useful for separating how the data was created and how it is later accessed.

You can initialize it various ways:

temp_object = Dragonfly::TempObject.new('this is the content')           # with a String
temp_object = Dragonfly::TempObject.new(Pathname.new('path/to/content')) # with a Pathname
temp_object = Dragonfly::TempObject.new(File.new('path/to/content'))     # with a File
temp_object = Dragonfly::TempObject.new(some_tempfile)                   # with a Tempfile
temp_object = Dragonfly::TempObject.new(some_other_temp_object)          # with another TempObject

However, no matter how it was initialized, you can always access the data a number of ways:

temp_object.data      # returns a data string
temp_object.file      # returns a file object holding the data
temp_object.path      # returns a path for the file

The data/file are created lazily, something which you may wish to take advantage of.

For example, if a TempObject is initialized with a file, and temp_object.data is never called, then the data string will never be loaded into memory.

Conversely, if the TempObject is initialized with a data string, and neither temp_object.file nor temp_object.path are ever called, then the filesystem will never be hit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ TempObject

Instance Methods



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dragonfly/temp_object.rb', line 45

def initialize(obj)
  if obj.is_a? TempObject
    @data = obj.get_data
    @tempfile = obj.get_tempfile
    @pathname = obj.get_pathname
  elsif obj.is_a? String
    @data = obj
  elsif obj.is_a? Tempfile
    @tempfile = obj
  elsif obj.is_a? File
    @pathname = Pathname.new(obj.path)
    @original_filename = @pathname.basename.to_s
  elsif obj.is_a? Pathname
    @pathname = obj
    @original_filename = @pathname.basename.to_s
  elsif obj.respond_to?(:tempfile)
    @tempfile = obj.tempfile
  else
    raise ArgumentError, "#{self.class.name} must be initialized with a String, a Pathname, a File, a Tempfile, another TempObject, or something that responds to .tempfile"
  end
  @tempfile.close if @tempfile
  @original_filename = obj.original_filename if obj.respond_to?(:original_filename)
end

Instance Attribute Details

#original_filenameObject (readonly)

Returns the value of attribute original_filename



69
70
71
# File 'lib/dragonfly/temp_object.rb', line 69

def original_filename
  @original_filename
end

Instance Method Details

#dataObject



71
72
73
# File 'lib/dragonfly/temp_object.rb', line 71

def data
  @data ||= file{|f| f.read }
end

#each(&block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/dragonfly/temp_object.rb', line 107

def each(&block)
  to_io do |io|
    while part = io.read(block_size)
      yield part
    end
  end
end

#file(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dragonfly/temp_object.rb', line 87

def file(&block)
  f = tempfile.open
  tempfile.binmode
  if block_given?
    ret = yield f
    tempfile.close
  else
    ret = f
  end
  ret
end

#inspectObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/dragonfly/temp_object.rb', line 128

def inspect
  content_string = case
  when @data
    data_string = size > 20 ? "#{@data[0..20]}..." : @data
    "data=#{data_string.inspect}"
  when @pathname then "pathname=#{@pathname.inspect}"
  when @tempfile then "tempfile=#{@tempfile.inspect}"
  end
  to_s.sub(/>$/, " #{content_string} >")
end

#pathObject



99
100
101
# File 'lib/dragonfly/temp_object.rb', line 99

def path
  @pathname ? @pathname.expand_path.to_s : tempfile.path
end

#sizeObject



103
104
105
# File 'lib/dragonfly/temp_object.rb', line 103

def size
  @data ? @data.bytesize : File.size(path)
end

#tempfileObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dragonfly/temp_object.rb', line 75

def tempfile
  @tempfile ||= begin
    case
    when @data
      @tempfile = new_tempfile(@data)
    when @pathname
      @tempfile = copy_to_tempfile(@pathname.expand_path)
    end
    @tempfile
  end
end

#to_file(path) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/dragonfly/temp_object.rb', line 115

def to_file(path)
  if @data
    File.open(path, 'wb'){|f| f.write(@data) }
  else
    FileUtils.cp(self.path, path)
  end
  File.new(path, 'rb')
end

#to_io(&block) ⇒ Object



124
125
126
# File 'lib/dragonfly/temp_object.rb', line 124

def to_io(&block)
  @data ? StringIO.open(@data, 'rb', &block) : file(&block)
end