Class: Dse::Graph::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dse/graph/result.rb

Overview

An individual result of running a graph query. It wraps the JSON result and provides access to it as a hash. It also supports casting the result into a known domain object type: Vertex, Edge, and Path currently.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject (readonly)

Returns hash representation of the JSON result of a graph query if it's a complex result. A simple value otherwise.

Returns:

  • hash representation of the JSON result of a graph query if it's a complex result. A simple value otherwise



20
21
22
# File 'lib/dse/graph/result.rb', line 20

def value
  @value
end

Instance Method Details

#as_edgeEdge

Coerce this result into an Edge object.

Returns:

  • (Edge)

    an edge domain object.

Raises:

  • (ArgumentError)

    if the result data does not represent an edge.



52
53
54
55
56
57
# File 'lib/dse/graph/result.rb', line 52

def as_edge
  Cassandra::Util.assert_instance_of(::Hash, @value)
  Dse::Graph::Edge.new(@value['id'], @value['label'], @value.fetch('properties', {}),
                       @value['inV'], @value['inVLabel'],
                       @value['outV'], @value['outVLabel'])
end

#as_pathPath

Coerce this result into a Path object.

Returns:

  • (Path)

    a path domain object.



61
62
63
64
# File 'lib/dse/graph/result.rb', line 61

def as_path
  Cassandra::Util.assert_instance_of(::Hash, @value)
  Dse::Graph::Path.new(@value['labels'], @value['objects'])
end

#as_vertexVertex

Coerce this result into a Vertex object.

Returns:

  • (Vertex)

    a vertex domain object

Raises:

  • (ArgumentError)

    if the result data does not represent a vertex



44
45
46
47
# File 'lib/dse/graph/result.rb', line 44

def as_vertex
  Cassandra::Util.assert_instance_of(::Hash, @value)
  Dse::Graph::Vertex.new(@value['id'], @value['label'], @value.fetch('properties', {}))
end

#castVertex, ...

Coerce this result into a domain object if possible.

Returns:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dse/graph/result.rb', line 29

def cast
  type = @value['type'] if @value.is_a?(Hash)
  case type
  when 'vertex'
    as_vertex
  when 'edge'
    as_edge
  else
    self
  end
end