Skip to content
This repository
Browse code

Add payloads to elements.

  • Loading branch information...
commit d8c4ee6f6ce46dab0e682e2583e67249ff472898 1 parent 49112be
Darrick Wiebe authored
44 lib/pacer/blueprints/payload_elements.rb
... ... @@ -0,0 +1,44 @@
  1 +module Pacer
  2 + module Payload
  3 + class Element
  4 + include com.tinkerpop.blueprints.Element
  5 + extend Forwardable
  6 +
  7 + def initialize(element, payload = nil)
  8 + @element = element
  9 + self.payload = payload
  10 + end
  11 +
  12 + def inspect
  13 + "#<Payload #{ element.inspect } -- #{ payload.inspect }>"
  14 + end
  15 +
  16 + attr_reader :element
  17 + attr_accessor :payload
  18 + end
  19 +
  20 + class Edge < Element
  21 + include com.tinkerpop.blueprints.Edge
  22 +
  23 + def_delegators :@element,
  24 + # Object
  25 + :equals, :toString, :hashCode,
  26 + # Element
  27 + :getId, :getPropertyKeys, :getProperty, :setProperty, :removeProperty, :getRawElement,
  28 + # Edge
  29 + :getLabel, :getVertex, :getRawEdge
  30 + end
  31 +
  32 + class Vertex < Element
  33 + include com.tinkerpop.blueprints.Vertex
  34 +
  35 + def_delegators :@element,
  36 + # Object
  37 + :equals, :toString, :hashCode,
  38 + # Element
  39 + :getId, :getPropertyKeys, :getProperty, :setProperty, :removeProperty, :getRawElement,
  40 + # Vertex
  41 + :getEdges, :getVertices, :query, :getRawVertex
  42 + end
  43 + end
  44 +end
12 lib/pacer/core/graph/path_route.rb
@@ -29,6 +29,18 @@ def subgraph(target_graph = nil, opts = {})
29 29 target_graph
30 30 end
31 31
  32 + def payloads
  33 + map do |path|
  34 + path.map do |e|
  35 + if e.is_a? Pacer::Payload::Element
  36 + e.payload
  37 + elsif e.is_a? Pacer::Wrappers::ElementWrapper
  38 + e.element_payload
  39 + end
  40 + end
  41 + end
  42 + end
  43 +
32 44 protected
33 45
34 46 def configure_iterator(iter)
3  lib/pacer/loader.rb
@@ -48,6 +48,8 @@ module SideEffect end
48 48 require 'pacer/blueprints/ruby_graph'
49 49 require 'pacer/blueprints/multi_graph'
50 50
  51 +require 'pacer/blueprints/payload_elements'
  52 +
51 53 require 'pacer/support/array_list'
52 54 require 'pacer/support/enumerable'
53 55 require 'pacer/support/proc'
@@ -86,6 +88,7 @@ module SideEffect end
86 88 require 'pacer/transform/scatter'
87 89 require 'pacer/transform/has_count_cap'
88 90 require 'pacer/transform/sort_section'
  91 +require 'pacer/transform/payload'
89 92
90 93 require 'pacer/side_effect/aggregate'
91 94 require 'pacer/side_effect/as'
50 lib/pacer/transform/payload.rb
... ... @@ -0,0 +1,50 @@
  1 +module Pacer::Core::Graph::ElementRoute
  2 + def payload(&block)
  3 + chain_route transform: :payload, block: block
  4 + end
  5 +end
  6 +
  7 +module Pacer::Transform
  8 + module Payload
  9 + attr_accessor :block
  10 +
  11 + protected
  12 +
  13 + def attach_pipe(end_pipe)
  14 + pipe = PayloadPipe.new(self, block)
  15 + pipe.setStarts end_pipe if end_pipe
  16 + pipe
  17 + end
  18 +
  19 + class PayloadPipe < Pacer::Pipes::RubyPipe
  20 + field_reader :currentEnd
  21 +
  22 + attr_reader :block, :wrapper
  23 +
  24 + def initialize(route, block)
  25 + super()
  26 + if route.element_type == :edge
  27 + @wrapper = Pacer::Payload::Edge
  28 + elsif route.element_type == :vertex
  29 + @wrapper = Pacer::Payload::Vertex
  30 + else
  31 + fail Pacer::ClientError, 'Can not use PayloadPipe on non-element data'
  32 + end
  33 + block ||= proc { |el| nil }
  34 + @block = Pacer::Wrappers::WrappingPipeFunction.new route, block
  35 + end
  36 +
  37 + def processNextStart
  38 + el = starts.next
  39 + @wrapper.new el, block.call(el)
  40 + end
  41 +
  42 + def getPathToHere
  43 + path = super
  44 + path.remove path.size - 1
  45 + path.add currentEnd
  46 + path
  47 + end
  48 + end
  49 + end
  50 +end
9 lib/pacer/wrappers/edge_wrapper.rb
@@ -4,9 +4,12 @@ class EdgeWrapper < ElementWrapper
4 4 include Pacer::Core::Graph::EdgesRoute
5 5
6 6 def_delegators :@element,
7   - :getId, :getLabel, :getPropertyKeys, :getProperty, :setProperty, :removeProperty,
8   - :getVertex,
9   - :getRawEdge
  7 + # Object
  8 + :equals, :toString, :hashCode,
  9 + # Element
  10 + :getId, :getPropertyKeys, :getProperty, :setProperty, :removeProperty, :getRawElement,
  11 + # Edge
  12 + :getLabel, :getVertex, :getRawEdge
10 13
11 14 class << self
12 15 def wrappers
4 lib/pacer/wrappers/element_wrapper.rb
@@ -160,6 +160,10 @@ def <=>(other)
160 160 display_name.to_s <=> other.display_name.to_s
161 161 end
162 162
  163 + def element_payload
  164 + element.payload if element.is_a? Pacer::Payload::Element
  165 + end
  166 +
163 167 protected
164 168
165 169 def after_initialize
9 lib/pacer/wrappers/vertex_wrapper.rb
@@ -4,9 +4,12 @@ class VertexWrapper < ElementWrapper
4 4 include Pacer::Core::Graph::VerticesRoute
5 5
6 6 def_delegators :@element,
7   - :getId, :getPropertyKeys, :getProperty, :setProperty, :removeProperty,
8   - :getEdges,
9   - :getRawVertex
  7 + # Object
  8 + :equals, :toString, :hashCode,
  9 + # Element
  10 + :getId, :getPropertyKeys, :getProperty, :setProperty, :removeProperty, :getRawElement,
  11 + # Vertex
  12 + :getEdges, :getVertices, :query, :getRawVertex
10 13
11 14 class << self
12 15 def wrappers

0 comments on commit d8c4ee6

Please sign in to comment.
Something went wrong with that request. Please try again.