Class: Vedeu::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vedeu/repositories/collection.rb

Overview

Convert an Array into an object which has some meaning in the context it is being used.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection = [], parent = nil, name = nil) ⇒ Vedeu::Collection

Returns a new instance of Vedeu::Collection.

Parameters:

  • collection (void) (defaults to: [])
  • parent (void) (defaults to: nil)
  • name (String|NilClass) (defaults to: nil)


43
44
45
46
47
# File 'lib/vedeu/repositories/collection.rb', line 43

def initialize(collection = [], parent = nil, name = nil)
  @collection = collection
  @parent     = parent
  @name       = name
end

Instance Attribute Details

#collectionArray|Vedeu::Collection (readonly) Also known as: all

Returns:



12
13
14
# File 'lib/vedeu/repositories/collection.rb', line 12

def collection
  @collection
end

#nameString

Returns:

  • (String)


21
22
23
# File 'lib/vedeu/repositories/collection.rb', line 21

def name
  @name
end

#parentFixnum

Returns:

  • (Fixnum)


17
18
19
# File 'lib/vedeu/repositories/collection.rb', line 17

def parent
  @parent
end

Class Method Details

.coerce(collection = [], parent = nil, name = nil) ⇒ Vedeu::Collection

Parameters:

  • collection (Array|Vedeu::Collection) (defaults to: [])
  • parent (void) (defaults to: nil)
  • name (String|NilClass) (defaults to: nil)

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/vedeu/repositories/collection.rb', line 27

def self.coerce(collection = [], parent = nil, name = nil)
  if collection.is_a?(Vedeu::Collection)
    collection

  else
    new(Array(collection), parent, name)

  end
end

Instance Method Details

#[](value) ⇒ void

This method returns an undefined value.

Fetch an entry from the collection via index.

Parameters:

  • value (Fixnum)


53
54
55
# File 'lib/vedeu/repositories/collection.rb', line 53

def [](value)
  collection[value]
end

#add(*other) ⇒ Vedeu::Collection Also known as: <<

Adds an entry to the collection.

Parameters:

Returns:



61
62
63
# File 'lib/vedeu/repositories/collection.rb', line 61

def add(*other)
  self.class.new(@collection += other, parent, name)
end

#each(&block) ⇒ Enumerator

Provides iteration over the collection.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


70
71
72
# File 'lib/vedeu/repositories/collection.rb', line 70

def each(&block)
  collection.each(&block)
end

#empty?Boolean

Returns a boolean indicating whether the collection is empty.

Returns:

  • (Boolean)


77
78
79
# File 'lib/vedeu/repositories/collection.rb', line 77

def empty?
  collection.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


85
86
87
# File 'lib/vedeu/repositories/collection.rb', line 85

def eql?(other)
  self.class == other.class && collection == other.collection
end

#sizeFixnum

Returns the size of the collection.

Returns:

  • (Fixnum)


93
94
95
# File 'lib/vedeu/repositories/collection.rb', line 93

def size
  collection.size
end

#to_sString Also known as: to_str

Returns the collection as a String.

Returns:

  • (String)


100
101
102
# File 'lib/vedeu/repositories/collection.rb', line 100

def to_s
  collection.map(&:to_s).join
end