Class: SurrealDB::RecordID

Inherits:
Object
  • Object
show all
Defined in:
lib/surrealdb/models/record_id.rb

Overview

Represents a SurrealDB record identifier (table + id).

A RecordID uniquely identifies a record within a table. The id component can be a string, integer, array, or object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, id) ⇒ RecordID

Returns a new instance of RecordID.

Parameters:

  • table (String)
  • id (Object)

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/surrealdb/models/record_id.rb', line 17

def initialize(table, id)
  raise ArgumentError, 'table must be a non-empty string' if table.nil? || table.to_s.empty?

  @table = table.to_s.freeze
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns record identifier (String, Integer, Array, Hash).

Returns:

  • (Object)

    record identifier (String, Integer, Array, Hash)



13
14
15
# File 'lib/surrealdb/models/record_id.rb', line 13

def id
  @id
end

#tableString (readonly)

Returns table name.

Returns:

  • (String)

    table name



10
11
12
# File 'lib/surrealdb/models/record_id.rb', line 10

def table
  @table
end

Class Method Details

.parse(str) ⇒ RecordID

Parses a "table:id" string into a RecordID.

Parameters:

  • str (String)

    e.g. "user:john", "post:123"

Returns:

Raises:

  • (ArgumentError)

    if the string is not a valid record ID



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/surrealdb/models/record_id.rb', line 29

def self.parse(str)
  parts = str.to_s.split(':', 2)
  raise ArgumentError, "invalid record ID: #{str}" if parts.length < 2 || parts[0].empty?

  table = parts[0]
  raw_id = parts[1]

  # Unwrap angle bracket escaping: ⟨...⟩
  raw_id = raw_id[1..-2] if raw_id.start_with?("\u27E8") && raw_id.end_with?("\u27E9")

  id = try_numeric(raw_id)
  new(table, id)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



52
53
54
# File 'lib/surrealdb/models/record_id.rb', line 52

def ==(other)
  other.is_a?(RecordID) && other.table == @table && other.id == @id
end

#hashObject



57
58
59
# File 'lib/surrealdb/models/record_id.rb', line 57

def hash
  [self.class, @table, @id].hash
end

#inspectObject



48
49
50
# File 'lib/surrealdb/models/record_id.rb', line 48

def inspect
  "SurrealDB::RecordID(#{self})"
end

#to_sObject



43
44
45
46
# File 'lib/surrealdb/models/record_id.rb', line 43

def to_s
  id_str = format_id(@id)
  "#{@table}:#{id_str}"
end