Module: ActiveGraph::Shared::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Node::Persistence, Relationship::Persistence
Defined in:
lib/active_graph/shared/persistence.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#apply_default_valuesObject

[View source]

100
101
102
103
104
105
# File 'lib/active_graph/shared/persistence.rb', line 100

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

[View source]

216
217
218
219
220
221
222
223
224
# File 'lib/active_graph/shared/persistence.rb', line 216

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:

  • _attribute (Symbol, String)

    name of the attribute to increment

  • _by (Integer, Float) (defaults to: 1)

    amount to increment

[View source]

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

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

#create_or_updateObject

[View source]

87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_graph/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

  ActiveGraph::Base.transaction(&:rollback) if result == false

  result != false
ensure
  @_create_or_updating = nil
end

#destroyObject

[View source]

124
125
126
127
128
129
130
131
132
# File 'lib/active_graph/shared/persistence.rb', line 124

def destroy
  freeze

  destroy_query.exec if _persisted_obj

  @_deleted = true

  self
end

#destroyed?Boolean

Returns true if the object was destroyed.

Returns:

[View source]

141
142
143
# File 'lib/active_graph/shared/persistence.rb', line 141

def destroyed?
  @_deleted
end

#exist?Boolean

Returns:

[View source]

134
135
136
137
138
# File 'lib/active_graph/shared/persistence.rb', line 134

def exist?
  return if !_persisted_obj

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

#freezeObject

[View source]

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

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

[View source]

151
152
153
# File 'lib/active_graph/shared/persistence.rb', line 151

def frozen?
  @attributes.frozen?
end

#increment(attribute, by = 1) ⇒ Object

Increments a numeric attribute by a centain amount

Parameters:

  • attribute (Symbol, String)

    name of the attribute to increment

  • by (Integer, Float) (defaults to: 1)

    amount to increment

[View source]

51
52
53
54
55
# File 'lib/active_graph/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:

  • attribute (Symbol, String)

    name of the attribute to increment

  • by (Integer, Float) (defaults to: 1)

    amount to increment

[View source]

60
61
62
# File 'lib/active_graph/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:

[View source]

118
119
120
# File 'lib/active_graph/shared/persistence.rb', line 118

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:

[View source]

113
114
115
# File 'lib/active_graph/shared/persistence.rb', line 113

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

#propsHash

Returns all defined and none nil properties.

Returns:

  • (Hash)

    all defined and none nil properties

[View source]

146
147
148
# File 'lib/active_graph/shared/persistence.rb', line 146

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)
[View source]

31
32
33
34
35
36
37
# File 'lib/active_graph/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.

[View source]

8
9
10
# File 'lib/active_graph/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.

[View source]

40
41
42
43
44
45
46
# File 'lib/active_graph/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

[View source]

160
161
162
163
164
165
166
167
168
169
# File 'lib/active_graph/shared/persistence.rb', line 160

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

[View source]

171
172
173
174
# File 'lib/active_graph/shared/persistence.rb', line 171

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

#skip_update?Boolean

Returns:

[View source]

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

def skip_update?
  changed_attributes.blank?
end

#touchObject

[View source]

107
108
109
110
# File 'lib/active_graph/shared/persistence.rb', line 107

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.

[View source]

178
179
180
181
182
183
184
185
# File 'lib/active_graph/shared/persistence.rb', line 178

def update(attributes)
  ActiveGraph::Base.transaction do |tx|
    self.attributes = process_attributes(attributes)
    saved = save
    tx.rollback unless saved
    saved
  end
end

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

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

[View source]

208
209
210
211
212
213
# File 'lib/active_graph/shared/persistence.rb', line 208

def update!(attributes)
  ActiveGraph::Base.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

[View source]

74
75
76
77
# File 'lib/active_graph/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

[View source]

82
83
84
85
# File 'lib/active_graph/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

[View source]

194
195
196
197
198
199
200
201
202
203
204
# File 'lib/active_graph/shared/persistence.rb', line 194

def update_db_properties(hash)
  fail ::ActiveGraph::Error, 'can not update on a new record object' unless persisted?
  ActiveGraph::Base.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.properties.merge!(db_values)
    changed_attributes_selective_clear!(db_values)
    true
  end
end

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

[View source]

188
189
190
191
# File 'lib/active_graph/shared/persistence.rb', line 188

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

#update_modelObject

[View source]

12
13
14
15
16
17
18
# File 'lib/active_graph/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.properties.merge!(props)
  changed_attributes_clear!
end