class Dragonfly::Content
A Dragonfly::Content object is responsible for holding
-
content (in the form of a data string, file, tempfile, or path)
-
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
@return [Hash]
Public Class Methods
# 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 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 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
@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
# File lib/dragonfly/content.rb, line 190 def close previous_temp_objects.each{|temp_object| temp_object.close } temp_object.close end
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
Used by 'dup' and 'clone'
# File lib/dragonfly/content.rb, line 31 def initialize_copy(other) self.meta = meta.dup end
# File lib/dragonfly/content.rb, line 195 def inspect "<#{self.class.name} temp_object=#{temp_object.inspect}>" end
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
@example “beach.jpg” @return [String]
# File lib/dragonfly/content.rb, line 68 def name meta["name"] end
@example
content.name = "beach.jpg"
# File lib/dragonfly/content.rb, line 74 def name=(name) meta["name"] = name end
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
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
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
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
# File lib/dragonfly/content.rb, line 179 def store(opts={}) datastore.write(self, opts) end
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
# File lib/dragonfly/content.rb, line 207 def analyser_cache @analyser_cache ||= {} end
# File lib/dragonfly/content.rb, line 211 def clear_analyser_cache analyser_cache.clear end
# File lib/dragonfly/content.rb, line 215 def run(command, opts) shell.run(command, opts) end
# 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