Module: Pacer::Core::Graph::ElementRoute

Included in:
Wrappers::ElementWrapper
Defined in:
lib/pacer/core/graph/element_route.rb,
lib/pacer/transform/payload.rb

Overview

Basic methods for routes shared between all route types that emit routes: VerticesRoute, EdgesRoute and MixedRoute

Instance Method Summary collapse

Instance Method Details

#[](prop) ⇒ Core::Route #[](properties) ⇒ Core::Route #[](offset) ⇒ ElementRoute #[](range) ⇒ ElementRoute

Accepts a string or symbol to return an array of matching properties, or an integer to return the element at the given offset, or a range to return all elements between the offsets within the range.

Overloads:

  • #[](prop) ⇒ Core::Route

    Parameters:

    • prop (String, Symbol)

      map each element to a single property

    Returns:

  • #[](properties) ⇒ Core::Route

    Parameters:

    • properties (Array<String, Symbol>)

      map each element to a set of properties

    Returns:

  • #[](offset) ⇒ ElementRoute

    Returns with the same type and extensions as the source route.

    Parameters:

    • offset (Fixnum)

      narrow the result to a single element at the given position in the results

    Returns:

    • (ElementRoute)

      with the same type and extensions as the source route

  • #[](range) ⇒ ElementRoute

    Returns with the same type and extensions as the source route.

    Parameters:

    • range (Range)

      narrow the result to a subset of elements in positions corresponding to the range

    Returns:

    • (ElementRoute)

      with the same type and extensions as the source route



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pacer/core/graph/element_route.rb', line 79

def [](prop_or_subset)
  case prop_or_subset
  when String, Symbol
    typed_property(:object, prop_or_subset)
  when Fixnum
    range(prop_or_subset, prop_or_subset)
  when Range
    range(prop_or_subset.begin, prop_or_subset.end)
  when Array
    if prop_or_subset.all? { |i| i.is_a? String or i.is_a? Symbol }
      map(element_type: :array) do |element|
        element[prop_or_subset]
      end
    end
  end
end

#build_index(index, index_key = nil, property = nil, create = true) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pacer/core/graph/element_route.rb', line 134

def build_index(index, index_key = nil, property = nil, create = true)
  index_name = index
  unless index.is_a? com.tinkerpop.blueprints.Index
    index = graph.index index.to_s
  end
  sample_element = first
  unless index
    if sample_element
      if create
        index = graph.index index_name, graph.element_type(sample_element), create: true
      else
        raise "No index found for #{ index } on #{ graph }" unless index
      end
    else
      return nil
    end
  end
  index_key ||= index.name
  property ||= index_key
  if block_given?
    bulk_job do |element|
      value = yield(element)
      index.put(index_key, value, element.element) if value
    end
  else
    bulk_job do |element|
      value = element[property]
      index.put(index_key, value, element.element) if value
    end
  end
  index
end

#clone_into(target_graph, opts = {}) ⇒ Object



124
125
126
127
128
# File 'lib/pacer/core/graph/element_route.rb', line 124

def clone_into(target_graph, opts = {})
  bulk_job(nil, target_graph) do |element|
    element.clone_into(target_graph, opts)
  end
end

#copy_into(target_graph, opts = {}) ⇒ Object



130
131
132
# File 'lib/pacer/core/graph/element_route.rb', line 130

def copy_into(target_graph, opts = {})
  bulk_job(nil, target_graph) { |element| element.copy_into(target_graph, opts) }
end

#delete!Object

Delete all matching elements.



50
51
52
53
54
# File 'lib/pacer/core/graph/element_route.rb', line 50

def delete!
  count = 0
  uniq.bulk_job { |e| count += 1; e.delete! }
  count
end

#e(*filters, &block) ⇒ Object

Undefined for vertex routes.



25
26
27
# File 'lib/pacer/core/graph/element_route.rb', line 25

def e(*filters, &block)
  raise Pacer::UnsupportedOperation, "Can't get edges for this route type."
end

#element_idsCore::Route

Attach a route to the element id for each element emitted by the route.

Returns:



120
121
122
# File 'lib/pacer/core/graph/element_route.rb', line 120

def element_ids
  chain_route :element_type => :object, :pipe_class => Pacer::Pipes::IdPipe
end

#filter(*filters) {|ElementWrapper| ... } ⇒ ElementRoute

Attach a filter to the current route.

Parameters:

  • filter (Array<Hash, extension>, Hash, extension)

    see Route#property_filter

Yields:

  • (ElementWrapper)

    filter proc, see Route#property_filter

Returns:

  • (ElementRoute)

    the same type and extensions as the source route.



11
12
13
# File 'lib/pacer/core/graph/element_route.rb', line 11

def filter(*filters, &block)
  Pacer::Route.property_filter(self, filters, block, true)
end

#payload(&block) ⇒ Object



2
3
4
# File 'lib/pacer/transform/payload.rb', line 2

def payload(&block)
  chain_route transform: :payload, block: block
end

#propertiesCore::Route

Attach a transform that emits the properties of the elements rather than the elements themselves.

Returns:



32
33
34
# File 'lib/pacer/core/graph/element_route.rb', line 32

def properties
  map(element_type: :hash) { |v| v.properties }
end

#property?(name) ⇒ Core::Route

Map each element to a property but filter out elements that do not have the given property.

Parameters:

  • name (#to_s)

    the property name

Returns:



110
111
112
113
114
# File 'lib/pacer/core/graph/element_route.rb', line 110

def property?(name)
  chain_route(:element_type => :object,
              :pipe_class => Pacer::Pipes::PropertyPipe,
              :pipe_args => [name.to_s, false])
end

#raw_property_mapsObject



36
37
38
39
# File 'lib/pacer/core/graph/element_route.rb', line 36

def raw_property_maps
  chain_route(:element_type => :object,
              :pipe_class => Pacer::Pipes::PropertyMapPipe)
end

#result(name = nil) ⇒ ElementRoute

Stores the result of the current route in a new route so it will not need to be recalculated.

Returns:

  • (ElementRoute)

    with the same type and extensions as the source route



60
61
62
# File 'lib/pacer/core/graph/element_route.rb', line 60

def result(name = nil)
  element_ids.to_a.id_to_element_route(:based_on => self, :name => name)
end

#subgraph(graph = nil, opts = {}) ⇒ TinkerGraph

Create a new TinkerGraph based on the paths of all matching elements.

Returns:

  • (TinkerGraph)

    the subgraph



44
45
46
47
# File 'lib/pacer/core/graph/element_route.rb', line 44

def subgraph(graph = nil, opts = {})
  graph, opts = [nil, graph] if graph.is_a? Hash
  paths.subgraph graph, opts
end

#typed_property(element_type, name) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/pacer/core/graph/element_route.rb', line 96

def typed_property(element_type, name)
  route = chain_route(:element_type => :object,
                      :pipe_class => Pacer::Pipes::PropertyPipe,
                      :pipe_args => [name.to_s],
                      :lookahead_replacement => proc { |r| r.back.property?(name) })
  route.map(route_name: 'decode', remove_from_lookahead: true, element_type: element_type) do |v|
    graph.decode_property(v)
  end
end

#v(*filters) ⇒ Object

v is undefined for edge routes.



18
19
20
# File 'lib/pacer/core/graph/element_route.rb', line 18

def v(*filters)
  raise Pacer::UnsupportedOperation, "Can't get vertices for this route type."
end