Class: Dragonfly::Content

Inherits:
Object show all
Extended by:
Forwardable
Includes:
HasFilename
Defined in:
lib/dragonfly/content.rb

Overview

A Dragonfly::Content object is responsible for holding

  1. content (in the form of a data string, file, tempfile, or path)

  2. metadata about the content (i.e. name, etc.)

Furthermore, it belongs to a Dragonfly app, so has access to its already registered generators, processors, analysers and datastore. It provides convenience methods for updating its content, and though the original data may have been in the form of a String, or a Pathname, etc. methods like “path”, “data” and “file” will always work regardless.

It is acted upon in generator, processor, analyser and datastore methods and provides a standard interface for updating content, no matter how that content first got there (whether in the form of a String/Pathname/File/etc.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasFilename

#basename, #basename=, #ext, #ext=

Constructor Details

#initialize(app, obj = "", meta = nil) ⇒ Content

Returns a new instance of Content



23
24
25
26
27
28
# File 'lib/dragonfly/content.rb', line 23

def initialize(app, obj="", meta=nil)
  @app = app
  @meta = {}
  @previous_temp_objects = []
  update(obj, meta)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app



35
36
37
# File 'lib/dragonfly/content.rb', line 35

def app
  @app
end

#metaHash

Returns:



42
43
44
# File 'lib/dragonfly/content.rb', line 42

def meta
  @meta
end

#temp_objectObject

Returns the value of attribute temp_object



39
40
41
# File 'lib/dragonfly/content.rb', line 39

def temp_object
  @temp_object
end

Instance Method Details

#add_meta(meta) ⇒ Object

Add to the meta (merge)

Parameters:

  • meta (Hash)
    • should be json-like, i.e. contain no types other than String, Number, Boolean



126
127
128
129
# File 'lib/dragonfly/content.rb', line 126

def add_meta(meta)
  self.meta.merge!(meta)
  self
end

#analyse(name) ⇒ Object

Analyse the content using a pre-registered analyser

Examples:

content.analyse(:width)  # ===> 280


106
107
108
# File 'lib/dragonfly/content.rb', line 106

def analyse(name)
  analyser_cache[name.to_s] ||= app.get_analyser(name).call(self)
end

#b64_dataString

Returns A data url representation of the data

Examples:

"data:image/jpeg;base64,IGSsdhfsoi..."

Returns:

  • (String)

    A data url representation of the data



186
187
188
# File 'lib/dragonfly/content.rb', line 186

def b64_data
  "data:#{mime_type};base64,#{Base64.encode64(data)}"
end

#closeObject



190
191
192
193
# File 'lib/dragonfly/content.rb', line 190

def close
  previous_temp_objects.each{|temp_object| temp_object.close }
  temp_object.close
end

#dataString

Returns the content data as a string (even if it was initialized with a file)

Returns:

  • (String)

    the content data as a string (even if it was initialized with a file)



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#fileFile

Returns the content as a readable file (even if it was initialized with data)

Examples:

content.file

With a block (it closes the file at the end)

content.file do |f|
  # do something with f
end

Returns:

  • (File)

    the content as a readable file (even if it was initialized with data)



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#generate!(name, *args) ⇒ Content

Set the content using a pre-registered generator

Examples:

content.generate!(:text, "some text")

Returns:



89
90
91
92
# File 'lib/dragonfly/content.rb', line 89

def generate!(name, *args)
  app.get_generator(name).call(self, *args)
  self
end

#initialize_copy(other) ⇒ Object

Used by 'dup' and 'clone'



31
32
33
# File 'lib/dragonfly/content.rb', line 31

def initialize_copy(other)
  self.meta = meta.dup
end

#inspectObject



195
196
197
# File 'lib/dragonfly/content.rb', line 195

def inspect
  "<#{self.class.name} temp_object=#{temp_object.inspect}>"
end

#mime_typeString

The mime-type taken from the name's file extension

Examples:

“image/jpeg”

Returns:

  • (String)


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

def mime_type
  app.mime_type_for(ext)
end

#nameString

Examples:

“beach.jpg”

Returns:

  • (String)


68
69
70
# File 'lib/dragonfly/content.rb', line 68

def name
  meta["name"] || temp_object.original_filename
end

#name=(name) ⇒ Object

Examples:

content.name = "beach.jpg"


74
75
76
# File 'lib/dragonfly/content.rb', line 74

def name=(name)
  meta["name"] = name
end

#pathString

Returns a file path for the content (even if it was initialized with data)

Returns:

  • (String)

    a file path for the content (even if it was initialized with data)



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#process!(name, *args) ⇒ Content

Update the content using a pre-registered processor

Examples:

content.process!(:convert, "-resize 300x300")

Returns:



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

def process!(name, *args)
  app.get_processor(name).call(self, *args)
  self
end

#shell_eval(opts = {}) ⇒ Object

Analyse the content using a shell command

Examples:

content.shell_eval do |path|
  "file --mime-type #{path}"
end
# ===> "beach.jpg: image/jpeg"

Parameters:

  • opts (Hash) (defaults to: {})

    passing :escape => false doesn't shell-escape each word



138
139
140
141
142
# File 'lib/dragonfly/content.rb', line 138

def shell_eval(opts={})
  should_escape = opts[:escape] != false
  command = yield(should_escape ? shell.quote(path) : path)
  run command, :escape => should_escape
end

#shell_generate(opts = {}) ⇒ Content

Set the content using a shell command

Examples:

content.shell_generate do |path|
  "/usr/local/bin/generate_text gumfry -o #{path}"
end

Parameters:

  • opts (Hash) (defaults to: {})

    :ext sets the file extension of the new path and :escape => false doesn't shell-escape each word

Returns:



151
152
153
154
155
156
157
158
159
# File 'lib/dragonfly/content.rb', line 151

def shell_generate(opts={})
  ext = opts[:ext] || self.ext
  should_escape = opts[:escape] != false
  tempfile = Utils.new_tempfile(ext)
  new_path = should_escape ? shell.quote(tempfile.path) : tempfile.path
  command = yield(new_path)
  run(command, :escape => should_escape)
  update(tempfile)
end

#shell_update(opts = {}) ⇒ Content

Update the content using a shell command

Examples:

content.shell_update do |old_path, new_path|
  "convert -resize 20x10 #{old_path} #{new_path}"
end

Parameters:

  • opts (Hash) (defaults to: {})

    :ext sets the file extension of the new path and :escape => false doesn't shell-escape each word

Returns:



168
169
170
171
172
173
174
175
176
177
# File 'lib/dragonfly/content.rb', line 168

def shell_update(opts={})
  ext = opts[:ext] || self.ext
  should_escape = opts[:escape] != false
  tempfile = Utils.new_tempfile(ext)
  old_path = should_escape ? shell.quote(path) : path
  new_path = should_escape ? shell.quote(tempfile.path) : tempfile.path
  command = yield(old_path, new_path)
  run(command, :escape => should_escape)
  update(tempfile)
end

#sizeFixnum

Returns the size in bytes

Returns:

  • (Fixnum)

    the size in bytes



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#store(opts = {}) ⇒ Object



179
180
181
# File 'lib/dragonfly/content.rb', line 179

def store(opts={})
  datastore.write(self, opts)
end

#to_fileFile

Returns a new file

Parameters:

  • path (String)

Returns:

  • (File)

    a new file



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#to_tempfileTempfile

Returns a new tempfile

Returns:



63
64
# File 'lib/dragonfly/content.rb', line 63

def_delegators :temp_object,
:data, :file, :tempfile, :path, :size, :each, :to_file, :to_tempfile

#update(obj, meta = nil) ⇒ Content

Update the content

Parameters:

  • obj (String, Pathname, Tempfile, File, Content, TempObject)

    can be any of these types

  • meta (Hash) (defaults to: nil)
    • should be json-like, i.e. contain no types other than String, Number, Boolean

Returns:



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

def update(obj, meta=nil)
  self.temp_object = TempObject.new(obj)
  original_filename = temp_object.original_filename
  self.meta['name'] ||= original_filename if original_filename
  clear_analyser_cache
  add_meta(obj.meta) if obj.respond_to?(:meta)
  add_meta(meta) if meta
  self
end