class LittleGhost::Artifact
Represents a file, image, or document produced by a Tool or supplied to a Run. LittleGhost sends supported media to the model once and may store the same bytes for filesystem Tools. Inline artifacts contain their bytes. Deferred artifacts contain an application-defined reference that the block passed to Configuration#artifacts may use to load the bytes.
image = LittleGhost::Artifact.new( data: File.binread("chart.png"), media_type: "image/png", name: "chart.png" ) download = LittleGhost::Artifact.deferred( reference: {file_id: "file-481"}, media_type: "application/pdf", name: "report.pdf" )
Attributes
Known byte count, otherwise nil for an unresolved artifact.
Binary content for an inline artifact, otherwise nil.
MIME media type used to present the artifact.
Deeply frozen application metadata.
Optional display filename.
Application-defined deferred reference, or generated Workspace reference after storage; otherwise nil.
Public Class Methods
# File lib/little_ghost/artifact.rb, line 39 def self.deferred(reference:, media_type:, name: nil, metadata: {}) raise ArgumentError, "artifact reference is required" if reference.nil? reference = immutable_reference(reference) allocate.tap do |artifact| artifact.__send__( :initialize_fields, data: nil, reference:, media_type:, name:, bytes: nil, metadata: ) end end
Creates an artifact whose bytes may be loaded later by the block passed to Configuration#artifacts.
# File lib/little_ghost/artifact.rb, line 23 def initialize(data:, media_type:, name: nil, metadata: {}) data = String(data).b initialize_fields( data: data.freeze, reference: nil, media_type:, name:, bytes: data.bytesize, metadata: ) rescue TypeError raise ArgumentError, "artifact data must be a string" end
Creates an inline artifact from binary data and a MIME media_type.
Public Instance Methods
Source
# File lib/little_ghost/artifact.rb, line 77 def ==(other) other.instance_of?(self.class) && [data, reference, name, media_type, bytes, metadata] == [other.data, other.reference, other.name, other.media_type, other.bytes, other.metadata] end
Compares all immutable artifact fields.
Source
# File lib/little_ghost/artifact.rb, line 74 def deferred? = !reference.nil? && bytes.nil?
Whether this artifact requires an application resolver.
Source
# File lib/little_ghost/artifact.rb, line 86 def hash = [self.class, data, reference, name, media_type, bytes, metadata].hash
Computes a Hash key from all immutable artifact fields.
Source
# File lib/little_ghost/artifact.rb, line 71 def inline? = !data.nil?
Whether this artifact contains its bytes directly.
Source
# File lib/little_ghost/artifact.rb, line 89 def inspect kind = if inline? "inline" elsif deferred? "deferred" else "stored" end "#<#{self.class} kind=#{kind.inspect} media_type=#{media_type.inspect} bytes=#{bytes.inspect}>" end
Avoids placing bytes, names, metadata, or deferred references in diagnostics.