class Dragonfly::Content

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.)

Attributes

app[R]
meta[RW]

@return [Hash]

previous_temp_objects[R]
temp_object[R]

Public Class Methods

new(app, obj="", meta=nil) click to toggle source
# File lib/dragonfly/content.rb, line 23
def initialize(app, obj="", meta=nil)
  @app = app
  @meta = {}
  @previous_temp_objects = []
  update(obj, meta)
end

Public Instance Methods

add_meta(meta) click to toggle source

Add to the meta (merge) @param meta [Hash] - should be json-like, i.e. contain no types other than String, Number, Boolean

# File lib/dragonfly/content.rb, line 126
def add_meta(meta)
  self.meta.merge!(meta)
  self
end
analyse(name) click to toggle source

Analyse the content using a pre-registered analyser @example

content.analyse(:width)  # ===> 280
# File lib/dragonfly/content.rb, line 106
def analyse(name)
  analyser_cache[name.to_s] ||= app.get_analyser(name).call(self)
end
b64_data() click to toggle source

@example

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

@return [String] A data url representation of the data

# File lib/dragonfly/content.rb, line 186
def b64_data
  "data:#{mime_type};base64,#{Base64.encode64(data)}"
end
close() click to toggle source
# File lib/dragonfly/content.rb, line 190
def close
  previous_temp_objects.each{|temp_object| temp_object.close }
  temp_object.close
end
generate!(name, *args) click to toggle source

Set the content using a pre-registered generator @example

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

@return [Content] self

# File lib/dragonfly/content.rb, line 89
def generate!(name, *args)
  app.get_generator(name).call(self, *args)
  self
end
initialize_copy(other) click to toggle source

Used by 'dup' and 'clone'

# File lib/dragonfly/content.rb, line 31
def initialize_copy(other)
  self.meta = meta.dup
end
inspect() click to toggle source
# File lib/dragonfly/content.rb, line 195
def inspect
  "<#{self.class.name} temp_object=#{temp_object.inspect}>"
end
mime_type() click to toggle source

The mime-type taken from the name's file extension @example “image/jpeg” @return [String]

# File lib/dragonfly/content.rb, line 81
def mime_type
  app.mime_type_for(ext)
end
name() click to toggle source

@example “beach.jpg” @return [String]

# File lib/dragonfly/content.rb, line 68
def name
  meta["name"]
end
name=(name) click to toggle source

@example

content.name = "beach.jpg"
# File lib/dragonfly/content.rb, line 74
def name=(name)
  meta["name"] = name
end
process!(name, *args) click to toggle source

Update the content using a pre-registered processor @example

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

@return [Content] self

# File lib/dragonfly/content.rb, line 98
def process!(name, *args)
  app.get_processor(name).call(self, *args)
  self
end
shell_eval(opts={}) { |should_escape ? quote: path| ... } click to toggle source

Analyse the content using a shell command @param opts [Hash] passing :escape => false doesn't shell-escape each word @example

content.shell_eval do |path|
  "file --mime-type #{path}"
end
# ===> "beach.jpg: image/jpeg"
# 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={}) { |new_path| ... } click to toggle source

Set the content using a shell command @param opts [Hash] :ext sets the file extension of the new path and :escape => false doesn't shell-escape each word @example

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

@return [Content] self

# 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={}) { |old_path, new_path| ... } click to toggle source

Update the content using a shell command @param opts [Hash] :ext sets the file extension of the new path and :escape => false doesn't shell-escape each word @example

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

@return [Content] self

# 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
store(opts={}) click to toggle source
# File lib/dragonfly/content.rb, line 179
def store(opts={})
  datastore.write(self, opts)
end
update(obj, meta=nil) click to toggle source

Update the content @param obj [String, Pathname, Tempfile, File, Content, TempObject] can be any of these types @param meta [Hash] - should be json-like, i.e. contain no types other than String, Number, Boolean @return [Content] self

# File lib/dragonfly/content.rb, line 114
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

Private Instance Methods

analyser_cache() click to toggle source
# File lib/dragonfly/content.rb, line 207
def analyser_cache
  @analyser_cache ||= {}
end
clear_analyser_cache() click to toggle source
# File lib/dragonfly/content.rb, line 211
def clear_analyser_cache
  analyser_cache.clear
end
run(command, opts) click to toggle source
# File lib/dragonfly/content.rb, line 215
def run(command, opts)
  shell.run(command, opts)
end
temp_object=(temp_object) click to toggle source
# File lib/dragonfly/content.rb, line 202
def temp_object=(temp_object)
  previous_temp_objects.push(@temp_object) if @temp_object
  @temp_object = temp_object
end