Class: Pluggaloid::Collection
- Inherits:
-
Object
- Object
- Pluggaloid::Collection
- Defined in:
- lib/pluggaloid/collection.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #add(*v) ⇒ Object (also: #<<)
- #argument_hash_same?(specs) ⇒ Boolean
- #delete(*v) ⇒ Object
-
#initialize(event, *args) ⇒ Collection
constructor
A new instance of Collection.
- #rewind(&block) ⇒ Object
Constructor Details
#initialize(event, *args) ⇒ Collection
Returns a new instance of Collection.
7 8 9 10 11 12 13 |
# File 'lib/pluggaloid/collection.rb', line 7 def initialize(event, *args) @event = event args[event.collect_index] = nil @args = args.freeze @spec = argument_hash(args) @values = [].freeze end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
5 6 7 |
# File 'lib/pluggaloid/collection.rb', line 5 def values @values end |
Instance Method Details
#add(*v) ⇒ Object Also known as: <<
15 16 17 18 19 |
# File 'lib/pluggaloid/collection.rb', line 15 def add(*v) rewind do |primitive| primitive + v end end |
#argument_hash_same?(specs) ⇒ Boolean
45 46 47 |
# File 'lib/pluggaloid/collection.rb', line 45 def argument_hash_same?(specs) @spec == argument_hash(specs) end |
#delete(*v) ⇒ Object
22 23 24 25 26 |
# File 'lib/pluggaloid/collection.rb', line 22 def delete(*v) rewind do |primitive| primitive - v end end |
#rewind(&block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pluggaloid/collection.rb', line 28 def rewind(&block) new_values = block.(@values.dup) added, deleted = new_values - @values, @values - new_values @values = new_values.freeze unless added.empty? args = @args.dup args[@event.collect_index] = added @event.collection_add_event.call(*args) end unless deleted.empty? args = @args.dup args[@event.collect_index] = deleted @event.collection_delete_event.call(*args) end self end |