Class: Cathode::ObjectCollection
- Inherits:
-
Object
- Object
- Cathode::ObjectCollection
- Defined in:
- lib/cathode/object_collection.rb
Overview
Provides an enumerable interface for arrays of objects.
Instance Attribute Summary collapse
-
#objects ⇒ Object
Returns the value of attribute objects.
Instance Method Summary collapse
-
#add(items) ⇒ ObjectCollection
Adds a new object to the collection.
-
#delete(name) ⇒ ObjectCollection
Delets an object from the collection by name.
-
#find(name) ⇒ Object
Look up an object by its ‘name` property.
-
#initialize ⇒ ObjectCollection
constructor
A new instance of ObjectCollection.
-
#method_missing(method, args) ⇒ Object
Forwards all missing methods to the ‘objects` array stored in this collection.
-
#names ⇒ Array
An array of all the ‘name` properties in this object.
Constructor Details
#initialize ⇒ ObjectCollection
Returns a new instance of ObjectCollection.
9 10 11 |
# File 'lib/cathode/object_collection.rb', line 9 def initialize @objects = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, args) ⇒ Object
Forwards all missing methods to the ‘objects` array stored in this collection.
45 46 47 |
# File 'lib/cathode/object_collection.rb', line 45 def method_missing(method, args) objects.send method, args end |
Instance Attribute Details
#objects ⇒ Object
Returns the value of attribute objects.
4 5 6 |
# File 'lib/cathode/object_collection.rb', line 4 def objects @objects end |
Instance Method Details
#add(items) ⇒ ObjectCollection
Adds a new object to the collection.
29 30 31 32 33 |
# File 'lib/cathode/object_collection.rb', line 29 def add(items) items = [items] unless items.is_a?(Array) self.objects += items self end |
#delete(name) ⇒ ObjectCollection
Delets an object from the collection by name.
38 39 40 41 |
# File 'lib/cathode/object_collection.rb', line 38 def delete(name) objects.delete find(name) self end |
#find(name) ⇒ Object
Look up an object by its ‘name` property.
16 17 18 |
# File 'lib/cathode/object_collection.rb', line 16 def find(name) objects.detect { |o| o.name == name } end |
#names ⇒ Array
An array of all the ‘name` properties in this object.
22 23 24 |
# File 'lib/cathode/object_collection.rb', line 22 def names objects.map(&:name) end |