Module: Neo4j::Shared::Persistence

Extended by:
ActiveSupport::Concern
Included in:
ActiveNode::Persistence, ActiveRel::Persistence
Defined in:
lib/neo4j/shared/persistence.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#apply_default_valuesObject



104
105
106
107
108
109
# File 'lib/neo4j/shared/persistence.rb', line 104

def apply_default_values
  return if self.class.declared_property_defaults.empty?
  self.class.declared_property_defaults.each_pair do |key, value|
    self.send("#{key}=", value.respond_to?(:call) ? value.call : value) if self.send(key).nil?
  end
end

#cache_keyObject



220
221
222
223
224
225
226
227
228
# File 'lib/neo4j/shared/persistence.rb', line 220

def cache_key
  if self.new_record?
    "#{model_cache_key}/new"
  elsif self.respond_to?(:updated_at) && !self.updated_at.blank?
    "#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}"
  else
    "#{model_cache_key}/#{neo_id}"
  end
end

#concurrent_increment!(_attribute, _by = 1) ⇒ Object

Increments concurrently a numeric attribute by a centain amount

Parameters:

  • name (Symbol, String)

    of the attribute to increment

  • amount (Integer, Float)

    to increment



67
68
69
# File 'lib/neo4j/shared/persistence.rb', line 67

def concurrent_increment!(_attribute, _by = 1)
  fail 'not_implemented'
end

#create_or_updateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/neo4j/shared/persistence.rb', line 87

def create_or_update
  # since the same model can be created or updated twice from a relationship we have to have this guard
  @_create_or_updating = true
  apply_default_values
  result = _persisted_obj ? update_model : create_model
  current_transaction = Neo4j::ActiveBase.current_transaction

  current_transaction.mark_failed if result == false && current_transaction

  result != false
rescue => e
  current_transaction.mark_failed if current_transaction
  raise e
ensure
  @_create_or_updating = nil
end

#destroyObject



128
129
130
131
132
133
134
135
136
# File 'lib/neo4j/shared/persistence.rb', line 128

def destroy
  freeze

  destroy_query.exec if _persisted_obj

  @_deleted = true

  self
end

#destroyed?Boolean

Returns true if the object was destroyed.

Returns:



145
146
147
# File 'lib/neo4j/shared/persistence.rb', line 145

def destroyed?
  @_deleted
end

#exist?Boolean

Returns:



138
139
140
141
142
# File 'lib/neo4j/shared/persistence.rb', line 138

def exist?
  return if !_persisted_obj

  neo4j_query(query_as(:n).return('ID(n)')).any?
end

#freezeObject



159
160
161
162
# File 'lib/neo4j/shared/persistence.rb', line 159

def freeze
  @attributes.freeze
  self
end

#frozen?Boolean

Returns true if the attributes hash has been frozen.

Returns:

  • (Boolean)

    true if the attributes hash has been frozen



155
156
157
# File 'lib/neo4j/shared/persistence.rb', line 155

def frozen?
  @attributes.frozen?
end

#increment(attribute, by = 1) ⇒ Object

Increments a numeric attribute by a centain amount

Parameters:

  • name (Symbol, String)

    of the attribute to increment

  • amount (Integer, Float)

    to increment



51
52
53
54
55
# File 'lib/neo4j/shared/persistence.rb', line 51

def increment(attribute, by = 1)
  self[attribute] ||= 0
  self[attribute] += by
  self
end

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

Convenience method to increment numeric attribute and #save at the same time

Parameters:

  • name (Symbol, String)

    of the attribute to increment

  • amount (Integer, Float)

    to increment



60
61
62
# File 'lib/neo4j/shared/persistence.rb', line 60

def increment!(attribute, by = 1)
  increment(attribute, by).update_attribute(attribute, self[attribute])
end

#new_record?Boolean Also known as: new?

Returns true if the record hasn’t been saved to Neo4j yet.

Returns:



122
123
124
# File 'lib/neo4j/shared/persistence.rb', line 122

def new_record?
  !_persisted_obj
end

#persisted?Boolean

Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed

Returns:



117
118
119
# File 'lib/neo4j/shared/persistence.rb', line 117

def persisted?
  !new_record? && !destroyed?
end

#propsHash

Returns all defined and none nil properties.

Returns:

  • (Hash)

    all defined and none nil properties



150
151
152
# File 'lib/neo4j/shared/persistence.rb', line 150

def props
  attributes.reject { |_, v| v.nil? }.symbolize_keys
end

#props_for_createHash

