Module: ActiveCypher::Model::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_cypher/model/persistence.rb

Instance Method Summary collapse

Instance Method Details

#new_record?Boolean

Returns true if the record is new and untouched by the database.

If this ever returns true for a record you thought was persisted, consult your local sorcerer.

Returns:

  • (Boolean)

    true if the record is innocent.



75
# File 'lib/active_cypher/model/persistence.rb', line 75

def new_record? = @new_record

#persisted?Boolean

Returns true if the record has been saved and now bears a scar (internal_id).

If this ever returns false for a record you see in the database, that’s pure ORM sorcery.

Returns:

  • (Boolean)

    true if the record has a past.



81
# File 'lib/active_cypher/model/persistence.rb', line 81

def persisted? = !new_record? && internal_id.present?

#reloadself

Reloads the record from the database and overwrites your foolish edits.

Great for when you want to feel powerless again. Because sometimes you just want to see your changes disappear. If this ever resurrects data you thought was lost, that’s not a bug—it’s back magick.

Returns:

  • (self)

    the refreshed version of yourself, now with 30% more doubt.

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_cypher/model/persistence.rb', line 57

def reload
  raise ActiveCypher::RecordNotFound, 'Record not persisted' if new_record?

  fresh = self.class.find(internal_id)
  unless fresh
    raise ActiveCypher::RecordNotFound,
          "#{self.class} with internal_id=#{internal_id.inspect} not found"
  end

  self.attributes = fresh.attributes
  clear_changes_information
  self
end

#save(validate: true) ⇒ Boolean

Saves the current record to the database.

If it’s a new record, it’s born into this cruel digital world. If it already exists, we patch up its regrets. If it fails, we return false, like cowards.

Because nothing says “robust” like pretending persistence is easy. If this works and you can’t explain why, that’s probably back magick.

Parameters:

  • validate (Boolean) (defaults to: true)

    whether to run validations (default: true)

Returns:

  • (Boolean)

    true if saved successfully, false if the database ghosted us.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_cypher/model/persistence.rb', line 21

def save(validate: true)
  return false if validate && !valid?

  # before_/after_create
  _run(:save) do
    if new_record?
      _run(:create) { create_record }
    else
      _run(:update) { update_record }
    end
  end
rescue RecordNotSaved
  false
end

#update(attrs) ⇒ Boolean

Updates the record’s attributes and then saves it.

You know, like hope. But with hash keys. Because nothing says “optimism” like update-in-place. If this method ever fixes a bug you couldn’t reproduce, that’s just ORM witchcraft.

Parameters:

  • attrs (Hash)

    the attributes to assign.

Returns:

  • (Boolean)

    true if we pretended hard enough to update.



44
45
46
47
# File 'lib/active_cypher/model/persistence.rb', line 44

def update(attrs)
  assign_attributes(attrs)
  save
end