Class: Dragonfly::Content
Overview
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.)
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
- #meta ⇒ Hash
-
#temp_object ⇒ Object
readonly
Returns the value of attribute temp_object.
Instance Method Summary collapse
-
#add_meta(meta) ⇒ Object
Add to the meta (merge).
-
#analyse(name) ⇒ Object
Analyse the content using a pre-registered analyser.
-
#b64_data ⇒ String
A data url representation of the data.
- #close ⇒ Object
-
#data ⇒ String
The content data as a string (even if it was initialized with a file).
-
#file ⇒ File
The content as a readable file (even if it was initialized with data).
-
#generate!(name, *args) ⇒ Content
Set the content using a pre-registered generator.
-
#initialize(app, obj = "", meta = nil) ⇒ Content
constructor
A new instance of Content.
-
#initialize_copy(other) ⇒ Object
Used by ‘dup’ and ‘clone’.
- #inspect ⇒ Object
-
#mime_type ⇒ String
The mime-type taken from the name’s file extension.
- #name ⇒ String
- #name=(name) ⇒ Object
-
#path ⇒ String
A file path for the content (even if it was initialized with data).
-
#process!(name, *args) ⇒ Content
Update the content using a pre-registered processor.
-
#shell_eval(opts = {}) ⇒ Object
Analyse the content using a shell command.
-
#shell_generate(opts = {}) ⇒ Content
Set the content using a shell command.
-
#shell_update(opts = {}) ⇒ Content
Update the content using a shell command.
-
#size ⇒ Fixnum
The size in bytes.
- #store(opts = {}) ⇒ Object
-
#to_file ⇒ File
A new file.
-
#to_tempfile ⇒ Tempfile
A new tempfile.
-
#update(obj, meta = nil) ⇒ Content
Update the content.
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="", =nil) @app = app @meta = {} @previous_temp_objects = [] update(obj, ) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
35 36 37 |
# File 'lib/dragonfly/content.rb', line 35 def app @app end |
#temp_object ⇒ Object
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)
126 127 128 129 |
# File 'lib/dragonfly/content.rb', line 126 def () self..merge!() self end |
#analyse(name) ⇒ Object
Analyse the content using a pre-registered analyser
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_data ⇒ String
Returns 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 |
#close ⇒ Object
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 |
#data ⇒ String
Returns 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 |
#file ⇒ File
Returns 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
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. = .dup end |
#inspect ⇒ Object
195 196 197 |
# File 'lib/dragonfly/content.rb', line 195 def inspect "<#{self.class.name} temp_object=#{temp_object.inspect}>" end |
#mime_type ⇒ String
The mime-type taken from the name’s file extension
81 82 83 |
# File 'lib/dragonfly/content.rb', line 81 def mime_type app.mime_type_for(ext) end |
#name ⇒ String
68 69 70 |
# File 'lib/dragonfly/content.rb', line 68 def name ["name"] end |
#name=(name) ⇒ Object
74 75 76 |
# File 'lib/dragonfly/content.rb', line 74 def name=(name) ["name"] = name end |
#path ⇒ String
Returns 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
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
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
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
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 |
#size ⇒ Fixnum
Returns 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_file ⇒ File
Returns 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_tempfile ⇒ Tempfile
Returns a new tempfile.
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
114 115 116 117 118 119 120 121 122 |
# File 'lib/dragonfly/content.rb', line 114 def update(obj, =nil) ||= {} self.temp_object = TempObject.new(obj, ['name']) self.['name'] ||= temp_object.name if temp_object.name clear_analyser_cache (obj.) if obj.respond_to?(:meta) () self end |