Module: Neo4j::ActiveNode::HasN

Extended by:
ActiveSupport::Concern
Included in:
Neo4j::ActiveNode
Defined in:
lib/neo4j/active_node/has_n.rb,
lib/neo4j/active_node/has_n/association.rb,
lib/neo4j/active_node/has_n/association/rel_factory.rb,
lib/neo4j/active_node/has_n/association_cypher_methods.rb

Defined Under Namespace

Modules: AssociationCypherMethods, ClassMethods Classes: Association, AssociationProxy, HasOneConstraintError, NonPersistedNodeError

Instance Method Summary collapse

Instance Method Details

#active_rel_corresponding_rel(active_rel, direction, target_class) ⇒ Object



255
256
257
258
259
260
# File 'lib/neo4j/active_node/has_n.rb', line 255

def active_rel_corresponding_rel(active_rel, direction, target_class)
  self.class.associations.find do |_key, assoc|
    assoc.relationship_class_name == active_rel.class.name ||
      (assoc.relationship_type == active_rel.type.to_sym && assoc.target_class == target_class && assoc.direction == direction)
  end
end

#association_proxy(name, options = {}) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/neo4j/active_node/has_n.rb', line 212

def association_proxy(name, options = {})
  name = name.to_sym
  hash = association_proxy_hash(name, options)
  association_proxy_cache_fetch(hash) do
    if result_cache = self.instance_variable_get('@source_proxy_result_cache')
      cache = nil
      result_cache.inject(nil) do |proxy_to_return, object|
        proxy = fresh_association_proxy(name, options.merge(start_object: object),
                                        proc { (cache ||= previous_proxy_results_by_previous_id(result_cache, name))[object.neo_id] })

        object.association_proxy_cache[hash] = proxy

        (self == object ? proxy : proxy_to_return)
      end
    else
      fresh_association_proxy(name, options)
    end
  end
end

#association_proxy_cacheObject

Returns the current AssociationProxy cache for the association cache. It is in the format { :association_name => AssociationProxy} This is so that we

  • don’t need to re-build the QueryProxy objects

  • also because the QueryProxy object caches it’s results

  • so we don’t need to query again

  • so that we can cache results from association calls or eager loading



193
194
195
# File 'lib/neo4j/active_node/has_n.rb', line 193

def association_proxy_cache
  @association_proxy_cache ||= {}
end

#association_proxy_cache_fetch(key) ⇒ Object



197
198
199
200
201
202
# File 'lib/neo4j/active_node/has_n.rb', line 197

def association_proxy_cache_fetch(key)
  association_proxy_cache.fetch(key) do
    value = yield
    association_proxy_cache[key] = value
  end
end

#association_proxy_hash(name, options = {}) ⇒ Object



208
209
210
# File 'lib/neo4j/active_node/has_n.rb', line 208

def association_proxy_hash(name, options = {})
  [name.to_sym, options.values_at(:node, :rel, :labels, :rel_length)].hash
end

#association_query_proxy(name, options = {}) ⇒ Object



204
205
206
# File 'lib/neo4j/active_node/has_n.rb', line 204

def association_query_proxy(name, options = {})
  self.class.send(:association_query_proxy, name, {start_object: self}.merge!(options))
end

#reverse_association(association) ⇒ Object



238
239
240
241
242
243
# File 'lib/neo4j/active_node/has_n.rb', line 238

def reverse_association(association)
  reverse_assoc = self.class.associations.find do |_key, assoc|
    association.inverse_of?(assoc) || assoc.inverse_of?(association)
  end
  reverse_assoc && reverse_assoc.last
end

#validate_has_one_rel!(rel, other_node) ⇒ Object



250
251
252
253
# File 'lib/neo4j/active_node/has_n.rb', line 250

def validate_has_one_rel!(rel, other_node)
  raise_error = (node = send(rel.name.to_s)) && node != other_node
  fail(HasOneConstraintError, "node #{self.class}##{neo_id} has a has_one relationship with #{other_node.class}##{other_node.neo_id}") if raise_error
end

#validate_reverse_has_one_active_rel(active_rel, direction, other_node) ⇒ Object



245
246
247
248
# File 'lib/neo4j/active_node/has_n.rb', line 245

def validate_reverse_has_one_active_rel(active_rel, direction, other_node)
  rel = active_rel_corresponding_rel(active_rel, direction, other_node.class)
  validate_has_one_rel!(rel.last, other_node) if rel && rel.last.type == :has_one
end

#validate_reverse_has_one_core_rel(association, other_node) ⇒ Object



232
233
234
235
236
# File 'lib/neo4j/active_node/has_n.rb', line 232

def validate_reverse_has_one_core_rel(association, other_node)
  return unless Neo4j::Config[:enforce_has_one]
  reverse_assoc = reverse_association(association)
  validate_has_one_rel!(reverse_assoc, other_node) if reverse_assoc && reverse_assoc.type == :has_one
end