Module: Neo4j::Rails::Persistence::ClassMethods

Defined in:
lib/neo4j/rails/persistence.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) create(*args)

Initialize a model and set a bunch of attributes at the same time. Returns the object whether saved successfully or not.



98
99
100
101
102
103
# File 'lib/neo4j/rails/persistence.rb', line 98

def create(*args)
  new(*args).tap do |o|
    yield o if block_given?
    o.save
  end
end

- (Neo4j::Rails::Model, Neo4j::Rails::Relationship) create!(*args)

Same as #create, but raises an error if there is a problem during save.



125
126
127
128
129
130
# File 'lib/neo4j/rails/persistence.rb', line 125

def create!(*args)
  new(*args).tap do |o|
    yield o if block_given?
    o.save!
  end
end

- (Object) destroy_all

Destroy each node in turn. Runs the destroy callbacks for each node.



133
134
135
136
137
# File 'lib/neo4j/rails/persistence.rb', line 133

def destroy_all
  all.each do |n|
    n.destroy
  end
end

- (Object) get_or_create(*args)

Get the indexed entity, creating it (exactly once) if no indexed entity exist.

Examples:

Creating a Unique node


class MyNode < Neo4j::Rails::Model
  property :email, :index => :exact, :unique => true
end

node = MyNode.get_or_create(:email =>'jimmy@gmail.com', :name => 'jimmy')

See Also:

  • #put_if_absent


116
117
118
119
120
121
# File 'lib/neo4j/rails/persistence.rb', line 116

def get_or_create(*args)
  props = args.first
  raise "Can't get or create entity since #{props.inspect} does not included unique key #{props[unique_factory_key]}'" unless props[unique_factory_key]
  index = index_for_type(_decl_props[unique_factory_key][:index])
  Neo4j::Core::Index::UniqueFactory.new(unique_factory_key, index) { |*| create!(*args) }.get_or_create(unique_factory_key, props[unique_factory_key]).wrapper
end

- (Object) new(*args, &block)



90
91
92
93
94
# File 'lib/neo4j/rails/persistence.rb', line 90

def new(*args, &block)
  instance = orig_new(*args, &block)
  instance.instance_eval(&block) if block
  instance
end

- (Object) transaction(&block)



84
85
86
87
88
# File 'lib/neo4j/rails/persistence.rb', line 84

def transaction(&block)
  Neo4j::Rails::Transaction.run do |tx|
    block.call(tx)
  end
end