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.



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

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.



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

def app
  @app
end

#metaHash

Returns:



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

def meta
  @meta
end

#temp_objectObject

Returns the value of attribute temp_object.



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

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



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

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


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

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



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

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

#closeObject



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

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)



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

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)



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

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:



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

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

#initialize_copy(other) ⇒ Object

Used by ‘dup’ and ‘clone’



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

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

#inspectObject



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

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)


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

def mime_type
  meta["mime_type"] || app.mime_type_for(ext)
end

#nameString

Examples:

“beach.jpg”

Returns:

  • (String)


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

def name
  meta["name"]
end

#name=(name) ⇒ Object

Examples:

content.name = "beach.jpg"


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

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)



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

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!(:thumb, "300x300")

Returns:



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

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



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

def shell_eval(opts = {})
  should_escape = opts[:escape] != false
  command = yield(should_escape ? shell.escape(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:



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

def shell_generate(opts = {})
  ext = opts[:ext] || self.ext
  should_escape = opts[:escape] != false
  tempfile = Utils.new_tempfile(ext)
  new_path = should_escape ? shell.escape(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:



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

def shell_update(opts = {})
  ext = opts[:ext] || self.ext
  should_escape = opts[:escape] != false
  tempfile = Utils.new_tempfile(ext)
  old_path = should_escape ? shell.escape(path) : path
  new_path = should_escape ? shell.escape(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



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

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

#store(opts = {}) ⇒ Object



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

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

#to_fileFile

Returns a new file.

Parameters:

  • path (String)

Returns:

  • (File)

    a new file



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

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

#to_tempfileTempfile

Returns a new tempfile.

Returns:

  • (Tempfile)

    a new tempfile



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

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:



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

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