Class: Ldaptic::Filter::Hash

Inherits:
Abstract show all
Defined in:
lib/ldaptic/filter.rb

Overview

A hash is the most general and most useful type of filter builder.

Ldaptic::Filter(
  :givenName => "David",
  :sn! => "Thomas",
  :postalCode => (70000..80000)
).to_s # => "(&(givenName=David)(&(postalCode>=70000)(postalCode<=80000))(!(sn=Thomas)))"

Including :* => true allows asterisks to pass through unaltered. Otherwise, they are escaped.

Ldaptic::Filter(:givenName => "Dav*", :* => true).to_s # => "(givenName=Dav*)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#&, #inspect, #to_ber, #to_net_ldap_filter, #to_s, #|, #~

Constructor Details

#initialize(hash) ⇒ Hash

Call Ldaptic::Filter(hash) instead of instantiating this class directly.



194
195
196
197
# File 'lib/ldaptic/filter.rb', line 194

def initialize(hash)
  @hash = hash.dup
  @escape_asterisks = !@hash.delete(:*)
end

Instance Attribute Details

#escape_asterisksObject

Returns the value of attribute escape_asterisks.



190
191
192
# File 'lib/ldaptic/filter.rb', line 190

def escape_asterisks
  @escape_asterisks
end

#hashObject (readonly)

Returns the value of attribute hash.



191
192
193
# File 'lib/ldaptic/filter.rb', line 191

def hash
  @hash
end

Instance Method Details

#processObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/ldaptic/filter.rb', line 199

def process
  string = @hash.map {|k, v| [k.to_s, v]}.sort.map do |(k, v)|
    Pair.new(k, v, @escape_asterisks ? "==" : "=~").process
  end.join
  case @hash.size
  when 0 then nil
  when 1 then string
  else "(&#{string})"
  end
end