Class: Treequel::BranchCollection

Inherits:
Object
  • Object
show all
Extended by:
Loggability, Delegation
Includes:
Enumerable, Constants
Defined in:
lib/treequel/branchcollection.rb

Overview

A Treequel::BranchCollection is a union of Treequel::Branchset objects, suitable for performing operations on multiple branches of the directory at once.

For example, if you have hosts under ou=Hosts in two different subdomains (e.g., acme.com, seattle.acme.com, and newyork.acme.com), and you want to search for a host by its CN, you could do so like this:

# Top-level hosts, and those in the 'seattle' subdomain, but not
# those in the 'newyork' subdomain:
west_coast_hosts = dir.ou( :hosts ) + dir.dc( :seattle ).ou( :hosts )
west_coast_www_hosts = west_coast_hosts.filter( :cn => 'www' )

# And one that includes hosts in all three DCs:
all_hosts = west_coast_hosts + dir.dc( :newyork ).ou( :hosts )
all_ns_hosts = all_hosts.filter( :cn => 'ns*' )

Note that you could accomplish most of what BranchCollection does using filters, but some people might find this a bit more readable.

Constant Summary

Constants included from Constants

Constants::CONTROL_NAMES, Constants::CONTROL_OIDS, Constants::EXTENSION_NAMES, Constants::EXTENSION_OIDS, Constants::FEATURE_NAMES, Constants::FEATURE_OIDS, Constants::MINIMAL_OPERATIONAL_ATTRIBUTES, Constants::SCOPE, Constants::SCOPE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

def_ivar_delegators, def_method_delegators, make_ivar_delegator, make_method_delegator

Constructor Details

#initialize(*branchsets) ⇒ BranchCollection

Create a new Treequel::BranchCollection that will operate on the given branchsets, which can be either Treequel::Branchset or Treequel::Branch objects.



80
81
82
83
84
85
86
87
88
# File 'lib/treequel/branchcollection.rb', line 80

def initialize( *branchsets )
	@branchsets = branchsets.flatten.collect do |obj|
		if obj.respond_to?( :each )
			obj
		else
			Treequel::Branchset.new( obj )
		end
	end
end

Instance Attribute Details

#branchsetsObject (readonly)

The collection's branchsets



101
# File 'lib/treequel/branchcollection.rb', line 101

def_method_delegators :branchsets, :include?

Instance Method Details

#&(other_collection) ⇒ Object

Return a new Treequel::BranchCollection that contains the union of the branchsets from both collections.



206
207
208
# File 'lib/treequel/branchcollection.rb', line 206

def &( other_collection )
	return self.class.new( self.branchsets & other_collection.branchsets )
end

#+(other_object) ⇒ Object

Return either a new Treequel::BranchCollection that includes both the receiver's Branchsets and those in other_object (if it responds_to #branchsets), or the results from executing the BranchCollection's search with other_object appended if it doesn't.



185
186
187
188
189
190
191
192
193
# File 'lib/treequel/branchcollection.rb', line 185

def +( other_object )
	if other_object.respond_to?( :branchsets )
		return self.class.new( self.branchsets + other_object.branchsets )
	elsif other_object.respond_to?( :collection )
		return self.class.new( self.branchsets + [other_object] )
	else
		return self.all + Array( other_object )
	end
end

#-(other_object) ⇒ Object

Return the results from each of the receiver's Branchsets without the other_object, which must respond to #dn.



198
199
200
201
# File 'lib/treequel/branchcollection.rb', line 198

def -( other_object )
	other_dn = other_object.dn
	return self.reject {|branch| branch.dn == other_dn }
end

#<<(object) ⇒ Object

Append operator: add the specified object (either a Treequel::Branchset or an object that responds to #branchset and returns a Treequel::Branchset) to the collection and return the receiver.



171
172
173
174
175
176
177
178
179
# File 'lib/treequel/branchcollection.rb', line 171

def <<( object )
	if object.respond_to?( :branchset )
		self.branchsets << object.branchset
	else
		self.branchsets << object
	end

	return self
end

#base_dnsObject

Return the base DN of all of the collection's Branchsets.



219
220
221
# File 'lib/treequel/branchcollection.rb', line 219

def base_dns
	return self.branchsets.collect {|bs| bs.base_dn }
end

#each(&block) ⇒ Object

Iterate over the Treequel::Branches found by each member branchset, yielding each one in turn.

Raises:

  • (LocalJumpError)


128
129
130
131
132
133
# File 'lib/treequel/branchcollection.rb', line 128

def each( &block )
	raise LocalJumpError, "no block given" unless block
	self.branchsets.each do |bs|
		bs.each( &block )
	end
end

#empty?Boolean

Return true if none of the collection's branches match any entries.

Returns:

  • (Boolean)


149
150
151
# File 'lib/treequel/branchcollection.rb', line 149

def empty?
	return self.branchsets.all? {|bs| bs.empty? } ? true : false
end

#filterObject

Delegator methods that clone the receiver with the results of mapping the branchsets with the delegated method.



95
96
# File 'lib/treequel/branchcollection.rb', line 95

def_cloning_delegators :filter, :scope, :select, :select_all, :select_more, :timeout,
:without_timeout

#firstObject

Return the first Treequel::Branch that is returned from the collection's branchsets.



137
138
139
140
141
142
143
144
145
# File 'lib/treequel/branchcollection.rb', line 137

def first
	branch = nil

	self.branchsets.each do |bs|
		break if branch = bs.first
	end

	return branch
end

#inspectObject

Return a human-readable string representation of the object suitable for debugging.



116
117
118
119
120
121
122
123
# File 'lib/treequel/branchcollection.rb', line 116

def inspect
	"#<%s:0x%0x %d branchsets: %p>" % [
		self.class.name,
		self.object_id * 2,
		self.branchsets.length,
		self.branchsets.collect {|bs| bs.to_s },
	]
end

#map(attribute = nil, &block) ⇒ Object

Overridden to support Branchset#map



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/treequel/branchcollection.rb', line 155

def map( attribute=nil, &block )
	if attribute
		if block
			super() {|branch| block.call(branch[attribute]) }
		else
			super() {|branch| branch[attribute] }
		end
	else
		super( &block )
	end
end

#|(other_collection) ⇒ Object

Return a new Treequel::BranchCollection that contains the intersection of the branchsets from both collections.



213
214
215
# File 'lib/treequel/branchcollection.rb', line 213

def |( other_collection )
	return self.class.new( self.branchsets | other_collection.branchsets )
end