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



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

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.



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

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



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

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))

  new_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



145
146
147
# File 'lib/neo4j/active_node/persistence.rb', line 145

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.



150
151
152
# File 'lib/neo4j/active_node/persistence.rb', line 150

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

#find_or_initialize_by(attributes) ⇒ Object



154
155
156
# File 'lib/neo4j/active_node/persistence.rb', line 154

def find_or_initialize_by(attributes)
  find_by(attributes) || new(attributes).tap { |o| yield(o) if block_given? }
end

#load_entity(id) ⇒ Object



158
159
160
161
162
# File 'lib/neo4j/active_node/persistence.rb', line 158

def load_entity(id)
  query = query_base_for(id, :n).return(:n)
  result = neo4j_query(query).first
  result && result.n
end

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



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

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)

  new_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

#query_base_for(neo_id, var = :n) ⇒ Object



164
165
166
# File 'lib/neo4j/active_node/persistence.rb', line 164

def query_base_for(neo_id, var = :n)
  Neo4j::ActiveBase.new_query.match(var).where(var => {neo_id: neo_id})
end