Class: ActiveCypher::Associations::CollectionProxy

Inherits:
Relation
  • Object
show all
Defined in:
lib/active_cypher/associations/collection_proxy.rb

Overview

CollectionProxy wraps association collections, providing lazy loading and mutation helpers. Because what’s one more layer between you and your data?

Constant Summary

Constants inherited from Relation

Relation::LOAD_METHODS

Instance Attribute Summary collapse

Attributes inherited from Relation

#cyrel_query, #model_class

Instance Method Summary collapse

Methods inherited from Relation

#count, #first, #last, #limit, #loaded?, #merge, #order, #reset!, #where

Constructor Details

#initialize(owner, reflection, base_relation) ⇒ CollectionProxy

Initializes the proxy, because direct access would be too easy.



20
21
22
23
24
25
26
# File 'lib/active_cypher/associations/collection_proxy.rb', line 20

def initialize(owner, reflection, base_relation)
  super(reflection[:class_name].constantize, base_relation.cyrel_query)

  @owner      = owner
  @reflection = reflection
  @records    = nil # lazy – load on first enumeration
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



9
10
11
# File 'lib/active_cypher/associations/collection_proxy.rb', line 9

def owner
  @owner
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



9
10
11
# File 'lib/active_cypher/associations/collection_proxy.rb', line 9

def reflection
  @reflection
end

Instance Method Details

#<<(*records) ⇒ self

hobby.people << bob

  • persists the edge (via the relationship model if supplied)

  • updates the in‑memory collection, so ‘include?` etc. work

Because shoveling objects into a collection is the pinnacle of ORM magic.



72
73
74
75
76
77
78
79
80
# File 'lib/active_cypher/associations/collection_proxy.rb', line 72

def <<(*records)
  unless owner.persisted?
    raise ActiveCypher::PersistenceError,
          'Cannot modify associations on a new record'
  end

  records.flatten.each { |rec| add_record(rec) }
  self
end

#each {|record| ... } ⇒ Object Also known as: to_a

Iterates over the records in the association. Because Rubyists love pretending their database is just an array.

Yields:

  • (record)

    Yields each record in the collection



36
37
38
39
# File 'lib/active_cypher/associations/collection_proxy.rb', line 36

def each(&)
  load_target unless @records
  @records.each(&)
end

#idsArray<String>

Convenient ‘ids` reader ─ used by a few specs. Because sometimes you just want the IDs and none of the commitment.



86
87
88
# File 'lib/active_cypher/associations/collection_proxy.rb', line 86

def ids
  map(&:internal_id)
end

#reloadself

Fully refresh from the database. For when you want to relive the disappointment of your data changing.



52
53
54
55
56
# File 'lib/active_cypher/associations/collection_proxy.rb', line 52

def reload
  @records = nil
  load_target
  self
end

#sizeInteger Also known as: length

Returns the size, because counting things is the only certainty in life.



45
# File 'lib/active_cypher/associations/collection_proxy.rb', line 45

def size   = load_target.size