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 105 def create(props = {}) 106 new(props).tap do |obj| 107 yield obj if block_given? 108 obj.save 109 end 110 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 113 def create!(props = {}) 114 new(props).tap do |o| 115 yield o if block_given? 116 o.save! 117 end 118 end
find_or_create(find_attributes, set_attributes = {})
click to toggle source
# File lib/neo4j/active_node/persistence.rb 134 def find_or_create(find_attributes, set_attributes = {}) 135 on_create_attributes = set_attributes.reverse_merge(find_attributes.merge(self.new(find_attributes).props_for_create)) 136 137 neo4j_session.query.merge(n: {self.mapped_label_names => find_attributes}) 138 .on_create_set(n: on_create_attributes) 139 .pluck(:n).first 140 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 143 def find_or_create_by(attributes, &block) 144 find_by(attributes) || create(attributes, &block) 145 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 148 def find_or_create_by!(attributes, &block) 149 find_by(attributes) || create!(attributes, &block) 150 end
load_entity(id)
click to toggle source
# File lib/neo4j/active_node/persistence.rb 152 def load_entity(id) 153 Neo4j::Node.load(id) 154 end
merge(match_attributes, optional_attrs = {})
click to toggle source
# File lib/neo4j/active_node/persistence.rb 120 def merge(match_attributes, optional_attrs = {}) 121 options = [:on_create, :on_match, :set] 122 optional_attrs.assert_valid_keys(*options) 123 124 optional_attrs.default = {} 125 on_create_attrs, on_match_attrs, set_attrs = optional_attrs.values_at(*options) 126 127 neo4j_session.query.merge(n: {self.mapped_label_names => match_attributes}) 128 .on_create_set(on_create_clause(on_create_attrs)) 129 .on_match_set(on_match_clause(on_match_attrs)) 130 .break.set(n: set_attrs) 131 .pluck(:n).first 132 end
Private Instance Methods
on_create_clause(clause)
click to toggle source
# File lib/neo4j/active_node/persistence.rb 158 def on_create_clause(clause) 159 if clause.is_a?(Hash) 160 {n: clause.merge(self.new(clause).props_for_create)} 161 else 162 clause 163 end 164 end
on_match_clause(clause)
click to toggle source
# File lib/neo4j/active_node/persistence.rb 166 def on_match_clause(clause) 167 if clause.is_a?(Hash) 168 {n: clause.merge(attributes_nil_hash.key?('updated_at') ? {updated_at: Time.new.to_i} : {})} 169 else 170 clause 171 end 172 end