class Neo4j::Rails::Relationships::RelsDSL

Instances of this class is returned from the rels, and generated accessor methods: has_n and has_one. Notice, this class is very similar to the Neo4j::Rails::Relationships::NodesDSL except that if creates, finds relationships instead of nodes.

Example

class Person < Neo4j::Rails::Model
   has_n(:friends)
end

person = Person.find(...)
person.friends_rels  #=> returns a Neo4j::Rails::Relationships::RelsDSL
rel = person.friends_rels.create(relationship properties)

Public Class Methods

new(storage, dir=:both) click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 72
def initialize(storage, dir=:both)
  @storage = storage
  @dir = dir
end

Public Instance Methods

[](index) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#[] except that it returns the n:th relationship instead of the n:th node

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 216
def [](index)
  i = 0
  each{|x| return x if i == index; i += 1}
  nil # out of index
end
all(*args) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#all except that it searches the relationships instead of the nodes.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 188
def all(*args)
  if args.first.class == Neo4j::Rails::Relationship #arg is a relationship
    find_all{|r| r == args.first}
  elsif ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0) #arg is an int
    find_all{|r| r.start_node.id.to_i == args.first.to_i || r.end_node.id.to_i == args.first.to_i}
  elsif node_in?(*args) #arg is a node
    find_all{|r| r.start_node == args.first || r.end_node == args.first}
  else #there either aren't any args, or we don't understand them
    collect
  end
end
build(attrs = {}) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#build except that you specify the properties of the relationships and it returns a relationship

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 80
def build(attrs = {})
  node = @storage.build(attrs)
  @storage.create_relationship_to(node, @dir)
end
connect(other_node, relationship_properties = nil) click to toggle source

Connects this node with an already existing other node with a new relationship. The relationship can optionally be given a hash of properties Does not save it. Returns the created relationship

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 105
def connect(other_node, relationship_properties = nil)
  rel = @storage.create_relationship_to(other_node, @dir)
  rel.attributes = relationship_properties if relationship_properties
  rel
end
create(attrs = {}) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#create except that you specify the properties of the relationships and it returns a relationship

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 87
def create(attrs = {})
  node = @storage.create(attrs)
  rel = @storage.create_relationship_to(node, @dir)
  node.save
  rel
end
create!(attrs) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#create! except that you specify the properties of the relationships and it returns a relationship

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 113
def create!(attrs)
  node = @storage.create(attrs)
  rel = @storage.create_relationship_to(node, @dir)
  node.save!
  rel
end
delete_all() click to toggle source

Delete all relationship.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 161
def delete_all
  each {|n| n.delete}
  @storage.clear_unpersisted
end
destroy_all() click to toggle source

Destroys all relationships object. Will not destroy the nodes.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 155
def destroy_all
  each {|n| n.destroy}
  @storage.clear_unpersisted
end
each(&block) click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 140
def each(&block)
  @storage.each_rel(@dir, &block)
end
empty?() click to toggle source

True if no relationship

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 150
def empty?
  size == 0
end
find(*args, &block) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#find except that it searches the relationships instead of the nodes.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 168
def find(*args, &block)
  return super(*args, &block) if block

  case args.first
    when :all, :first          
      kind = args.shift
      send(kind, *args)
    when "0", 0
      nil
    else
      if ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0)
        find_by_id(*args)
      else
        first(*args)
      end
    end                             
end
first(*args) click to toggle source

Same as Neo4j::Rails::Relationships::NodesDSL#first except that it searches the relationships instead of the nodes.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 202
def first(*args)
  if result = all(*args)
    if result.respond_to?(:collect) #if it's enumerable, get the first result
      result.first
    else 
      result
    end
  else
    nil
  end
end
incoming() click to toggle source

Returns incoming relationship See #outgoing

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 135
def incoming
  @dir = :incoming
  self
end
is_a?(type) click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 222
def is_a?(type)
  # ActionView requires this for nested attributes to work
  return true if Array == type
  super
end
outgoing() click to toggle source

Specifies that we want outgoing (undeclared) relationships.

Example

class Thing < Neo4j::Rails::Model
end

t = Thing.find(...)
t.rels(:reltype).outgoing  # returns an enumerable of all outgoing relationship of type :reltype
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 129
def outgoing
  @dir = :outgoing
  self
end
size() click to toggle source

Simply counts all relationships

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 145
def size
  @storage.size(@dir)
end
to_other(other) click to toggle source

Find all relationships between two nodes like this if the node is persisted. Returns an Enumerable. If the node is not persisted it will raise an error. Notice, only persisted relationships will be returned.

# File lib/neo4j/rails/relationships/rels_dsl.rb, line 97
def to_other(other)
  @storage.to_other(other)
end
to_s() click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 228
def to_s
  "Rels dir: #{@dir}, #{@storage}"
end

Protected Instance Methods

find_by_id(*args) click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 243
def find_by_id(*args)
  find{|r| r.id.to_i == args.first.to_i}       
end
node_in?(*args) click to toggle source
# File lib/neo4j/rails/relationships/rels_dsl.rb, line 234
def node_in?(*args)
  # does it contain an string, which will be treated like a condition ?
  if args.find { |a| a.class.superclass == Neo4j::Rails::Model }
    return true 
  else
    return false
  end
end