module Neo4j::Shared::Persistence

Public Instance Methods

apply_default_values() click to toggle source
    # File lib/neo4j/shared/persistence.rb
 97 def apply_default_values
 98   return if self.class.declared_property_defaults.empty?
 99   self.class.declared_property_defaults.each_pair do |key, value|
100     self.send("#{key}=", value) if self.send(key).nil?
101   end
102 end
cache_key() click to toggle source
    # File lib/neo4j/shared/persistence.rb
182 def cache_key
183   if self.new_record?
184     "#{model_cache_key}/new"
185   elsif self.respond_to?(:updated_at) && !self.updated_at.blank?
186     "#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}"
187   else
188     "#{model_cache_key}/#{neo_id}"
189   end
190 end
concurrent_increment!(_attribute, _by = 1) click to toggle source

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/shared/persistence.rb
59 def concurrent_increment!(_attribute, _by = 1)
60   fail 'not_implemented'
61 end
create_or_update() click to toggle source
   # File lib/neo4j/shared/persistence.rb
79 def create_or_update
80   # since the same model can be created or updated twice from a relationship we have to have this guard
81   @_create_or_updating = true
82   apply_default_values
83   result = _persisted_obj ? update_model : create_model
84   if result == false
85     Neo4j::Transaction.current.failure if Neo4j::Transaction.current
86     false
87   else
88     true
89   end
90 rescue => e
91   Neo4j::Transaction.current.failure if Neo4j::Transaction.current
92   raise e
93 ensure
94   @_create_or_updating = nil
95 end
destroy() click to toggle source
    # File lib/neo4j/shared/persistence.rb
121 def destroy
122   freeze
123   _persisted_obj && _persisted_obj.del
124   @_deleted = true
125 end
destroyed?() click to toggle source

Returns true if the object was destroyed.

    # File lib/neo4j/shared/persistence.rb
132 def destroyed?
133   @_deleted
134 end
exist?() click to toggle source
    # File lib/neo4j/shared/persistence.rb
127 def exist?
128   _persisted_obj && _persisted_obj.exist?
129 end
freeze() click to toggle source
    # File lib/neo4j/shared/persistence.rb
146 def freeze
147   @attributes.freeze
148   self
149 end
frozen?() click to toggle source

@return true if the attributes hash has been frozen

    # File lib/neo4j/shared/persistence.rb
142 def frozen?
143   @attributes.frozen?
144 end
increment(attribute, by = 1) click to toggle source

Increments 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/shared/persistence.rb
43 def increment(attribute, by = 1)
44   self[attribute] ||= 0
45   self[attribute] += by
46   self
47 end
increment!(attribute, by = 1) click to toggle source

Convenience method to increment numeric attribute and save at the same time @param [Symbol, String] name of the attribute to increment @param [Integer, Float] amount to increment

   # File lib/neo4j/shared/persistence.rb
52 def increment!(attribute, by = 1)
53   increment(attribute, by).update_attribute(attribute, self[attribute])
54 end
new?()
Alias for: new_record?
new_record?() click to toggle source

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

    # File lib/neo4j/shared/persistence.rb
115 def new_record?
116   !_persisted_obj
117 end
Also aliased as: new?
persisted?() click to toggle source

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

    # File lib/neo4j/shared/persistence.rb
110 def persisted?
111   !new_record? && !destroyed?
112 end
props() click to toggle source

@return [Hash] all defined and none nil properties

    # File lib/neo4j/shared/persistence.rb
137 def props
138   attributes.reject { |_, v| v.nil? }.symbolize_keys
139 end
props_for_create() click to toggle source

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. @return [Hash]

   # File lib/neo4j/shared/persistence.rb
23 def props_for_create
24   inject_timestamps!
25   props_with_defaults = inject_defaults!(props)
26   converted_props = props_for_db(props_with_defaults)
27   return converted_props unless self.class.respond_to?(:default_property_values)
28   inject_primary_key!(converted_props)
29 end
props_for_persistence() click to toggle source

