Module: Pacer::ElementMixin

Included in:
DexEdge, DexVertex, EdgeWrapper, Neo4jEdge, Neo4jVertex, TinkerEdge, TinkerVertex, VertexWrapper
Defined in:
lib/pacer/graph/element_mixin.rb

Overview

This module is mixed into the raw Blueprints Edge and Vertex classes from any graph implementation.

Adds more convenient/rubyish methods and adds support for extensions to some methods where needed.

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Fixnum

Sort objects semi arbitrarily based on VertexMixin#display_name or Pacer::EdgeMixin#display_name.

Parameters:

  • other

Returns:

  • (Fixnum)


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

def <=>(other)
  display_name.to_s <=> other.display_name.to_s
end

#==(other) ⇒ Object

Test equality to another object.

Elements are equal if they are the same type and have the same id and the same graph, regardless of extensions.

If the graphdb instantiates multiple copies of the same element this method will return true when comparing them.

Parameters:

  • other

See Also:



144
145
146
147
148
149
150
151
152
# File 'lib/pacer/graph/element_mixin.rb', line 144

def ==(other)
  if other.respond_to?(:element) and other.element.class == element.class and other.element_id == element_id
    if graph and other.graph
      other.graph == graph
    else
      true
    end
  end
end

#[](key) ⇒ Object

Convenience method to retrieve a property by name.

Parameters:

  • key (#to_s)

    the property name

Returns:

  • (Object)


63
64
65
66
67
68
69
70
# File 'lib/pacer/graph/element_mixin.rb', line 63

def [](key)
  value = get_property(key.to_s)
  if graph
    graph.decode_property(value)
  else
    value
  end
end

#[]=(key, value) ⇒ Object

Convenience method to set a property by name to the given value.

Parameters:

  • key (#to_s)

    the property name

  • value (Object)

    the value to set the property to



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pacer/graph/element_mixin.rb', line 75

def []=(key, value)
  value = graph.encode_property(value) if graph
  key = key.to_s
  if value
    if value != get_property(key)
      set_property(key, value)
    end
  else
    remove_property(key) if property_keys.include? key
  end
end

#e(*args) ⇒ Route

Returns:



33
34
35
36
37
38
39
# File 'lib/pacer/graph/element_mixin.rb', line 33

def e(*args)
  route = super
  if args.empty? and not block_given?
    route.add_extensions extensions
  end
  route
end

#each {|ElementMixin| ... } ⇒ Enumerator

Yields the element once or returns an enumerator if no block is given. Follows Ruby conventions and is meant to be used along with the Enumerable mixin.

Yields:

Returns:



178
179
180
181
182
183
184
# File 'lib/pacer/graph/element_mixin.rb', line 178

def each
  if block_given?
    yield self
  else
    [self].to_enum
  end
end

#elementElementMixin

Returns the underlying element. For unwrapped elements, returns self.

Returns:



189
190
191
# File 'lib/pacer/graph/element_mixin.rb', line 189

def element
  self
end

#element_idObject

The id of the current element

Returns:

  • (Object)

    element id (type varies by graph implementation.



121
122
123
# File 'lib/pacer/graph/element_mixin.rb', line 121

def element_id
  element.get_id
end

#eql?(other) ⇒ Boolean

Test object equality of the element instance.

Wrappers/extensions (if any) are ignored, the underlying element only is compared

If the graphdb instantiates multiple copies of the same element this method will return false when comparing them.

Parameters:

  • other

Returns:

  • (Boolean)

See Also:



164
165
166
167
168
169
170
# File 'lib/pacer/graph/element_mixin.rb', line 164

def eql?(other)
  if other.respond_to? :element
    super(other.element)
  else
    super
  end
end

#extensionsSet[extensions]

Which extensions does this element have?

Returns:

  • (Set[extensions])


17
18
19
# File 'lib/pacer/graph/element_mixin.rb', line 17

def extensions
  Set[]
end

#from_graph?(g) ⇒ Boolean

Query whether the current node belongs to the given graph.

Parameters:

  • g (Object)

    the object to compare to #graph

Returns:

  • (Boolean)


96
97
98
# File 'lib/pacer/graph/element_mixin.rb', line 96

def from_graph?(g)
  g.equal? graph
end

#graphGraphMixin

The graph the element belongs to.

Used to help prevent objects from different graphs from being accidentally associated, as well as to get graph-specific data for the element.

Returns:



55
56
57
# File 'lib/pacer/graph/element_mixin.rb', line 55

def graph
  @graph
end

#graph=(graph) ⇒ Object

For internal use only.

Specify the graph the element belongs to.



44
45
46
# File 'lib/pacer/graph/element_mixin.rb', line 44

def graph=(graph)
  @graph = graph
end

#propertiesHash

Returns a hash of property values by name.

Returns:



103
104
105
# File 'lib/pacer/graph/element_mixin.rb', line 103

def properties
  property_keys.inject({}) { |h, name| h[name] = get_property(name); h }
end

#properties=(props) ⇒ Object

Replace the element's properties with the given hash

Parameters:

  • props (Hash)

    the element's new properties



110
111
112
113
114
115
116
117
# File 'lib/pacer/graph/element_mixin.rb', line 110

def properties=(props)
  (property_keys - props.keys.collect { |k| k.to_s }).each do |key|
    remove_property key
  end
  props.each do |key, value|
    self[key] = value
  end
end

#result(name = nil) ⇒ ElementMixin

Specialize result to return self for elements.

Returns:



89
90
91
# File 'lib/pacer/graph/element_mixin.rb', line 89

def result(name = nil)
  self
end

#v(*args) ⇒ Route

Returns:



23
24
25
26
27
28
29
# File 'lib/pacer/graph/element_mixin.rb', line 23

def v(*args)
  route = super
  if args.empty? and not block_given?
    route.add_extensions extensions
  end
  route
end