module Neo4j::ActiveNode::Persistence
Public Instance Methods
TODO: This does not seem like it should be the responsibility of the node. Creates an unwrapped node in the database. @param [Hash] node_props The type-converted properties to be added to the new node. @param [Array] labels The labels to use for creating the new node. @return [Neo4j::Node] A CypherNode or EmbeddedNode
# File lib/neo4j/active_node/persistence.rb 69 def _create_node(node_props, labels = labels_for_create) 70 self.class.neo4j_session.create_node(node_props, labels) 71 end
Increments concurrently a numeric attribute by a centain amount @param [Symbol, String] name of the attribute to increment @param [Integer, Float] amount to increment
# File lib/neo4j/active_node/persistence.rb 35 def concurrent_increment!(attribute, by = 1) 36 query_node = Neo4j::Session.query.match_nodes(n: neo_id) 37 increment_by_query! query_node, attribute, by 38 end
Creates a model with values matching those of the instance attributes and returns its id. @private @return true
# File lib/neo4j/active_node/persistence.rb 57 def create_model 58 node = _create_node(props_for_create) 59 init_on_load(node, node.props) 60 @deferred_nodes = nil 61 true 62 end
As the name suggests, this inserts the primary key (id property) into the properties hash. The method called here, ‘default_property_values`, is a holdover from an earlier version of the gem. It does NOT contain the default values of properties, it contains the Default Property
, which we now refer to as the ID Property
. It will be deprecated and renamed in a coming refactor. @param [Hash] converted_props A hash of properties post-typeconversion, ready for insertion into the DB.
# File lib/neo4j/active_node/persistence.rb 78 def inject_primary_key!(converted_props) 79 self.class.default_property_values(self).tap do |destination_props| 80 destination_props.merge!(converted_props) if converted_props.is_a?(Hash) 81 end 82 end
@return [Array] Labels
to be set on the node during a create event
# File lib/neo4j/active_node/persistence.rb 85 def labels_for_create 86 self.class.mapped_label_names 87 end
Saves the model.
If the model is new a record gets created in the database, otherwise the existing record gets updated. If perform_validation is true validations run. If any of them fail the action is cancelled and save returns false. If the flag is false validations are bypassed altogether. See ActiveRecord::Validations for more information. There’s a series of callbacks associated with save. If any of the before_* callbacks return false the action is cancelled and save returns false.
# File lib/neo4j/active_node/persistence.rb 25 def save(*) 26 cascade_save do 27 association_proxy_cache.clear 28 create_or_update 29 end 30 end
Persist the object to the database. Validations
and Callbacks
are included by default but validation can be disabled by passing :validate => false to save!
Creates a new transaction.
@raise a RecordInvalidError
if there is a problem during save. @param (see Neo4j::Rails::Validations#save) @return nil @see save
@see Neo4j::Rails::Validations Neo4j::Rails::Validations - for the :validate parameter @see Neo4j::Rails::Callbacks Neo4j::Rails::Callbacks - for callbacks
# File lib/neo4j/active_node/persistence.rb 50 def save!(*args) 51 save(*args) or fail(RecordInvalidError, self) # rubocop:disable Style/AndOr 52 end
Private Instance Methods
The pending associations are cleared during the save process, so it’s necessary to build the processable hash before it begins. If there are nodes and associations that need to be created after the node is saved, a new transaction is started.
# File lib/neo4j/active_node/persistence.rb 94 def cascade_save 95 Neo4j::Transaction.run(pending_deferred_creations?) do 96 result = yield 97 process_unpersisted_nodes! 98 result 99 end 100 end