Class: Disposable::Twin::Collection
- Inherits:
-
Array
- Object
- Array
- Disposable::Twin::Collection
- Includes:
- Changed
- Defined in:
- lib/disposable/twin/collection.rb
Overview
Provides collection semantics like add, delete, and more for twin collections. Tracks additions and deletions in #added and #deleted.
Defined Under Namespace
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
TODO: test me and rethink me.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(model) ⇒ Object
Note that this expects a model, untwinned.
-
#added ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?.
-
#delete(twin) ⇒ Object
Remove an item from a collection.
-
#deleted ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?.
-
#destroy(twin) ⇒ Object
Deletes twin from collection and destroys it in #save.
-
#destroyed ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?.
-
#initialize(twinner, items) ⇒ Collection
constructor
A new instance of Collection.
-
#insert(index, model) ⇒ Object
Note that this expects a model, untwinned.
-
#save ⇒ Object
only gets called when Collection::Semantics mixed in.
Methods included from Changed
Constructor Details
#initialize(twinner, items) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 |
# File 'lib/disposable/twin/collection.rb', line 10 def initialize(twinner, items) super(items) @twinner = twinner # DISCUSS: twin items here? @original = items end |
Instance Attribute Details
#original ⇒ Object (readonly)
TODO: test me and rethink me.
15 16 17 |
# File 'lib/disposable/twin/collection.rb', line 15 def original @original end |
Class Method Details
.for_models(twinner, models) ⇒ Object
6 7 8 |
# File 'lib/disposable/twin/collection.rb', line 6 def self.for_models(twinner, models) new(twinner, models.collect { |model| twinner.(model) }) end |
Instance Method Details
#<<(model) ⇒ Object
Note that this expects a model, untwinned.
18 19 20 21 22 |
# File 'lib/disposable/twin/collection.rb', line 18 def <<(model) super(twin = @twinner.(model)) added << twin # this will return the model, anyway. end |
#added ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?
57 58 59 |
# File 'lib/disposable/twin/collection.rb', line 57 def added @added ||= [] end |
#delete(twin) ⇒ Object
Remove an item from a collection. This will not destroy the model.
32 33 34 35 36 |
# File 'lib/disposable/twin/collection.rb', line 32 def delete(twin) super(twin).tap do |res| deleted << twin if res end end |
#deleted ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?
62 63 64 |
# File 'lib/disposable/twin/collection.rb', line 62 def deleted @deleted ||= [] end |
#destroy(twin) ⇒ Object
Deletes twin from collection and destroys it in #save.
39 40 41 42 |
# File 'lib/disposable/twin/collection.rb', line 39 def destroy(twin) delete(twin) to_destroy << twin end |
#destroyed ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?
67 68 69 |
# File 'lib/disposable/twin/collection.rb', line 67 def destroyed @destroyed ||= [] end |
#insert(index, model) ⇒ Object
Note that this expects a model, untwinned.
25 26 27 28 29 |
# File 'lib/disposable/twin/collection.rb', line 25 def insert(index, model) super(index, twin = @twinner.(model)) added << twin twin end |
#save ⇒ Object
only gets called when Collection::Semantics mixed in.
44 45 46 |
# File 'lib/disposable/twin/collection.rb', line 44 def save # only gets called when Collection::Semantics mixed in. destroy! end |