Class: Vedeu::Collection
- Inherits:
-
Object
- Object
- Vedeu::Collection
- 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
- #collection ⇒ Array|Vedeu::Collection (also: #all) readonly
- #name ⇒ String
- #parent ⇒ Fixnum
Class Method Summary collapse
Instance Method Summary collapse
-
#[](value) ⇒ void
Fetch an entry from the collection via index.
-
#add(*other) ⇒ Vedeu::Collection
(also: #<<)
Adds an entry to the collection.
-
#each(&block) ⇒ Enumerator
Provides iteration over the collection.
-
#empty? ⇒ Boolean
Returns a boolean indicating whether the collection is empty.
-
#initialize(collection = [], parent = nil, name = nil) ⇒ Vedeu::Collection
constructor
Returns a new instance of Vedeu::Collection.
-
#size ⇒ Fixnum
Returns the size of the collection.
-
#to_s ⇒ String
Returns the collection as a String.
Constructor Details
#initialize(collection = [], parent = nil, name = nil) ⇒ Vedeu::Collection
Returns a new instance of Vedeu::Collection.
42 43 44 45 46 |
# File 'lib/vedeu/repositories/collection.rb', line 42 def initialize(collection = [], parent = nil, name = nil) @collection = collection @parent = parent @name = name end |
Instance Attribute Details
#collection ⇒ Array|Vedeu::Collection (readonly) Also known as: all
11 12 13 |
# File 'lib/vedeu/repositories/collection.rb', line 11 def collection @collection end |
#name ⇒ String
20 21 22 |
# File 'lib/vedeu/repositories/collection.rb', line 20 def name @name end |
#parent ⇒ Fixnum
16 17 18 |
# File 'lib/vedeu/repositories/collection.rb', line 16 def parent @parent end |
Class Method Details
.coerce(collection = [], parent = nil, name = nil) ⇒ Vedeu::Collection
26 27 28 29 30 31 32 33 34 |
# File 'lib/vedeu/repositories/collection.rb', line 26 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.
52 53 54 |
# File 'lib/vedeu/repositories/collection.rb', line 52 def [](value) collection[value] end |
#add(*other) ⇒ Vedeu::Collection Also known as: <<
Adds an entry to the collection.
60 61 62 |
# File 'lib/vedeu/repositories/collection.rb', line 60 def add(*other) self.class.new(@collection += other, parent, name) end |
#each(&block) ⇒ Enumerator
Provides iteration over the collection.
69 70 71 |
# File 'lib/vedeu/repositories/collection.rb', line 69 def each(&block) collection.each(&block) end |
#empty? ⇒ Boolean
Returns a boolean indicating whether the collection is empty.
76 77 78 |
# File 'lib/vedeu/repositories/collection.rb', line 76 def empty? collection.empty? end |
#size ⇒ Fixnum
Returns the size of the collection.
83 84 85 |
# File 'lib/vedeu/repositories/collection.rb', line 83 def size collection.size end |
#to_s ⇒ String
Returns the collection as a String.
90 91 92 |
# File 'lib/vedeu/repositories/collection.rb', line 90 def to_s collection.map(&:to_s).join end |