Class: ActiveCypher::Associations::CollectionProxy
- 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
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Attributes inherited from Relation
Instance Method Summary collapse
-
#<<(*records) ⇒ self
hobby.people << bob.
-
#each {|record| ... } ⇒ Object
(also: #to_a)
Iterates over the records in the association.
-
#ids ⇒ Array<String>
Convenient ‘ids` reader ─ used by a few specs.
-
#initialize(owner, reflection, base_relation) ⇒ CollectionProxy
constructor
Initializes the proxy, because direct access would be too easy.
-
#reload ⇒ self
Fully refresh from the database.
-
#size ⇒ Integer
(also: #length)
Returns the size, because counting things is the only certainty in life.
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
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
9 10 11 |
# File 'lib/active_cypher/associations/collection_proxy.rb', line 9 def owner @owner end |
#reflection ⇒ Object (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.
36 37 38 39 |
# File 'lib/active_cypher/associations/collection_proxy.rb', line 36 def each(&) load_target unless @records @records.each(&) end |
#ids ⇒ Array<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 |
#reload ⇒ self
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 |
#size ⇒ Integer 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 |