Class: Rcal::Util::TypesafeList

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Rcal::Util
Defined in:
lib/rcal/util/typesafe_list.rb

Overview

A typesafe list - that is, a list that allows only things of type T to be added. T can be a class or module, and checks use kind_of?, so inherited classes work fine.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TypesafeList) initialize(klass)

Create a new TypesafeList of type klass.

Raises ArgumentError if klass is not a class or module.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/rcal/util/typesafe_list.rb', line 18

def initialize(klass)
  raise ArgumentError.new("#{klass} is not a class or module") unless
    klass.kind_of?(Module)
  @klass = klass
  @array = []
end

Instance Attribute Details

- (Object) klass (readonly)

Returns the value of attribute klass



10
11
12
# File 'lib/rcal/util/typesafe_list.rb', line 10

def klass
  @klass
end

Instance Method Details

- (Object) +(other)

Concatenates self with other. If other is a TypesafeList of the same type as this list, the result is also a TypesafeList of the same type. Otherwise, the result is an Array.

Returns a TypesafeList or Array.

Raises ArgumentError if other does not respond to each.

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rcal/util/typesafe_list.rb', line 101

def +(other)
  raise ArgumentError.new("Cannot concatenate a TypesafeList with #{other}") unless
    other.respond_to?(:each)
    
  if same_kind_as?(other)
    result = TypesafeList.new(klass)
    result.array = self.array.dup
  else
    result = self.to_a
  end
  
  other.each { |i| result << i }
  
  result
end

- (Object) <<(obj)

Adds obj to the end of the list.

Returns self.

Raises TypeError if obs is not a klass.



77
78
79
80
81
# File 'lib/rcal/util/typesafe_list.rb', line 77

def <<(obj)
  check!(obj)
  array << obj
  self
end

- (Object) ==(other)



125
126
127
# File 'lib/rcal/util/typesafe_list.rb', line 125

def ==(other)
  same_kind_as?(other) && self.array == other.array
end

- (Object) clear

Empties the list.

Returns self.



28
29
30
31
# File 'lib/rcal/util/typesafe_list.rb', line 28

def clear
  array.clear
  self
end

- (Object) each(&block)

Calls block once for each element in self, passing that element as a parameter.



47
48
49
# File 'lib/rcal/util/typesafe_list.rb', line 47

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

- (Boolean) empty?

Returns whether or not the list is empty.

Returns:

  • (Boolean)


34
35
36
# File 'lib/rcal/util/typesafe_list.rb', line 34

def empty?
  array.empty?
end

- (Object) first

Returns the first element in the list, or nil if the list is empty.



52
53
54
# File 'lib/rcal/util/typesafe_list.rb', line 52

def first
  array.first
end

- (Object) last

Returns the last element in the list, or nil if the list is empty.



57
58
59
# File 'lib/rcal/util/typesafe_list.rb', line 57

def last
  array.last
end

- (Object) length Also known as: size

Returns the list's length.



39
40
41
# File 'lib/rcal/util/typesafe_list.rb', line 39

def length
  array.length
end

- (Object) push(*objs)

Adds each of objs to the end of the list.

Returns self.

Raises TypeError if any of objs is not a klass.



66
67
68
69
70
# File 'lib/rcal/util/typesafe_list.rb', line 66

def push(*objs)
  check!(*objs)
  array.push(*objs)
  self
end

- (Object) to_a



117
118
119
# File 'lib/rcal/util/typesafe_list.rb', line 117

def to_a
  array.dup
end

- (Object) to_ical



121
122
123
# File 'lib/rcal/util/typesafe_list.rb', line 121

def to_ical
  array.to_ical
end

- (Object) unshift(*objs)

Adds objs to the front of the list.

Returns self.

Raises TypeError if any of objs is not a klass.



88
89
90
91
92
# File 'lib/rcal/util/typesafe_list.rb', line 88

def unshift(*objs)
  check!(*objs)
  array.unshift(*objs)
  self
end