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/member.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) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/collection/collection.rb', line 58

def self.Array(items)
  items = Kernel::Array(items)

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

  type_parameter = items[0].class

  array = Array[type_parameter].new

  items.each do |member|
    array.add(member)
  end

  array
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) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.Set(items)
  items = Kernel::Array(items)

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

  type_parameter = items[0].class

  set = Set[type_parameter].new

  items.each do |member|
    set.add(member)
  end

  set
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.content == self.content
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

  content << val

  self
end

#clearObject



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

def clear
  content.clear
  self
end

#each(&action) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  content.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