class Neo4j::HasList::Mapping

Enables creating and traversal of nodes in a list.

It uses the TimeLine api.neo4j.org/current/org/neo4j/index/timeline/Timeline.html,

Includes the Enumerable Mixin. The Neo4j::Mapping::ClassMethods::List#has_list methods returns an object of this type.

Example, index same as size of list

class Person
   include Neo4j::NodeMixin
   has_list :feeds
end

person = Person.new
person.feeds << Neo4j::Node:new << Neo4j::Node.new

Example, using a custom index

class Person
   include Neo4j::NodeMixin
   has_list :feeds
end

person = Person.new
person.feeds[42] = (a = Neo4j::Node:new)
person.feeds[1251] = Neo4j::Node.new

person.feeds[42] # => a

Public Class Methods

new(indexer_for, node, name) click to toggle source
# File lib/neo4j/has_list/mapping.rb, line 40
def initialize(indexer_for, node, name)
  @index = Neo4j.started_db.lucene.for_nodes("#{indexer_for.to_s.gsub('::', '_')}_#{name}-timeline", Neo4j::Config[:lucene][:exact])
  @time_line = org.neo4j.index.lucene.LuceneTimeline.new(Neo4j.started_db.graph, @index)
  @node      = node
  @name      = name
  self.size = 0 unless size
end

Public Instance Methods

<<(other) click to toggle source
# File lib/neo4j/has_list/mapping.rb, line 138
def <<(other)
  @time_line.add(other._java_node, size)
  self.size = self.size + 1
  self
end
[](n) click to toggle source

returns the first node with index n

# File lib/neo4j/has_list/mapping.rb, line 60
def [](n)
  @time_line.getBetween(n, n).first
end
[]=(n, other_node) click to toggle source

adds a node to the list with the given index n

# File lib/neo4j/has_list/mapping.rb, line 80
def []=(n, other_node)
  @time_line.add(other_node, n)
  self.size = self.size + 1
end
all(n) click to toggle source

returns all nodes with the given index n

# File lib/neo4j/has_list/mapping.rb, line 65
def all(n)
  @time_line.getBetween(n, n)
end
between(range) click to toggle source

returns all the nodes between the given Range

# File lib/neo4j/has_list/mapping.rb, line 86
def between(range)
  @time_line.getBetween(range.first, range.end)
end
each() { |node| ... } click to toggle source

Required by the Enumerable mixin so that we can

Example

class Person
   include Neo4j::NodeMixin
   has_list :feeds
end

person.feeds.each {|node| node}
# File lib/neo4j/has_list/mapping.rb, line 107
def each
  @time_line.getBetween(java.lang.Long::MIN_VALUE,java.lang.Long::MAX_VALUE).iterator.each do |node|
    if @raw then
      yield node
    else
      yield node.wrapper
    end
  end
end
empty?() click to toggle source

same as #size == 0

# File lib/neo4j/has_list/mapping.rb, line 55
def empty?
  size == 0
end
first() click to toggle source

returns the first node in the list or nil

# File lib/neo4j/has_list/mapping.rb, line 70
def first
  @time_line.getFirst()
end
last() click to toggle source

returns the last node in the list or nil

# File lib/neo4j/has_list/mapping.rb, line 75
def last
  @time_line.getLast
end
raw() click to toggle source

If called then it will only return the raw java nodes and not the Ruby wrappers using the Neo4j::NodeMixin

# File lib/neo4j/has_list/mapping.rb, line 134
def raw
  @raw = true
end
remove(node) click to toggle source

removes one node from the list and decrases the size of the list,

# File lib/neo4j/has_list/mapping.rb, line 91
def remove(node)
  @index.remove(node, "timestamp")
  self.size = self.size - 1
end
size() click to toggle source

returns the size of this list notice in order to get correct result you must call the #remove method when an item is removed from the list

# File lib/neo4j/has_list/mapping.rb, line 50
def size
  @node["_list_size_#{@name}"]
end