Sha256: d437f280b48c7c7b1416648f1e4c02b23f1859106bd807652fddd99ca68cb9c0

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module Neo4j
  module Mapping

    # Enables creating and traversal of nodes.
    # Includes the Enumerable Mixin.
    #
    class HasN
      include Enumerable
      include ToJava

      def initialize(node, dsl) # :nodoc:
        @node = node
        @direction = dsl.direction
        @dsl = @direction == :outgoing ? dsl : dsl.incoming_dsl
      end

      def to_s
        "HasN [#@direction, #{@node.neo_id} #{@dsl.namespace_type}]"
      end

      def size
        [*self].size
      end

      alias_method :length, :size

      def [](index)
        each_with_index {|node,i| break node if index == i}
      end

      # Pretend we are an array - this is neccessarly for Rails actionpack/actionview/formhelper to work with this
      def is_a?(type)
        # ActionView requires this for nested attributes to work
        return true if Array == type
        super
      end

      # Required by the Enumerable mixin.
      def each(&block)
        @dsl.each_node(@node, @direction, &block)
      end


      # Returns true if there are no node in this type of relationship
      def empty?
        first != nil
      end


      # Creates a relationship instance between this and the other node.
      # Returns the relationship object
      def new(other)
        @dsl.create_relationship_to(@node, other)
      end


      # Creates a relationship between this and the other node.
      #
      # ==== Example
      # 
      #   n1 = Node.new # Node has declared having a friend type of relationship
      #   n2 = Node.new
      #   n3 = Node.new
      #
      #   n1 << n2 << n3
      #
      # ==== Returns
      # self
      #
      def <<(other)
        @dsl.create_relationship_to(@node, other)
        self
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
neo4j-1.0.0.beta.15 lib/neo4j/mapping/has_n.rb
neo4j-1.0.0.beta.14 lib/neo4j/mapping/has_n.rb