Module: Neo4j::Shared::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- ActiveNode::Persistence, ActiveRel::Persistence
- Defined in:
- lib/neo4j/shared/persistence.rb
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
- #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_model ⇒ Object
Methods included from ActiveSupport::Concern
Instance Method Details
#apply_default_values ⇒ Object
97 98 99 100 101 102 |
# File 'lib/neo4j/shared/persistence.rb', line 97 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) if self.send(key).nil? end end |
#cache_key ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/neo4j/shared/persistence.rb', line 182 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
59 60 61 |
# File 'lib/neo4j/shared/persistence.rb', line 59 def concurrent_increment!(_attribute, _by = 1) fail 'not_implemented' end |
#create_or_update ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/neo4j/shared/persistence.rb', line 79 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 if result == false Neo4j::Transaction.current.failure if Neo4j::Transaction.current false else true end rescue => e Neo4j::Transaction.current.failure if Neo4j::Transaction.current raise e ensure @_create_or_updating = nil end |
#destroy ⇒ Object
121 122 123 124 125 |
# File 'lib/neo4j/shared/persistence.rb', line 121 def destroy freeze _persisted_obj && _persisted_obj.del @_deleted = true end |
#destroyed? ⇒ Boolean
Returns true
if the object was destroyed.
132 133 134 |
# File 'lib/neo4j/shared/persistence.rb', line 132 def destroyed? @_deleted end |
#exist? ⇒ Boolean
127 128 129 |
# File 'lib/neo4j/shared/persistence.rb', line 127 def exist? _persisted_obj && _persisted_obj.exist? end |
#freeze ⇒ Object
146 147 148 149 |
# File 'lib/neo4j/shared/persistence.rb', line 146 def freeze @attributes.freeze self end |
#frozen? ⇒ Boolean
Returns true if the attributes hash has been frozen.
142 143 144 |
# File 'lib/neo4j/shared/persistence.rb', line 142 def frozen? @attributes.frozen? end |
#increment(attribute, by = 1) ⇒ Object
Increments a numeric attribute by a centain amount
43 44 45 46 47 |
# File 'lib/neo4j/shared/persistence.rb', line 43 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
52 53 54 |
# File 'lib/neo4j/shared/persistence.rb', line 52 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.
115 116 117 |
# File 'lib/neo4j/shared/persistence.rb', line 115 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
110 111 112 |
# File 'lib/neo4j/shared/persistence.rb', line 110 def persisted? !new_record? && !destroyed? end |
#props ⇒ Hash
Returns all defined and none nil properties.
137 138 139 |
# File 'lib/neo4j/shared/persistence.rb', line 137 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.
23 24 25 26 27 28 29 |
# File 'lib/neo4j/shared/persistence.rb', line 23 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.
6 7 8 |
# File 'lib/neo4j/shared/persistence.rb', line 6 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.
32 33 34 35 36 37 38 |
# File 'lib/neo4j/shared/persistence.rb', line 32 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
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/neo4j/shared/persistence.rb', line 151 def reload return self if new_record? association_proxy_cache.clear if respond_to?(:association_proxy_cache) changed_attributes && changed_attributes.clear unless reload_from_database @_deleted = true freeze end self end |
#reload_from_database ⇒ Object
162 163 164 165 |
# File 'lib/neo4j/shared/persistence.rb', line 162 def reload_from_database reloaded = self.class.load_entity(neo_id) reloaded ? init_on_reload(reloaded._persisted_obj) : nil end |
#touch ⇒ Object
104 105 106 107 |
# File 'lib/neo4j/shared/persistence.rb', line 104 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.
169 170 171 172 |
# File 'lib/neo4j/shared/persistence.rb', line 169 def update(attributes) self.attributes = process_attributes(attributes) save end |
#update!(attributes) ⇒ Object Also known as: update_attributes!
Same as #update_attributes, but raises an exception if saving fails.
176 177 178 179 |
# File 'lib/neo4j/shared/persistence.rb', line 176 def update!(attributes) self.attributes = process_attributes(attributes) save! end |
#update_attribute(attribute, value) ⇒ Object
Convenience method to set attribute and #save at the same time
66 67 68 69 |
# File 'lib/neo4j/shared/persistence.rb', line 66 def update_attribute(attribute, value) send("#{attribute}=", value) self.save 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) send("#{attribute}=", value) self.save! end |
#update_model ⇒ Object
10 11 12 13 14 |
# File 'lib/neo4j/shared/persistence.rb', line 10 def update_model return if !changed_attributes || changed_attributes.empty? _persisted_obj.update_props(props_for_update) changed_attributes.clear end |