Sha256: d876bcccfdf471c330868fb3a9edfce51c611f716fffc072de8168378ce80067

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Neo4j
  module Mapping

    # Enables creating and traversal of nodes.
    #
    # Includes the Enumerable Mixin.
    # The Neo4j::Mapping::ClassMethods::Relationship#has_n and Neo4j::Mapping::ClassMethods::Relationship#one
    # methods returns an object of this type.
    #
    # ==== See Also
    # Neo4j::Mapping::ClassMethods::Relationship
    #
    class HasN
      include Enumerable
      include ToJava

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

      def to_s
        "HasN [#@direction, id: #{@node.neo_id} type: #{@dsl.rel_type} dsl:#{@dsl}]"
      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

1 entries across 1 versions & 1 rubygems

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