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(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, opts = {}) ⇒ TempObject

Instance Methods



43
44
45
46
47
48
# File 'lib/dragonfly/temp_object.rb', line 43

def initialize(obj, opts={})
  opts ||= {} # in case it's nil
  initialize_from_object!(obj)
  validate_options!(opts)
  extract_attributes_from(opts)
end

Instance Attribute Details

#formatObject

Returns the value of attribute format



93
94
95
# File 'lib/dragonfly/temp_object.rb', line 93

def format
  @format
end

#metaObject



97
98
99
# File 'lib/dragonfly/temp_object.rb', line 97

def meta
  @meta ||= {}
end

#nameObject

Returns the value of attribute name



93
94
95
# File 'lib/dragonfly/temp_object.rb', line 93

def name
  @name
end

Instance Method Details

#attributesObject



134
135
136
137
138
139
140
# File 'lib/dragonfly/temp_object.rb', line 134

def attributes
  {
    :name => name,
    :meta => meta,
    :format => format
  }
end

#basenameObject



101
102
103
# File 'lib/dragonfly/temp_object.rb', line 101

def basename
  File.basename(name, '.*') if name
end

#dataObject



50
51
52
# File 'lib/dragonfly/temp_object.rb', line 50

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

#each(&block) ⇒ Object



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

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

#extObject



105
106
107
# File 'lib/dragonfly/temp_object.rb', line 105

def ext
  File.extname(name)[/\.(.*)/, 1] if name
end

#extract_attributes_from(hash) ⇒ Object



142
143
144
145
146
# File 'lib/dragonfly/temp_object.rb', line 142

def extract_attributes_from(hash)
  self.name   = hash.delete(:name)     unless hash[:name].blank?
  self.format = hash.delete(:format)   unless hash[:format].blank?
  self.meta.merge!(hash.delete(:meta)) unless hash[:meta].blank?
end

#file(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dragonfly/temp_object.rb', line 69

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

#inspectObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/dragonfly/temp_object.rb', line 148

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

#pathObject



81
82
83
# File 'lib/dragonfly/temp_object.rb', line 81

def path
  tempfile.path
end

#sizeObject



85
86
87
88
89
90
91
# File 'lib/dragonfly/temp_object.rb', line 85

def size
  if initialized_data
    initialized_data.bytesize
  else
    File.size(path)
  end
end

#tempfileObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dragonfly/temp_object.rb', line 54

def tempfile
  @tempfile ||= begin
    case initialized_with
    when :tempfile
      @tempfile = initialized_tempfile
      @tempfile.close
    when :data
      @tempfile = new_tempfile(initialized_data)
    when :file
      @tempfile = copy_to_tempfile(initialized_file.path)
    end
    @tempfile
  end
end

#to_file(path) ⇒ Object



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

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

#to_io(&block) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/dragonfly/temp_object.rb', line 126

def to_io(&block)
  if initialized_data
    StringIO.open(initialized_data, 'rb', &block)
  else
    file(&block)
  end
end