Class: Engram::Provenance::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/engram/provenance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_id:, source_type:, message_index:, role:, spans:, alignment:) ⇒ Source

Returns a new instance of Source.

Raises:

  • (ArgumentError)


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 message_index.is_a?(Integer) && !message_index.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 = 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

#alignmentObject (readonly)

Returns the value of attribute alignment.



48
49
50
# File 'lib/engram/provenance.rb', line 48

def alignment
  @alignment
end

#message_indexObject (readonly)

Returns the value of attribute message_index.



48
49
50
# File 'lib/engram/provenance.rb', line 48

def message_index
  @message_index
end

#roleObject (readonly)

Returns the value of attribute role.



48
49
50
# File 'lib/engram/provenance.rb', line 48

def role
  @role
end

#source_idObject (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_typeObject (readonly)

Returns the value of attribute source_type.



48
49
50
# File 'lib/engram/provenance.rb', line 48

def source_type
  @source_type
end

#spansObject (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

#hashObject



83
# File 'lib/engram/provenance.rb', line 83

def hash = to_h.hash

#to_hObject



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"] = message_index
  data["role"] = role
  data
end