module Neo4j::ActiveNode::Persistence::ClassMethods

Public Instance Methods

create(props = {}) { |obj| ... } click to toggle source

Creates and saves a new node @param [Hash] props the properties the new node should have

    # File lib/neo4j/active_node/persistence.rb
107 def create(props = {})
108   new(props).tap do |obj|
109     yield obj if block_given?
110     obj.save
111   end
112 end
create!(props = {}) { |o| ... } click to toggle source

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

    # File lib/neo4j/active_node/persistence.rb
115 def create!(props = {})
116   new(props).tap do |o|
117     yield o if block_given?
118     o.save!
119   end
120 end
find_or_create(find_attributes, set_attributes = {}) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
136 def find_or_create(find_attributes, set_attributes = {})
137   on_create_attributes = set_attributes.reverse_merge(find_attributes.merge(self.new(find_attributes).props_for_create))
138 
139   new_query.merge(n: {self.mapped_label_names => find_attributes})
140            .on_create_set(n: on_create_attributes)
141            .pluck(:n).first
142 end
find_or_create_by(attributes, &block) click to toggle source

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

    # File lib/neo4j/active_node/persistence.rb
145 def find_or_create_by(attributes, &block)
146   find_by(attributes) || create(attributes, &block)
147 end
find_or_create_by!(attributes, &block) click to toggle source

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

    # File lib/neo4j/active_node/persistence.rb
150 def find_or_create_by!(attributes, &block)
151   find_by(attributes) || create!(attributes, &block)
152 end
find_or_initialize_by(attributes) { |o| ... } click to toggle source
    # File lib/neo4j/active_node/persistence.rb
154 def find_or_initialize_by(attributes)
155   find_by(attributes) || new(attributes).tap { |o| yield(o) if block_given? }
156 end
load_entity(id) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
158 def load_entity(id)
159   query = query_base_for(id, :n).return(:n)
160   result = neo4j_query(query).first
161   result && result.n
162 end
merge(match_attributes, optional_attrs = {}) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
122 def merge(match_attributes, optional_attrs = {})
123   options = [:on_create, :on_match, :set]
124   optional_attrs.assert_valid_keys(*options)
125 
126   optional_attrs.default = {}
127   on_create_attrs, on_match_attrs, set_attrs = optional_attrs.values_at(*options)
128 
129   new_query.merge(n: {self.mapped_label_names => match_attributes})
130            .on_create_set(on_create_clause(on_create_attrs))
131            .on_match_set(on_match_clause(on_match_attrs))
132            .break.set(n: set_attrs)
133            .pluck(:n).first
134 end
query_base_for(neo_id, var = :n) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
164 def query_base_for(neo_id, var = :n)
165   Neo4j::ActiveBase.new_query.match(var).where(var => {neo_id: neo_id})
166 end

Private Instance Methods

on_create_clause(clause) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
170 def on_create_clause(clause)
171   if clause.is_a?(Hash)
172     {n: clause.merge(self.new(clause).props_for_create)}
173   else
174     clause
175   end
176 end
on_match_clause(clause) click to toggle source
    # File lib/neo4j/active_node/persistence.rb
178 def on_match_clause(clause)
179   if clause.is_a?(Hash)
180     {n: clause.merge(attributes_nil_hash.key?('updated_at') ? {updated_at: Time.new.to_i} : {})}
181   else
182     clause
183   end
184 end