Class: Teapot::IdentitySet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/teapot/identity_set.rb

Overview

Very similar to a set but uses a specific callback (defaults to &:name) for object identity.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents = []) ⇒ IdentitySet

Returns a new instance of IdentitySet.



28
29
30
31
32
33
34
# File 'lib/teapot/identity_set.rb', line 28

def initialize(contents = [])
	@table = {}
	
	contents.each do |object|
		add(object)
	end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



36
37
38
# File 'lib/teapot/identity_set.rb', line 36

def table
  @table
end

Instance Method Details

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



52
53
54
# File 'lib/teapot/identity_set.rb', line 52

def add(object)
	@table[identity(object)] = object
end

#delete(object) ⇒ Object



58
59
60
# File 'lib/teapot/identity_set.rb', line 58

def delete(object)
	@table.delete(identity(object))
end

#each(&block) ⇒ Object



66
67
68
# File 'lib/teapot/identity_set.rb', line 66

def each(&block)
	@table.each_value(&block)
end

#freezeObject



38
39
40
41
42
# File 'lib/teapot/identity_set.rb', line 38

def freeze
	@table.freeze
	
	super
end

#identity(object) ⇒ Object



48
49
50
# File 'lib/teapot/identity_set.rb', line 48

def identity(object)
	object.name
end

#include?(object) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/teapot/identity_set.rb', line 62

def include?(object)
	@table.include?(identity(object))
end

#initialize_dup(other) ⇒ Object



44
45
46
# File 'lib/teapot/identity_set.rb', line 44

def initialize_dup(other)
	@table = other.table.dup
end

#slice(names) ⇒ Object



70
71
72
# File 'lib/teapot/identity_set.rb', line 70

def slice(names)
	names.collect{|name| @table[name]}
end