class Disposable::Twin::Collection

Provides collection semantics like add, delete, and more for twin collections. Tracks additions and deletions in added and deleted.

Attributes

original[R]

Public Class Methods

for_models(twinner, models, *options) click to toggle source
# File lib/disposable/twin/collection.rb, line 6
def self.for_models(twinner, models, *options)
  new(twinner, models.collect { |model| twinner.(model, *options) })
end
new(twinner, items) click to toggle source
Calls superclass method
# File lib/disposable/twin/collection.rb, line 10
def initialize(twinner, items)
  super(items)
  @twinner  = twinner # DISCUSS: twin items here?
  @original = items
end

Public Instance Methods

<<(model) click to toggle source

Note that this expects a model, untwinned.

Calls superclass method
# File lib/disposable/twin/collection.rb, line 28
def <<(model)
  super(twin = @twinner.(model))
  added << twin
  # this will return the model, anyway.
end
added() click to toggle source

DISCUSS: am i a public concept, hard-wired into Collection?

# File lib/disposable/twin/collection.rb, line 67
def added
  @added ||= []
end
append(model) click to toggle source

Note that this expects a model, untwinned.

# File lib/disposable/twin/collection.rb, line 23
def append(model)
  (self << model).last
end
delete(twin) click to toggle source

Remove an item from a collection. This will not destroy the model.

Calls superclass method
# File lib/disposable/twin/collection.rb, line 42
def delete(twin)
  super(twin).tap do |res|
    deleted << twin if res
  end
end
deleted() click to toggle source

DISCUSS: am i a public concept, hard-wired into Collection?

# File lib/disposable/twin/collection.rb, line 72
def deleted
  @deleted ||= []
end
destroy(twin) click to toggle source

Deletes twin from collection and destroys it in save.

# File lib/disposable/twin/collection.rb, line 49
def destroy(twin)
  delete(twin)
  to_destroy << twin
end
destroyed() click to toggle source

DISCUSS: am i a public concept, hard-wired into Collection?

# File lib/disposable/twin/collection.rb, line 77
def destroyed
  @destroyed ||= []
end
find_by(options) click to toggle source
# File lib/disposable/twin/collection.rb, line 17
def find_by(options)
  field, value = options.to_a.first
  find { |item| item.send(field).to_s == value.to_s }
end
insert(index, model) click to toggle source

Note that this expects a model, untwinned.

Calls superclass method
# File lib/disposable/twin/collection.rb, line 35
def insert(index, model)
  super(index, twin = @twinner.(model))
  added << twin
  twin
end
save() click to toggle source
# File lib/disposable/twin/collection.rb, line 54
def save # only gets called when Collection::Semantics mixed in.
  destroy!
end

Private Instance Methods

destroy!() click to toggle source
# File lib/disposable/twin/collection.rb, line 86
def destroy!
  to_destroy.each do |twin|
    twin.send(:model).destroy
    destroyed << twin
  end
end
to_destroy() click to toggle source
# File lib/disposable/twin/collection.rb, line 82
def to_destroy
  @to_destroy ||= []
end