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

- (Fixnum) <=>(other)

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



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

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

- (Object) ==(other)

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.

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

- (Object) [](key)

Convenience method to retrieve a property by name.



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

- (Object) []=(key, value)

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



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

- (Route) e(*args)



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

- (Enumerator) each {|ElementMixin| ... }

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:



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

- (ElementMixin) element

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



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

def element
  self
end

- (Object) element_id

The id of the current element



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

def element_id
  element.get_id
end

- (Boolean) eql?(other)

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.

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

- (Set[extensions]) extensions

Which extensions does this element have?



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

def extensions
  Set[]
end

- (Boolean) from_graph?(g)

Query whether the current node belongs to the given graph.



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

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

- (GraphMixin) graph

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.



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

def graph
  @graph
end

- (Object) graph=(graph)

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

- (Hash) properties

Returns a hash of property values by name.



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

- (Object) properties=(props)

Replace the element's properties with the given hash



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

- (ElementMixin) result(name = nil)

Specialize result to return self for elements.



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

def result(name = nil)
  self
end

- (Route) v(*args)



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