module ActiveGraph::Node::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/active_graph/node/persistence.rb 106 def create(props = {}) 107 new(props).tap do |obj| 108 yield obj if block_given? 109 obj.save 110 end 111 end
create!(props = {}) { |o| ... }
click to toggle source
Same as create
, but raises an error if there is a problem during save.
# File lib/active_graph/node/persistence.rb 114 def create!(props = {}) 115 new(props).tap do |o| 116 yield o if block_given? 117 o.save! 118 end 119 end
find_or_create(find_attributes, set_attributes = {})
click to toggle source
# File lib/active_graph/node/persistence.rb 135 def find_or_create(find_attributes, set_attributes = {}) 136 on_create_attributes = set_attributes.reverse_merge(find_attributes.merge(self.new(find_attributes).props_for_create)) 137 138 new_query.merge(n: {self.mapped_label_names => find_attributes}) 139 .on_create_set(n: on_create_attributes) 140 .pluck(:n).first 141 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/active_graph/node/persistence.rb 144 def find_or_create_by(attributes, &block) 145 find_by(attributes) || create(attributes, &block) 146 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/active_graph/node/persistence.rb 149 def find_or_create_by!(attributes, &block) 150 find_by(attributes) || create!(attributes, &block) 151 end
find_or_initialize_by(attributes) { |o| ... }
click to toggle source
# File lib/active_graph/node/persistence.rb 153 def find_or_initialize_by(attributes) 154 find_by(attributes) || new(attributes).tap { |o| yield(o) if block_given? } 155 end
load_entity(id)
click to toggle source
# File lib/active_graph/node/persistence.rb 157 def load_entity(id) 158 query = query_base_for(id, :n).return(:n) 159 result = neo4j_query(query).first 160 result && result[:n] 161 end
merge(match_attributes, optional_attrs = {})
click to toggle source
# File lib/active_graph/node/persistence.rb 121 def merge(match_attributes, optional_attrs = {}) 122 options = [:on_create, :on_match, :set] 123 optional_attrs.assert_valid_keys(*options) 124 125 optional_attrs.default = {} 126 on_create_attrs, on_match_attrs, set_attrs = optional_attrs.values_at(*options) 127 128 new_query.merge(n: {self.mapped_label_names => match_attributes}) 129 .on_create_set(on_create_clause(on_create_attrs)) 130 .on_match_set(on_match_clause(on_match_attrs)) 131 .break.set(n: set_attrs) 132 .pluck(:n).first 133 end
query_base_for(neo_id, var = :n)
click to toggle source
# File lib/active_graph/node/persistence.rb 163 def query_base_for(neo_id, var = :n) 164 ActiveGraph::Base.new_query.match(var).where(var => {neo_id: neo_id}) 165 end
Private Instance Methods
on_create_clause(clause)
click to toggle source
# File lib/active_graph/node/persistence.rb 169 def on_create_clause(clause) 170 if clause.is_a?(Hash) 171 {n: clause.merge(self.new(clause).props_for_create)} 172 else 173 clause 174 end 175 end
on_match_clause(clause)
click to toggle source
# File lib/active_graph/node/persistence.rb 177 def on_match_clause(clause) 178 if clause.is_a?(Hash) 179 {n: clause.merge(attributes_nil_hash.key?('updated_at') ? {updated_at: Time.new.to_i} : {})} 180 else 181 clause 182 end 183 end