Module: Collection

Included in:
Array, Set
Defined in:
lib/collection/set.rb,
lib/collection/array.rb,
lib/collection/generic.rb,
lib/collection/collection.rb,
lib/collection/controls/array.rb,
lib/collection/controls/class.rb

Defined Under Namespace

Modules: Controls, Generic Classes: Array, Set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#type_parameterObject (readonly)

Returns the value of attribute type_parameter.



7
8
9
# File 'lib/collection/collection.rb', line 7

def type_parameter
  @type_parameter
end

Class Method Details

.Array(items, &implementation) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/collection/collection.rb', line 56

def self.Array(items, &implementation)
  items = Kernel::Array(items)

  raise ArgumentError, "Collection can't be empty" if items.empty?

  type_parameter = items[0].class

  cls = Array[type_parameter, &implementation]

  collection = cls.build(items)

  collection
end

.included(cls) ⇒ Object



2
3
4
5
# File 'lib/collection/collection.rb', line 2

def self.included(cls)
  cls.include Enumerable
  cls.extend Generic
end

.Set(items, &implementation) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/collection/collection.rb', line 42

def self.Set(items, &implementation)
  items = Kernel::Array(items)

  raise ArgumentError, "Collection can't be empty" if items.empty?

  type_parameter = items[0].class

  cls = Set[type_parameter, &implementation]

  collection = cls.build(items)

  collection
end

Instance Method Details

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



37
38
39
# File 'lib/collection/collection.rb', line 37

def ==(other)
  other.class == self.class && other.type_parameter == self.type_parameter && other.items == self.items
end

#add(val) ⇒ Object Also known as: <<



13
14
15
16
17
18
19
20
21
# File 'lib/collection/collection.rb', line 13

def add(val)
  if not val.is_a?(type_parameter)
    raise ArgumentError, "#{val.inspect} must be a #{type_parameter.name}"
  end

  items << val

  self
end

#clearObject



32
33
34
35
# File 'lib/collection/collection.rb', line 32

def clear
  items.clear
  self
end

#each(&action) ⇒ Object



24
25
26
# File 'lib/collection/collection.rb', line 24

def each(&action)
  items.each(&action)
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/collection/collection.rb', line 28

def empty?
  items.empty?
end

#initialize(type_parameter) ⇒ Object



9
10
11
# File 'lib/collection/collection.rb', line 9

def initialize(type_parameter)
  @type_parameter = type_parameter
end