Class: Ldaptic::Filter::Pair

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

Overview

Internal class used to process a single entry from a hash.

Constant Summary collapse

INVERSE_OPERATORS =
{
  "!=" => "==",
  "!~" => "=~",
  ">"  => "<=",
  "<"  => ">="
}

Instance Method Summary collapse

Methods inherited from Abstract

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

Constructor Details

#initialize(key, value, operator) ⇒ Pair

Returns a new instance of Pair.



219
220
221
222
223
224
225
226
# File 'lib/ldaptic/filter.rb', line 219

def initialize(key, value, operator)
  @key, @value, @operator = key.to_s.dup, value, operator.to_s
  @inverse = !!@key.sub!(/!$/, '')
  if op = INVERSE_OPERATORS[@operator]
    @inverse ^= true
    @operator = op
  end
end

Instance Method Details

#processObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/ldaptic/filter.rb', line 228

def process
  k = @key
  v = @value
  if @operator == "=~"
    operator = "=="
    star = true
  else
    operator = @operator
    star = false
  end
  inverse = @inverse
  operator = "=" if operator == "=="
  if v.respond_to?(:to_ary)
    q = "(|" + v.map {|e| "(#{Ldaptic.encode(k)}=#{Ldaptic.escape(e, star)})"}.join + ")"
  elsif v.kind_of?(Range)
    q = []
    if v.first != -1.0/0
      q << "(#{Ldaptic.encode(k)}>=#{Ldaptic.escape(v.first, star)})"
    end
    if v.last != 1.0/0
      if v.exclude_end?
        q << "(!(#{Ldaptic.encode(k)}>=#{Ldaptic.escape(v.last, star)}))"
      else
        q << "(#{Ldaptic.encode(k)}<=#{Ldaptic.escape(v.last, star)})"
      end
    end
    q = "(&#{q*""})"
  elsif v == true || v == :*
    q = "(#{Ldaptic.encode(k)}=*)"
  elsif !v
    q = "(#{Ldaptic.encode(k)}=*)"
    inverse ^= true
  else
    q = "(#{Ldaptic.encode(k)}#{operator}#{Ldaptic.escape(v, star)})"
  end
  inverse ? "(!#{q})" : q
end