@return [Hash] Given a node’s state, will call the appropriate ‘props_for_{action}` method.

  # File lib/neo4j/shared/persistence.rb
6 def props_for_persistence
7   _persisted_obj ? props_for_update : props_for_create
8 end
props_for_update() click to toggle source

@return [Hash] Properties and values, type-converted and timestamped for the database.

   # File lib/neo4j/shared/persistence.rb
32 def props_for_update
33   update_magic_properties
34   changed_props = attributes.select { |k, _| changed_attributes.include?(k) }
35   changed_props.symbolize_keys!
36   inject_defaults!(changed_props)
37   props_for_db(changed_props)
38 end
reload() click to toggle source
    # File lib/neo4j/shared/persistence.rb
151 def reload
152   return self if new_record?
153   association_proxy_cache.clear if respond_to?(:association_proxy_cache)
154   changed_attributes && changed_attributes.clear
155   unless reload_from_database
156     @_deleted = true
157     freeze
158   end
159   self
160 end
reload_from_database() click to toggle source
    # File lib/neo4j/shared/persistence.rb
162 def reload_from_database
163   reloaded = self.class.load_entity(neo_id)
164   reloaded ? init_on_reload(reloaded._persisted_obj) : nil
165 end
touch() click to toggle source
    # File lib/neo4j/shared/persistence.rb
104 def touch
105   fail 'Cannot touch on a new record object' unless persisted?
106   update_attribute!(:updated_at, Time.now) if respond_to?(:updated_at=)
107 end
update(attributes) click to toggle source

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.

    # File lib/neo4j/shared/persistence.rb
169 def update(attributes)
170   self.attributes = process_attributes(attributes)
171   save
172 end
Also aliased as: update_attributes
update!(attributes) click to toggle source

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

    # File lib/neo4j/shared/persistence.rb
176 def update!(attributes)
177   self.attributes = process_attributes(attributes)
178   save!
179 end
Also aliased as: update_attributes!
update_attribute(attribute, value) click to toggle source

Convenience method to set attribute and save at the same time @param [Symbol, String] attribute of the attribute to update @param [Object] value to set

   # File lib/neo4j/shared/persistence.rb
66 def update_attribute(attribute, value)
67   send("#{attribute}=", value)
68   self.save
69 end
update_attribute!(attribute, value) click to toggle source

Convenience method to set attribute and save! at the same time @param [Symbol, String] attribute of the attribute to update @param [Object] value to set

   # File lib/neo4j/shared/persistence.rb
74 def update_attribute!(attribute, value)
75   send("#{attribute}=", value)
76   self.save!
77 end
update_attributes(attributes)
Alias for: update
update_attributes!(attributes)
Alias for: update!
update_model() click to toggle source
   # File lib/neo4j/shared/persistence.rb
10 def update_model
11   return if !changed_attributes || changed_attributes.empty?
12   _persisted_obj.update_props(props_for_update)
13   changed_attributes.clear
14 end

Protected Instance Methods

increment_by_query!(match_query, attribute, by, element_name = :n) click to toggle source
    # File lib/neo4j/shared/persistence.rb
194 def increment_by_query!(match_query, attribute, by, element_name = :n)
195   new_attribute = match_query.with(element_name)
196                   .set("#{element_name}.`#{attribute}` = COALESCE(#{element_name}.`#{attribute}`, 0) + {by}")
197                   .params(by: by).limit(1)
198                   .pluck("#{element_name}.`#{attribute}`").first
199   return false unless new_attribute
200   self[attribute] = new_attribute
201   changed_attributes.delete(attribute)
202   true
203 end

Private Instance Methods

inject_timestamps!() click to toggle source
    # File lib/neo4j/shared/persistence.rb
219 def inject_timestamps!
220   now = DateTime.now
221   self.created_at ||= now if respond_to?(:created_at=)
222   self.updated_at ||= now if respond_to?(:updated_at=)
223 end
model_cache_key() click to toggle source
    # File lib/neo4j/shared/persistence.rb
211 def model_cache_key
212   self.class.model_name.cache_key
213 end
props_for_db(props_hash) click to toggle source
    # File lib/neo4j/shared/persistence.rb
207 def props_for_db(props_hash)
208   self.class.declared_properties.convert_properties_to(self, :db, props_hash)
209 end
set_timestamps() click to toggle source
    # File lib/neo4j/shared/persistence.rb
225 def set_timestamps
226   warning = 'This method has been replaced with `inject_timestamps!` and will be removed in a future version'.freeze
227   ActiveSupport::Deprecation.warn warning, caller
228   inject_timestamps!
229 end
update_magic_properties() click to toggle source
    # File lib/neo4j/shared/persistence.rb
215 def update_magic_properties
216   self.updated_at = DateTime.now if respond_to?(:updated_at=) && (updated_at.nil? || (changed? && !updated_at_changed?))
217 end