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.
22 23 24 25 26 27 |
# File 'lib/dragonfly/content.rb', line 22 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.
34 35 36 |
# File 'lib/dragonfly/content.rb', line 34 def app @app end |
#temp_object ⇒ Object
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)
125 126 127 128 |
# File 'lib/dragonfly/content.rb', line 125 def () self..merge!() self end |
#analyse(name) ⇒ Object
Analyse the content using a pre-registered analyser
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_data ⇒ String
Returns 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 |
#close ⇒ Object
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 |
#data ⇒ String
Returns 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 |
#file ⇒ File
Returns 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
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. = .dup end |
#inspect ⇒ Object
194 195 196 |
# File 'lib/dragonfly/content.rb', line 194 def inspect "<#{self.class.name} temp_object=#{temp_object.inspect}>" end |
#mime_type ⇒ String
The mime-type taken from the name’s file extension
80 81 82 |
# File 'lib/dragonfly/content.rb', line 80 def mime_type ["mime_type"] || app.mime_type_for(ext) end |
#name ⇒ String
67 68 69 |
# File 'lib/dragonfly/content.rb', line 67 def name ["name"] end |
#name=(name) ⇒ Object
73 74 75 |
# File 'lib/dragonfly/content.rb', line 73 def name=(name) ["name"] = name end |
#path ⇒ String
Returns 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
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
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
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
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 |
#size ⇒ Fixnum
Returns 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_file ⇒ File
Returns 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_tempfile ⇒ Tempfile
Returns 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
113 114 115 116 117 118 119 120 121 |
# File 'lib/dragonfly/content.rb', line 113 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 |