Module: LoraRuby::Types

Defined in:
lib/lora_ruby/types.rb

Overview

Shared value model — aligned with the ‘LoraValue` contract used by `lora-node`, `lora-wasm`, and `lora-python`.

  • Scalars pass through as Ruby natives (‘nil`, `true`, `false`, `Integer`, `Float`, `String`).

  • Lists and maps come back as ‘Array` / `Hash` (string keys).

  • Graph, temporal, spatial, vector, and binary values come back as plain ‘Hash`es with a `“kind”` discriminator.

If you want to narrow a value explicitly, use the ‘node?` / `point?` / `temporal?` helpers below.

Constant Summary collapse

SRID_CARTESIAN_2D =
7203
SRID_CARTESIAN_3D =
9157
SRID_WGS84_2D =
4326
SRID_WGS84_3D =
4979
CRS_CARTESIAN_2D =
"cartesian"
CRS_CARTESIAN_3D =
"cartesian-3D"
CRS_WGS84_2D =
"WGS-84-2D"
CRS_WGS84_3D =
"WGS-84-3D"
TEMPORAL_KINDS =
%w[date time localtime datetime localdatetime duration].freeze
VECTOR_COORD_TYPES =
%w[FLOAT64 FLOAT32 INTEGER INTEGER32 INTEGER16 INTEGER8].freeze

Class Method Summary collapse

Class Method Details

.binary(segments) ⇒ Object


Binary / blob constructor — segmented bytes. The native extension accepts each segment as a Ruby String and preserves segment boundaries in WAL/snapshot storage.




65
66
67
68
69
70
71
72
# File 'lib/lora_ruby/types.rb', line 65

def binary(segments)
  copied = segments.map { |segment| segment.b.dup }
  {
    "kind"     => "binary",
    "length"   => copied.sum(&:bytesize),
    "segments" => copied,
  }
end

.binary?(v) ⇒ Boolean

Returns:

  • (Boolean)


140
# File 'lib/lora_ruby/types.rb', line 140

def binary?(v)       = tagged?(v, "binary")

.cartesian(x, y) ⇒ Object


Spatial constructors — mirrors lora_python.cartesian / wgs84. ‘cartesian(1, 2)` returns a 2D cartesian point; use `cartesian_3d` for the 3D variant. WGS-84 variants carry `longitude` / `latitude` aliases alongside `x` / `y` so result-side consumers can read either without conversion.




82
83
84
85
86
87
88
89
90
# File 'lib/lora_ruby/types.rb', line 82

def cartesian(x, y)
  {
    "kind" => "point",
    "srid" => SRID_CARTESIAN_2D,
    "crs"  => CRS_CARTESIAN_2D,
    "x"    => x.to_f,
    "y"    => y.to_f,
  }
end

.cartesian_3d(x, y, z) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/lora_ruby/types.rb', line 92

def cartesian_3d(x, y, z)
  {
    "kind" => "point",
    "srid" => SRID_CARTESIAN_3D,
    "crs"  => CRS_CARTESIAN_3D,
    "x"    => x.to_f,
    "y"    => y.to_f,
    "z"    => z.to_f,
  }
end

.date(iso) ⇒ Object


Temporal constructors — ISO-8601 tagged Hashes. The native extension normalises + validates these on the way into the engine; invalid ISO strings raise ‘LoraRuby::InvalidParamsError`.




38
# File 'lib/lora_ruby/types.rb', line 38

def date(iso)          = { "kind" => "date",          "iso" => iso }

.datetime(iso) ⇒ Object



41
# File 'lib/lora_ruby/types.rb', line 41

def datetime(iso)      = { "kind" => "datetime",      "iso" => iso }

.duration(iso) ⇒ Object



43
# File 'lib/lora_ruby/types.rb', line 43

def duration(iso)      = { "kind" => "duration",      "iso" => iso }

.kind_of(hash) ⇒ Object



152
153
154
# File 'lib/lora_ruby/types.rb', line 152

def kind_of(hash)
  hash["kind"] || hash[:kind]
end

.localdatetime(iso) ⇒ Object



42
# File 'lib/lora_ruby/types.rb', line 42

def localdatetime(iso) = { "kind" => "localdatetime", "iso" => iso }

.localtime(iso) ⇒ Object



40
# File 'lib/lora_ruby/types.rb', line 40

def localtime(iso)     = { "kind" => "localtime",     "iso" => iso }

.node?(v) ⇒ Boolean


Guards — duck-typed narrowing helpers. Accept symbol-keyed and string-keyed Hashes because that’s what callers might build up manually (the native extension always emits string keys).


Returns:

  • (Boolean)


135
# File 'lib/lora_ruby/types.rb', line 135

def node?(v)         = tagged?(v, "node")

.path?(v) ⇒ Boolean

Returns:

  • (Boolean)


137
# File 'lib/lora_ruby/types.rb', line 137

def path?(v)         = tagged?(v, "path")

.point?(v) ⇒ Boolean

Returns:

  • (Boolean)


138
# File 'lib/lora_ruby/types.rb', line 138

def point?(v)        = tagged?(v, "point")

.relationship?(v) ⇒ Boolean

Returns:

  • (Boolean)


136
# File 'lib/lora_ruby/types.rb', line 136

def relationship?(v) = tagged?(v, "relationship")

.tagged?(v, expected) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/lora_ruby/types.rb', line 147

def tagged?(v, expected)
  return false unless v.is_a?(Hash)
  kind_of(v) == expected
end

.temporal?(v) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/lora_ruby/types.rb', line 142

def temporal?(v)
  return false unless v.is_a?(Hash)
  TEMPORAL_KINDS.include?(kind_of(v))
end

.time(iso) ⇒ Object



39
# File 'lib/lora_ruby/types.rb', line 39

def time(iso)          = { "kind" => "time",          "iso" => iso }

.vector(values, dimension, coordinate_type) ⇒ Object


Vector constructor — returns the canonical tagged Hash that the engine accepts as a VECTOR parameter and emits on return.




50
51
52
53
54
55
56
57
# File 'lib/lora_ruby/types.rb', line 50

def vector(values, dimension, coordinate_type)
  {
    "kind"           => "vector",
    "dimension"      => dimension,
    "coordinateType" => coordinate_type.to_s,
    "values"         => values.dup,
  }
end

.vector?(v) ⇒ Boolean

Returns:

  • (Boolean)


139
# File 'lib/lora_ruby/types.rb', line 139

def vector?(v)       = tagged?(v, "vector")

.wgs84(longitude, latitude) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lora_ruby/types.rb', line 103

def wgs84(longitude, latitude)
  {
    "kind"      => "point",
    "srid"      => SRID_WGS84_2D,
    "crs"       => CRS_WGS84_2D,
    "x"         => longitude.to_f,
    "y"         => latitude.to_f,
    "longitude" => longitude.to_f,
    "latitude"  => latitude.to_f,
  }
end

.wgs84_3d(longitude, latitude, height) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/lora_ruby/types.rb', line 115

def wgs84_3d(longitude, latitude, height)
  {
    "kind"      => "point",
    "srid"      => SRID_WGS84_3D,
    "crs"       => CRS_WGS84_3D,
    "x"         => longitude.to_f,
    "y"         => latitude.to_f,
    "z"         => height.to_f,
    "longitude" => longitude.to_f,
    "latitude"  => latitude.to_f,
    "height"    => height.to_f,
  }
end