Class: Texd::AttachmentList
- Inherits:
-
Object
- Object
- Texd::AttachmentList
- Defined in:
- lib/texd/attachment.rb
Overview
AttachmentList will contain file references used in tex documents. This class is commonly interacted with in the ‘attach_file` view helper:
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#lookup_context ⇒ Object
readonly
Returns the value of attribute lookup_context.
Instance Method Summary collapse
-
#attach(path, rename = true) ⇒ Attachment::File
private
Adds a file with the given ‘path` to the list.
-
#initialize(lookup_context) ⇒ AttachmentList
constructor
A new instance of AttachmentList.
-
#inline(contents, path) ⇒ <Type>
Includes a file with the given ‘content` to the list.
-
#main_input(contents) ⇒ String
private
Adds main input file for the render request.
-
#reference(path, rename = false) ⇒ Attachment::Reference
private
Adds a file reference with the given path to the list.
-
#to_upload_ios(unknown_reference_ids = nil) ⇒ Object
private
Transforms this list to UploadIO objects suitable for Texd::Client#render.
Constructor Details
#initialize(lookup_context) ⇒ AttachmentList
Returns a new instance of AttachmentList.
27 28 29 30 |
# File 'lib/texd/attachment.rb', line 27 def initialize(lookup_context) @items = {} @lookup_context = lookup_context end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
23 24 25 |
# File 'lib/texd/attachment.rb', line 23 def items @items end |
#lookup_context ⇒ Object (readonly)
Returns the value of attribute lookup_context.
24 25 26 |
# File 'lib/texd/attachment.rb', line 24 def lookup_context @lookup_context end |
Instance Method Details
#attach(path, rename = true) ⇒ Attachment::File
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a file with the given ‘path` to the list. The file path must either be an absolute filename or be relative to app/tex/ of the host application. See `Texd::LookupContext` for details.
The output file name will be mangled, unless ‘rename` specifies a name to use. Setting `rename` to false also disables renaming (the output will then use the file’s basename unaltered).
Note: Adding the same ‘path` twice with different arguments will have no effect: The returned value on the second attempt will be the same as on the first one.
52 53 54 |
# File 'lib/texd/attachment.rb', line 52 def attach(path, rename = true) # rubocop:disable Style/OptionalBooleanParameter add(Attachment::File, path, rename) end |
#inline(contents, path) ⇒ <Type>
Includes a file with the given ‘content` to the list. This is useful if you generate the content on-the-fly (like creating CSV data from database records), and don’t want to save that content to disk first.
In short, these examples should be mostly equivalent:
# use a temporary file to write contents to, and attach that file:
tmp = Tempfile.new ["foo", ".csv"]
tmp.write @record.to_csv
tmp.flush
tmp.close
text_attach tmp.name
# skip writing to disk and inline the file:
texd_inline @record.to_csv, "foo.csv"
76 77 78 79 80 81 |
# File 'lib/texd/attachment.rb', line 76 def inline(contents, path) att = Attachment::Dynamic.new(path, contents) items[att.name] ||= att items[att.name] end |
#main_input(contents) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds main input file for the render request. The returned name for this file is either “input.tex”, or (if that name already exist) some alternative. You should add the return value as input parameter to the client’s render call.
119 120 121 122 123 124 125 126 127 |
# File 'lib/texd/attachment.rb', line 119 def main_input(contents) name = "input.tex" i = 0 name = format("doc%05d.tex", i += 1) while items.key?(name) att = Attachment::Dynamic.new(name, contents) items[name] = att items[name] end |
#reference(path, rename = false) ⇒ Attachment::Reference
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a file reference with the given path to the list. Similar to #attach, the file path must either be an absolute filename or be relative to app/tex/ of the host application.
File name mangling applies as well, with the same rules as in #attach.
File references allow to reduce the amount of data sent to the texd server instance, by initially only sending the file’s checksums. If the server can identify that checksum from an internal store, it’ll use the stored file. Otherwise we receive a list of unknown references, and can retry the render request with the missing content attached in full.
References will be stored on the server (for some amount of time), so you should only attach static files, which change seldomly. Do not add dynamic content.
107 108 109 |
# File 'lib/texd/attachment.rb', line 107 def reference(path, rename = false) # rubocop:disable Style/OptionalBooleanParameter add(Attachment::Reference, path, rename) end |
#to_upload_ios(unknown_reference_ids = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Transforms this list to UploadIO objects suitable for Texd::Client#render.
133 134 135 136 137 138 139 140 141 |
# File 'lib/texd/attachment.rb', line 133 def to_upload_ios(unknown_reference_ids = nil) items.values.each_with_object({}) { |att, ios| ios[att.name] = if unknown_reference_ids && att.is_a?(Attachment::Reference) att.to_upload_io(full: unknown_reference_ids.include?(att.checksum)) else att.to_upload_io end } end |