Class: Neo4j::Rails::Relationships::NodesDSL

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/neo4j/rails/relationships/node_dsl.rb

Overview

Instances of this class is returned from the #outgoing, #incoming and generated accessor methods: has_n and has_one. Notice that this class includes the Ruby Enumerable mixin. If you want to full traversal api use the core version of these methods (some_node._outgoing(…)).

Constant Summary

CORE_TRAVERSAL_METHODS =

These methods are using the Neo4j::Core::Traversal::Traverser which means that only persisted relationship will be seen but more advanced traversal can be performed.

[:depth, :outgoing, :incoming, :both, :expand, :depth_first, :breadth_first, :eval_paths, :unique, :expander, :prune, :filter, :include_start_node, :rels, :eval_paths, :query]

Instance Method Summary (collapse)

Constructor Details

- (NodesDSL) initialize(storage, dir)

A new instance of NodesDSL



13
14
15
16
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 13

def initialize(storage, dir)
  @storage = storage
  @dir = dir
end

Instance Method Details

- (Object) <<(other)

Adds a new node to the relationship, no transaction is needed.

Examples:

create a relationship between two nodes

node.friends << other

using existing nodes

node.friends = ['42', '32']

Parameters:

Returns:

  • self



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 65

def <<(other)
  if other.is_a?(String)
    # this is typically called in an assignment operator, person.friends = ['42', '32']
    node = Neo4j::Node.load(other)
    @storage.create_relationship_to(node, @dir) unless all.include?(node)
  else
    # allow multiple relationships to the same node
    @storage.create_relationship_to(other, @dir)
  end
  self
end

- (Object) [](index)

Returns the n:th item in the relationship. This method simply traverse all relationship and returns the n:th one.



87
88
89
90
91
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 87

def [](index)
  i = 0
  each { |x| return x if i == index; i += 1 }
  nil # out of index
end

- (Object) all(*args)

Same as #find except that it returns an Enumerator of all nodes found.



143
144
145
146
147
148
149
150
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 143

def all(*args)
  unless args.empty?
    raise "Illegal argument, expected a node" unless args.first.kind_of?(Neo4j::NodeMixin)
    enum = Enumerator.new(@storage, :each_node, @dir).find { |n| n == args.first }
  else
    enum = Enumerator.new(@storage, :each_node, @dir)
  end
end

- (Boolean) blank?

Returns:

  • (Boolean)


197
198
199
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 197

def blank?
  false unless empty?
end

- (Neo4j::Rails::Model) build(attrs = {})

Creates a new node given the specified attributes and connect it with a relationship. The new node and relationship will not be saved. Both the relationship class and the node class can be specified with the has_n and has_one.

Examples:

class Person < Neo4j::Rails::Model
   has_n(:friends).to(Person).relationship(Friend)
   has_n(:knows)
end

Person.friends.build(:name => 'kalle')  # creates a Person and Friends class.
Person.knows.build(:name => 'kalle') # creates a Neo4j::Rails::Model and Neo4j::Rails::Relationship class

Parameters:

  • attrs (Hash) (defaults to: {})

    the attributes for the created node

Returns:



32
33
34
35
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 32

def build(attrs = {})
  self << (node = @storage.build(attrs))
  node
end

- (Object) count Also known as: length

Counts all relationships



177
178
179
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 177

def count
  @storage.count(@dir)
end

- (Neo4j::Rails::Model) create(attrs = {})

Same as #build except that the relationship and node are saved.

Parameters:

  • attrs (Hash) (defaults to: {})

    the attributes for the created node

Returns:



40
41
42
43
44
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 40

def create(attrs = {})
  self << (node = @storage.create(attrs))
  node.save
  node
end

- (Neo4j::Rails::Model) create!(attrs)

Same as #create but will raise an exception if an error (like validation) occurs.

Parameters:

  • attrs (Hash)

    the attributes for the created node

Returns:



49
50
51
52
53
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 49

def create!(attrs)
  self << (node = @storage.create(attrs))
  node.save!
  node
end

- (Object) delete(*nodes)

Delete relationships to the given nodes

Parameters:



189
190
191
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 189

def delete(*nodes)
  @storage.destroy_rels(@dir, *nodes)
end

- (Object) delete_all

Deletes all nodes and relationships, similar to #destroy_all



172
173
174
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 172

def delete_all
  each { |n| n.delete }
end

- (Object) destroy_all

Destroys all nodes (!) and relationship. Notice, if you only want to destroy the relationship use the #rels(:friends).destroy_all method instead.



167
168
169
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 167

def destroy_all
  each { |n| n.destroy }
end

- (Object) each



183
184
185
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 183

def each
  @storage.each_node(@dir) { |n| yield n } # Why passing the &block through doesn't work on JRuby 1.9?
end

- (Boolean) empty?

Returns:

  • (Boolean)


193
194
195
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 193

def empty?
  !@storage.relationships?(@dir)
end

- (Neo4j::Rails::Model) find(*args, &block)

Find one node in the relationship.

Examples:

Declaration of the relationship used in the examples below


class Actor < Neo4j::Rails::Model
  has_n(:acted_in)
end

find all child nodes

actor.acted_in.find(:all)

find first child node

actor.acted_in.find(:first)

find a child node by node

actor.acted_in.find(some_movie)

find a child node by id“ do

actor.acted_in.find(some_movie.id)

find a child node by delegate to Enumerable#find

actor.acted_in.find{|n| n.title == 'movie_1'}

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 123

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

- (Object) first(*args)

Returns first node in the relationship specified by the arguments or returns nil.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 153

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

- (Boolean) is_a?(type)

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 93

def is_a?(type)
  # ActionView requires this for nested attributes to work
  return true if Array == type
  super
end

- (Boolean) persisted?

Returns:

  • (Boolean)


77
78
79
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 77

def persisted?
  @storage.persisted?
end

- (Boolean) rel_changed?

Returns:

  • (Boolean)


205
206
207
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 205

def rel_changed?
  @storage.persisted?
end

- (Object) to_ary



81
82
83
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 81

def to_ary
  all.to_a
end

- (Object) to_s



201
202
203
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 201

def to_s
  "Node dir: #{@dir}, #{@storage}"
end