Class: Build::Dependency::Set

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/build/dependency/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 = []) ⇒ Set

Initialize a new set with optional initial contents.



16
17
18
19
20
21
22
# File 'lib/build/dependency/set.rb', line 16

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

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



24
25
26
# File 'lib/build/dependency/set.rb', line 24

def table
  @table
end

Instance Method Details

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

Add an object to the set.



55
56
57
58
59
60
61
# File 'lib/build/dependency/set.rb', line 55

def add(object)
	if include?(object)
		raise KeyError, "Object #{identity(object)} already exists!"
	end
	
	@table[identity(object)] = object
end

#delete(object) ⇒ Object

Delete an object from the set.



68
69
70
# File 'lib/build/dependency/set.rb', line 68

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

#each(&block) ⇒ Object

Iterate over each object in the set.



81
82
83
# File 'lib/build/dependency/set.rb', line 81

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

#freezeObject

Freeze the set.



31
32
33
34
35
36
37
# File 'lib/build/dependency/set.rb', line 31

def freeze
	return self if frozen?
	
	@table.freeze
	
	super
end

#identity(object) ⇒ Object

Get the identity of an object for use as a hash key.



48
49
50
# File 'lib/build/dependency/set.rb', line 48

def identity(object)
	object.name
end

#include?(object) ⇒ Boolean

Check if the set includes an object.

Returns:

  • (Boolean)


75
76
77
# File 'lib/build/dependency/set.rb', line 75

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

#initialize_dup(other) ⇒ Object

Initialize a duplicate of another set.



41
42
43
# File 'lib/build/dependency/set.rb', line 41

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

#slice(names) ⇒ Object

Get a subset of objects by their names.



88
89
90
# File 'lib/build/dependency/set.rb', line 88

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