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

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.



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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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)


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

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)


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

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.



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

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.



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

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.



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

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



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

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.



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

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