Returns a hash containing:

  • All properties and values for insertion in the database

  • A ‘uuid` (or equivalent) key and value

  • Timestamps, if the class is set to include them.

Note that the UUID is added to the hash but is not set on the node. The timestamps, by comparison, are set on the node prior to addition in this hash.

Returns:

  • (Hash)


31
32
33
34
35
36
37
# File 'lib/neo4j/shared/persistence.rb', line 31

def props_for_create
  inject_timestamps!
  props_with_defaults = inject_defaults!(props)
  converted_props = props_for_db(props_with_defaults)
  return converted_props unless self.class.respond_to?(:default_property_values)
  inject_primary_key!(converted_props)
end

#props_for_persistenceHash

Returns Given a node’s state, will call the appropriate ‘props_for_action` method.

Returns:

  • (Hash)

    Given a node’s state, will call the appropriate ‘props_for_action` method.



8
9
10
# File 'lib/neo4j/shared/persistence.rb', line 8

def props_for_persistence
  _persisted_obj ? props_for_update : props_for_create
end

#props_for_updateHash

Returns Properties and values, type-converted and timestamped for the database.

Returns:

  • (Hash)

    Properties and values, type-converted and timestamped for the database.



40
41
42
43
44
45
46
# File 'lib/neo4j/shared/persistence.rb', line 40

def props_for_update
  update_magic_properties
  changed_props = attributes.select { |k, _| changed_attributes.include?(k) }
  changed_props.symbolize_keys!
  inject_defaults!(changed_props)
  props_for_db(changed_props)
end

#reloadObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/neo4j/shared/persistence.rb', line 164

def reload
  return self if new_record?
  association_proxy_cache.clear if respond_to?(:association_proxy_cache)
  changed_attributes_clear!
  unless reload_from_database
    @_deleted = true
    freeze
  end
  self
end

#reload_from_databaseObject



175
176
177
178
# File 'lib/neo4j/shared/persistence.rb', line 175

def reload_from_database
  reloaded = self.class.load_entity(neo_id)
  reloaded ? init_on_reload(reloaded._persisted_obj) : nil
end

#skip_update?Boolean

Returns:



20
21
22
# File 'lib/neo4j/shared/persistence.rb', line 20

def skip_update?
  changed_attributes.blank?
end

#touchObject



111
112
113
114
# File 'lib/neo4j/shared/persistence.rb', line 111

def touch
  fail 'Cannot touch on a new record object' unless persisted?
  update_attribute!(:updated_at, Time.now) if respond_to?(:updated_at=)
end

#update(attributes) ⇒ Object Also known as: update_attributes

Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved. If saving fails because the resource is invalid then false will be returned.



182
183
184
185
186
187
188
189
# File 'lib/neo4j/shared/persistence.rb', line 182

def update(attributes)
  self.class.run_transaction do |tx|
    self.attributes = process_attributes(attributes)
    saved = save
    tx.mark_failed unless saved
    saved
  end
end

#update!(attributes) ⇒ Object Also known as: update_attributes!

Same as #update_attributes, but raises an exception if saving fails.



212
213
214
215
216
217
# File 'lib/neo4j/shared/persistence.rb', line 212

def update!(attributes)
  self.class.run_transaction do
    self.attributes = process_attributes(attributes)
    save!
  end
end

#update_attribute(attribute, value) ⇒ Object

Convenience method to set attribute and #save at the same time

Parameters:

  • attribute (Symbol, String)

    of the attribute to update

  • value (Object)

    to set



74
75
76
77
# File 'lib/neo4j/shared/persistence.rb', line 74

def update_attribute(attribute, value)
  write_attribute(attribute, value)
  self.save
end

#update_attribute!(attribute, value) ⇒ Object

Convenience method to set attribute and #save! at the same time

Parameters:

  • attribute (Symbol, String)

    of the attribute to update

  • value (Object)

    to set



82
83
84
85
# File 'lib/neo4j/shared/persistence.rb', line 82

def update_attribute!(attribute, value)
  write_attribute(attribute, value)
  self.save!
end

#update_db_properties(hash) ⇒ Object Also known as: update_columns



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/neo4j/shared/persistence.rb', line 198

def update_db_properties(hash)
  fail ::Neo4j::Error, 'can not update on a new record object' unless persisted?
  self.class.run_transaction do
    db_values = props_for_db(hash)
    neo4j_query(query_as(:n).set(n: db_values))
    db_values.each_pair { |k, v| self.public_send(:"#{k}=", v) }
    _persisted_obj.props.merge!(db_values)
    changed_attributes_selective_clear!(db_values)
    true
  end
end

#update_db_property(field, value) ⇒ Object Also known as: update_column



192
193
194
195
# File 'lib/neo4j/shared/persistence.rb', line 192

def update_db_property(field, value)
  update_db_properties(field => value)
  true
end

#update_modelObject



12
13
14
15
16
17
18
# File 'lib/neo4j/shared/persistence.rb', line 12

def update_model
  return if skip_update?
  props = props_for_update
  neo4j_query(query_as(:n).set(n: props))
  _persisted_obj.props.merge!(props)
  changed_attributes_clear!
end