Module: ActiveGraph::Node::Persistence

Extended by:
ActiveSupport::Concern, Forwardable
Includes:
Shared::Persistence
Included in:
ActiveGraph::Node
Defined in:
lib/active_graph/node/persistence.rb

Defined Under Namespace

Modules: ClassMethods Classes: RecordInvalidError

Instance Method Summary collapse

Methods included from Shared::Persistence

#apply_default_values, #cache_key, #create_or_update, #destroy, #destroyed?, #exist?, #freeze, #frozen?, #increment, #increment!, #new_record?, #persisted?, #props, #props_for_create, #props_for_persistence, #props_for_update, #reload, #reload_from_database, #skip_update?, #touch, #update, #update!, #update_attribute, #update_attribute!, #update_db_properties, #update_db_property, #update_model

Instance Method Details

#_create_node(node_props, labels = labels_for_create) ⇒ ActiveGraph::Node

TODO: This does not seem like it should be the responsibility of the node. Creates an unwrapped node in the database.

Parameters:

  • node_props (Hash)

    The type-converted properties to be added to the new node.

  • labels (Array) (defaults to: labels_for_create)

    The labels to use for creating the new node.

Returns:



68
69
70
71
# File 'lib/active_graph/node/persistence.rb', line 68

def _create_node(node_props, labels = labels_for_create)
  query = "CREATE (n:`#{Array(labels).join('`:`')}`) SET n = $props RETURN n"
  neo4j_query(query, {props: node_props}, wrap: false).to_a[0][:n]
end

#concurrent_increment!(attribute, by = 1) ⇒ Object

Increments concurrently a numeric attribute by a centain amount

Parameters:

  • attribute (Symbol, String)

    name of the attribute to increment

  • by (Integer, Float) (defaults to: 1)

    amount to increment



35
36
37
# File 'lib/active_graph/node/persistence.rb', line 35

def concurrent_increment!(attribute, by = 1)
  increment_by_query! query_as(:n), attribute, by
end

#create_modelObject

Creates a model with values matching those of the instance attributes and returns its id.

Returns:

  • true



56
57
58
59
60
61
# File 'lib/active_graph/node/persistence.rb', line 56

def create_model
  node = _create_node(props_for_create)
  init_on_load(node, node.properties)
  @deferred_nodes = nil
  true
end

#inject_primary_key!(converted_props) ⇒ Object

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.

Parameters:

  • converted_props (Hash)

    A hash of properties post-typeconversion, ready for insertion into the DB.



78
79
80
81
82
# File 'lib/active_graph/node/persistence.rb', line 78

def inject_primary_key!(converted_props)
  self.class.default_property_values(self).tap do |destination_props|
    destination_props.merge!(converted_props) if converted_props.is_a?(Hash)
  end
end

#labels_for_createArray

Returns Labels to be set on the node during a create event.

Returns:

  • (Array)

    Labels to be set on the node during a create event



85
86
87
# File 'lib/active_graph/node/persistence.rb', line 85

def labels_for_create
  self.class.mapped_label_names
end

#saveObject

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.



25
26
27
28
29
30
# File 'lib/active_graph/node/persistence.rb', line 25

def save(*)
  cascade_save do
    association_proxy_cache.clear
    create_or_update
  end
end

#save!(*args) ⇒ Object

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.

Returns:

  • nil

Raises:

  • a RecordInvalidError if there is a problem during save.

See Also:

  • #save
  • ActiveGraph::Rails::Validations - for the :validate parameter
  • ActiveGraph::Rails::Callbacks - for callbacks


49
50
51
# File 'lib/active_graph/node/persistence.rb', line 49

def save!(*args)
  save(*args) or fail(RecordInvalidError, self) # rubocop:disable Style/AndOr
end