Class: Disposable::Twin::Collection

Inherits:
Array
  • Object
show all
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

Modules: Changed, Semantics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Changed

#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

#originalObject (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, *options)
  new(twinner, models.collect { |model| twinner.(model, *options) })
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

#addedObject

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

#deletedObject

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

#destroyedObject

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(options)
  field, value = options.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

#saveObject

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