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
- #apply_default_values ⇒ Object
- #cache_key ⇒ Object
-
#concurrent_increment!(_attribute, _by = 1) ⇒ Object
Increments concurrently a numeric attribute by a centain amount.
- #create_or_update ⇒ Object
- #destroy ⇒ Object
-
#destroyed? ⇒ Boolean
Returns
true
if the object was destroyed. - #exist? ⇒ Boolean
- #freeze ⇒ Object
-
#frozen? ⇒ Boolean
True if the attributes hash has been frozen.
-
#increment(attribute, by = 1) ⇒ Object
Increments a numeric attribute by a centain amount.
-
#increment!(attribute, by = 1) ⇒ Object
Convenience method to increment numeric attribute and #save at the same time.
-
#new_record? ⇒ Boolean
(also: #new?)
Returns
true
if the record hasn’t been saved to Neo4j yet. -
#persisted? ⇒ Boolean
Returns
true
if the record is persisted, i.e. -
#props ⇒ Hash
All defined and none nil properties.
-
#props_for_create ⇒ Hash
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.
-
#props_for_persistence ⇒ Hash
Given a node’s state, will call the appropriate ‘props_for_action` method.
-
#props_for_update ⇒ Hash
Properties and values, type-converted and timestamped for the database.
- #reload ⇒ Object
- #reload_from_database ⇒ Object
- #skip_update? ⇒ Boolean
- #touch ⇒ Object
-
#update(attributes) ⇒ Object
(also: #update_attributes)
Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved.
-
#update!(attributes) ⇒ Object
(also: #update_attributes!)
Same as #update_attributes, but raises an exception if saving fails.
-
#update_attribute(attribute, value) ⇒ Object
Convenience method to set attribute and #save at the same time.
-
#update_attribute!(attribute, value) ⇒ Object
Convenience method to set attribute and #save! at the same time.
- #update_db_properties(hash) ⇒ Object (also: #update_columns)
- #update_db_property(field, value) ⇒ Object (also: #update_column)
- #update_model ⇒ Object
Instance Method Details
#apply_default_values ⇒ Object
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_key ⇒ Object
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
67 68 69 |
# File 'lib/neo4j/shared/persistence.rb', line 67 def concurrent_increment!(_attribute, _by = 1) fail 'not_implemented' end |
#create_or_update ⇒ Object
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 |
#destroy ⇒ Object
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.
145 146 147 |
# File 'lib/neo4j/shared/persistence.rb', line 145 def destroyed? @_deleted end |
#exist? ⇒ Boolean
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 |
#freeze ⇒ Object
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.
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
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
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.
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
117 118 119 |
# File 'lib/neo4j/shared/persistence.rb', line 117 def persisted? !new_record? && !destroyed? end |
#props ⇒ Hash
Returns 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_create ⇒ Hash
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.
31 32 33 34 35 36 37 |
# File 'lib/neo4j/shared/persistence.rb', line 31 def props_for_create 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_persistence ⇒ Hash
Returns 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_update ⇒ Hash
Returns 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 |
#reload ⇒ Object
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_database ⇒ Object
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
20 21 22 |
# File 'lib/neo4j/shared/persistence.rb', line 20 def skip_update? changed_attributes.blank? end |
#touch ⇒ Object
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
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
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_model ⇒ Object
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 |