Class: Engram::Provenance::Source
- Inherits:
-
Object
- Object
- Engram::Provenance::Source
- Defined in:
- lib/engram/provenance.rb
Instance Attribute Summary collapse
-
#alignment ⇒ Object
readonly
Returns the value of attribute alignment.
-
#message_index ⇒ Object
readonly
Returns the value of attribute message_index.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#source_id ⇒ Object
readonly
Returns the value of attribute source_id.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
-
#spans ⇒ Object
readonly
Returns the value of attribute spans.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(source_id:, source_type:, message_index:, role:, spans:, alignment:) ⇒ Source
constructor
A new instance of Source.
- #to_h ⇒ Object
Constructor Details
#initialize(source_id:, source_type:, message_index:, role:, spans:, alignment:) ⇒ Source
Returns a new instance of Source.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/engram/provenance.rb', line 50 def initialize(source_id:, source_type:, message_index:, role:, spans:, alignment:) raise ArgumentError, "source_id is required" if source_id.to_s.strip.empty? raise ArgumentError, "source_type is required" if source_type.to_s.strip.empty? unless .is_a?(Integer) && !.negative? raise ArgumentError, "message_index must be a non-negative integer" end raise ArgumentError, "role is required" if role.to_s.strip.empty? unless alignment.is_a?(String) || alignment.is_a?(Symbol) raise ArgumentError, "unknown alignment #{alignment.inspect}" end normalized_alignment = alignment.to_sym raise ArgumentError, "unknown alignment #{alignment.inspect}" unless ALIGNMENTS.include?(normalized_alignment) @source_id = source_id.to_s.dup.freeze @source_type = source_type.to_s.dup.freeze @message_index = @role = role.to_s.dup.freeze raise ArgumentError, "spans must be an array" unless spans.is_a?(Array) raise ArgumentError, "spans must contain at least one span" if spans.empty? @spans = spans.map do |span| raise ArgumentError, "spans must contain Provenance::Span values" unless span.is_a?(Span) span end.freeze @alignment = normalized_alignment freeze end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
Returns the value of attribute alignment.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def alignment @alignment end |
#message_index ⇒ Object (readonly)
Returns the value of attribute message_index.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def @message_index end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def role @role end |
#source_id ⇒ Object (readonly)
Returns the value of attribute source_id.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def source_id @source_id end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def source_type @source_type end |
#spans ⇒ Object (readonly)
Returns the value of attribute spans.
48 49 50 |
# File 'lib/engram/provenance.rb', line 48 def spans @spans end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
78 79 80 |
# File 'lib/engram/provenance.rb', line 78 def ==(other) other.is_a?(self.class) && to_h == other.to_h end |
#hash ⇒ Object
83 |
# File 'lib/engram/provenance.rb', line 83 def hash = to_h.hash |
#to_h ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/engram/provenance.rb', line 85 def to_h data = { "source_id" => source_id, "source_type" => source_type, "spans" => spans.map(&:to_h), "alignment" => alignment.to_s } data["message_index"] = data["role"] = role data end |