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?.
-
#append(model) ⇒ Object
Note that this expects a model, untwinned.
-
#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?.
- #find_by(options) ⇒ Object
-
#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, *options) ⇒ 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.
28 29 30 31 32 |
# File 'lib/disposable/twin/collection.rb', line 28 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?
67 68 69 |
# File 'lib/disposable/twin/collection.rb', line 67 def added @added ||= [] end |
#append(model) ⇒ Object
Note that this expects a model, untwinned.
23 24 25 |
# File 'lib/disposable/twin/collection.rb', line 23 def append(model) (self << model).last end |
#delete(twin) ⇒ Object
Remove an item from a collection. This will not destroy the model.
42 43 44 45 46 |
# File 'lib/disposable/twin/collection.rb', line 42 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?
72 73 74 |
# File 'lib/disposable/twin/collection.rb', line 72 def deleted @deleted ||= [] end |
#destroy(twin) ⇒ Object
Deletes twin from collection and destroys it in #save.
49 50 51 52 |
# File 'lib/disposable/twin/collection.rb', line 49 def destroy(twin) delete(twin) to_destroy << twin end |
#destroyed ⇒ Object
DISCUSS: am i a public concept, hard-wired into Collection?
77 78 79 |
# File 'lib/disposable/twin/collection.rb', line 77 def destroyed @destroyed ||= [] end |
#find_by(options) ⇒ Object
17 18 19 20 |
# File 'lib/disposable/twin/collection.rb', line 17 def find_by() field, value = .to_a.first find { |item| item.send(field).to_s == value.to_s } end |
#insert(index, model) ⇒ Object
Note that this expects a model, untwinned.
35 36 37 38 39 |
# File 'lib/disposable/twin/collection.rb', line 35 def insert(index, model) super(index, twin = @twinner.(model)) added << twin twin end |
#save ⇒ Object
only gets called when Collection::Semantics mixed in.
54 55 56 |
# File 'lib/disposable/twin/collection.rb', line 54 def save # only gets called when Collection::Semantics mixed in. destroy! end |