Module: Neo4j::ActiveNode::Persistence::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#create(props = {}) ⇒ Object

Creates and saves a new node

Parameters:

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

    the properties the new node should have



105
106
107
108
109
110
# File 'lib/neo4j/active_node/persistence.rb', line 105

def create(props = {})
  new(props).tap do |obj|
    yield obj if block_given?
    obj.save
  end
end

#create!(props = {}) ⇒ Object

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



113
114
115
116
117
118
# File 'lib/neo4j/active_node/persistence.rb', line 113

def create!(props = {})
  new(props).tap do |o|
    yield o if block_given?
    o.save!
  end
end

#find_or_create(find_attributes, set_attributes = {}) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/neo4j/active_node/persistence.rb', line 134

def find_or_create(find_attributes, set_attributes = {})
  on_create_attributes = set_attributes.reverse_merge(find_attributes.merge(self.new(find_attributes).props_for_create))

  neo4j_session.query.merge(n: {self.mapped_label_names => find_attributes})
    .on_create_set(n: on_create_attributes)
    .pluck(:n).first
end

#find_or_create_by(attributes, &block) ⇒ Object

Finds the first node with the given attributes, or calls create if none found



143
144
145
# File 'lib/neo4j/active_node/persistence.rb', line 143

def find_or_create_by(attributes, &block)
  find_by(attributes) || create(attributes, &block)
end

#find_or_create_by!(attributes, &block) ⇒ Object

Same as #find_or_create_by, but calls #create! so it raises an error if there is a problem during save.



148
149
150
# File 'lib/neo4j/active_node/persistence.rb', line 148

def find_or_create_by!(attributes, &block)
  find_by(attributes) || create!(attributes, &block)
end

#load_entity(id) ⇒ Object



152
153
154
# File 'lib/neo4j/active_node/persistence.rb', line 152

def load_entity(id)
  Neo4j::Node.load(id)
end

#merge(match_attributes, optional_attrs = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/neo4j/active_node/persistence.rb', line 120

def merge(match_attributes, optional_attrs = {})
  options = [:on_create, :on_match, :set]
  optional_attrs.assert_valid_keys(*options)

  optional_attrs.default = {}
  on_create_attrs, on_match_attrs, set_attrs = optional_attrs.values_at(*options)

  neo4j_session.query.merge(n: {self.mapped_label_names => match_attributes})
    .on_create_set(on_create_clause(on_create_attrs))
    .on_match_set(on_match_clause(on_match_attrs))
    .break.set(n: set_attrs)
    .pluck(:n).first
end