Method: Neo4j::Wrapper::NodeMixin::ClassMethods#get_or_create

Defined in:
lib/neo4j-wrapper/node_mixin/class_methods.rb

#get_or_create(*args) ⇒ Object

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

Examples:

Creating a Unique node


class MyNode
  include Neo4j::NodeMixin
  property :email, :index => :exact, :unique => true
end

node = MyNode.get_or_create(:email =>'[email protected]', :name => 'jimmy')

See Also:

  • #put_if_absent


64
65
66
67
68
69
# File 'lib/neo4j-wrapper/node_mixin/class_methods.rb', line 64

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) { |*| new(*args) }.get_or_create(unique_factory_key, props[unique_factory_key])
end