Class: Cathode::ObjectCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/cathode/object_collection.rb

Overview

Provides an enumerable interface for arrays of objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectCollection

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

#objectsObject

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.

Parameters:

  • items (Object, Array)

    A single object or an array of objects to add

Returns:



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.

Parameters:

  • name (Symbol)

    The name of the object to remove

Returns:



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.

Parameters:

  • name (Symbol)

    The object’s name

Returns:

  • (Object)


16
17
18
# File 'lib/cathode/object_collection.rb', line 16

def find(name)
  objects.detect { |o| o.name == name }
end

#namesArray

An array of all the ‘name` properties in this object.

Returns:

  • (Array)


22
23
24
# File 'lib/cathode/object_collection.rb', line 22

def names
  objects.map(&:name)
end