Module: Grumlin::Typing

Defined in:
lib/grumlin/typing.rb

Constant Summary collapse

TYPES =
{
  "g:List" => ->(value) { cast_list(value) },
  "g:Set" => ->(value) { cast_list(value).to_set },
  "g:Map" => ->(value) { cast_map(value) },
  "g:Vertex" => ->(value) { cast_entity(Grumlin::Vertex, value) },
  "g:Edge" => ->(value) { cast_entity(Grumlin::Edge, value) },
  "g:Path" => ->(value) { cast_entity(Grumlin::Path, value) },
  "g:Traverser" => ->(value) { cast_entity(Grumlin::Traverser, value) },
  "g:Property" => ->(value) { cast_entity(Grumlin::Property, value) },
  "g:Int64" => ->(value) { cast_int(value) },
  "g:Int32" => ->(value) { cast_int(value) },
  "g:Double" => ->(value) { cast_double(value) },
  "g:Direction" => ->(value) { value.to_sym },
  "g:VertexProperty" => ->(value) { cast_entity(Grumlin::VertexProperty, value) },
  "g:TraversalMetrics" => ->(value) { cast_map(value[:@value]) },
  "g:Metrics" => ->(value) { cast_map(value[:@value]) },
  "g:T" => ->(value) { Grumlin::Expressions::T.public_send(value) },
  "g:UUID" => ->(value) { value }
}.freeze
CASTABLE_TYPES =
[Hash, String, Integer, TrueClass, FalseClass, NilClass, Array].freeze

Class Method Summary collapse

Class Method Details

.cast(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grumlin/typing.rb', line 27

def cast(value)
  verify_type!(value)

  return value.map { |v| cast(v) } if value.is_a?(Array)

  return value unless value.is_a?(Hash)

  type = TYPES[value[:@type]]

  verify_castable_hash!(value, type)

  type.call(value[:@value])
end