Class: Ldaptic::AttributeSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ldaptic/attribute_set.rb

Overview

AttributeSet, like the name suggests, represents a set of attributes. Most operations are delegated to an array, so the usual array methods should work transparently.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, name, target) ⇒ AttributeSet

Returns a new instance of AttributeSet.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ldaptic/attribute_set.rb', line 27

def initialize(entry, name, target)
  @entry  = entry
  @name   = Ldaptic.encode(name)
  @type   = @entry.namespace.attribute_type(@name)
  @syntax = @entry.namespace.attribute_syntax(@name)
  @target = target
  if @type.nil?
    @entry.logger.warn "Unknown type for attribute #@name"
  elsif @syntax.nil?
    @entry.logger.warn "Unknown syntax #{@type.syntax_oid} for attribute #{@name}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegates to an array.



55
56
57
# File 'lib/ldaptic/attribute_set.rb', line 55

def method_missing(method, *args, &block)
  to_a.send(method, *args, &block)
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



9
10
11
# File 'lib/ldaptic/attribute_set.rb', line 9

def entry
  @entry
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ldaptic/attribute_set.rb', line 9

def name
  @name
end

#syntaxObject (readonly)

Returns the value of attribute syntax.



9
10
11
# File 'lib/ldaptic/attribute_set.rb', line 9

def syntax
  @syntax
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/ldaptic/attribute_set.rb', line 9

def type
  @type
end

Instance Method Details

#===(object) ⇒ Object



59
60
61
# File 'lib/ldaptic/attribute_set.rb', line 59

def ===(object)
  to_a === object
end

#add(*attributes) ⇒ Object Also known as: <<, concat, push

Adds the given attributes, discarding duplicates. All arrays are flattened.



109
110
111
# File 'lib/ldaptic/attribute_set.rb', line 109

def add(*attributes)
  replace(@target + safe_array(attributes))
end

#add!(*attributes) ⇒ Object

Add the desired attributes to the LDAP server immediately.



117
118
119
120
# File 'lib/ldaptic/attribute_set.rb', line 117

def add!(*attributes)
  @entry.add!(@name, safe_array(attributes))
  self
end

#as_json(*args) ⇒ Object

:nodoc:



267
268
269
# File 'lib/ldaptic/attribute_set.rb', line 267

def as_json(*args) #:nodoc:
  to_a.as_json(*args)
end

#before_type_castObject

The original attributes before type conversion. Mutating the result mutates the original attributes.



13
14
15
# File 'lib/ldaptic/attribute_set.rb', line 13

def before_type_cast
  @target
end

#clearObject



146
147
148
149
# File 'lib/ldaptic/attribute_set.rb', line 146

def clear
  replace([])
  self
end

#collect!(&block) ⇒ Object Also known as: map!



183
184
185
# File 'lib/ldaptic/attribute_set.rb', line 183

def collect!(&block)
  replace(to_a.collect(&block))
end

#compare(target) ⇒ Object

Like #include?, but asks the server rather than checking locally.



103
104
105
# File 'lib/ldaptic/attribute_set.rb', line 103

def compare(target)
  @entry.compare(@name, target)
end

#delete(*attributes, &block) ⇒ Object Also known as: subtract

Remove the given attributes given, functioning more or less like Array#delete, except accepting multiple arguments.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ldaptic/attribute_set.rb', line 153

def delete(*attributes, &block)
  return clear if attributes.flatten.empty?
  dest = @target.dup
  ret = []
  safe_array(attributes).each do |attribute|
    ret << dest.delete(attribute) do
      match = dest.detect {|x| matchable(x) == matchable(attribute)}
      if match
        dest.delete(match)
      else
        yield(attribute) if block_given?
      end
    end
  end
  replace(dest)
  if attributes.size == 1 && !attributes.first.kind_of?(Array)
    typecast ret.first
  else
    self
  end
end

#delete!(*attributes) ⇒ Object

Delete the desired values from the attribute at the LDAP server. If no values are given, the entire attribute is removed.



178
179
180
181
# File 'lib/ldaptic/attribute_set.rb', line 178

def delete!(*attributes)
  @entry.delete!(@name, safe_array(attributes))
  self
end

#delete_if(&block) ⇒ Object



204
205
206
207
# File 'lib/ldaptic/attribute_set.rb', line 204

def delete_if(&block)
  reject!(&block)
  self
end

#each(&block) ⇒ Object



23
24
25
# File 'lib/ldaptic/attribute_set.rb', line 23

def each(&block)
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ldaptic/attribute_set.rb', line 76

def empty?
  @target.empty?
end

#eql?(object) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


63
64
65
# File 'lib/ldaptic/attribute_set.rb', line 63

def eql?(object)
  to_a.eql?(object)
end

#errorsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ldaptic/attribute_set.rb', line 40

def errors
  return ['is forbidden'] if forbidden? && !empty?
  errors = []
  if single_value? && size > 1
    errors << "does not accept multiple values"
  elsif mandatory? && empty?
    errors << "is mandatory"
  end
  if syntax_object
    errors += @target.map { |v| syntax_object.error(v) }.compact
  end
  errors
end

#exclude?(target) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ldaptic/attribute_set.rb', line 98

def exclude?(target)
  !index(target)
end

#forbidden?Boolean

Returns true if the attribute is marked neither MUST nor MAY in the object class.

Returns:

  • (Boolean)


229
230
231
# File 'lib/ldaptic/attribute_set.rb', line 229

def forbidden?
  !(@entry.must + @entry.may).include?(@name)
end

#human_nameObject

Invokes human_attribute_name on the attribute’s name.



276
277
278
# File 'lib/ldaptic/attribute_set.rb', line 276

def human_name
  @entry.class.human_attribute_name(@name)
end

#include?(target) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ldaptic/attribute_set.rb', line 94

def include?(target)
  !!index(target)
end

#index(*args, &block) ⇒ Object Also known as: find_index, rindex



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ldaptic/attribute_set.rb', line 80

def index(*args, &block)
  if block_given? || args.size != 1
    return to_a.index(*args, &block)
  else
    target = matchable(args.first)
    @target.each_with_index do |candidate, index|
      return index if matchable(candidate) == target
    end
  end
  nil
end

#insert(index, *objects) ⇒ Object



188
189
190
191
# File 'lib/ldaptic/attribute_set.rb', line 188

def insert(index, *objects)
  replace(@target.dup.insert(index, *safe_array(objects)))
  self
end

#inspectObject



263
264
265
# File 'lib/ldaptic/attribute_set.rb', line 263

def inspect
  "<#{to_a.inspect}>"
end

#mandatory?Boolean

Returns true if the attribute is marked MUST in the object class.

Returns:

  • (Boolean)


234
235
236
# File 'lib/ldaptic/attribute_set.rb', line 234

def mandatory?
  @entry.must.include?(@name)
end

#no_user_modification?Boolean

Returns true for read only attributes.

Returns:

  • (Boolean)


244
245
246
# File 'lib/ldaptic/attribute_set.rb', line 244

def no_user_modification?
  @type && @type.no_user_modification?
end

#oneObject

If the attribute is a single value, return it, otherwise, return self.



249
250
251
252
253
254
255
# File 'lib/ldaptic/attribute_set.rb', line 249

def one
  if single_value?
    first
  else
    self
  end
end

#reject!(&block) ⇒ Object



197
198
199
200
201
202
# File 'lib/ldaptic/attribute_set.rb', line 197

def reject!(&block)
  user_modification_guard
  @target.reject! do |value|
    yield(typecast(value))
  end
end

#replace(*attributes) ⇒ Object

Does a complete replacement of the attributes. Multiple attributes can be given as either multiple arguments or as an array.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ldaptic/attribute_set.rb', line 124

def replace(*attributes)
  attributes = safe_array(attributes)
  user_modification_guard
  seen = {}
  filtered = []
  attributes.each do |value|
    matchable = matchable(value)
    unless seen[matchable]
      filtered << value
      seen[matchable] = true
    end
  end
  @target.replace(filtered)
  self
end

#replace!(*attributes) ⇒ Object

Replace the entire attribute at the LDAP server immediately.



141
142
143
144
# File 'lib/ldaptic/attribute_set.rb', line 141

def replace!(*attributes)
  @entry.replace!(@name, safe_array(attributes))
  self
end

#respond_to?(method, *args) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


68
69
70
# File 'lib/ldaptic/attribute_set.rb', line 68

def respond_to?(method, *args) #:nodoc:
  super || @target.respond_to?(method, *args)
end

#single_value?Boolean

Returns true if the attribute may not be specified more than once.

Returns:

  • (Boolean)


239
240
241
# File 'lib/ldaptic/attribute_set.rb', line 239

def single_value?
  @type && @type.single_value?
end

#sizeObject



72
73
74
# File 'lib/ldaptic/attribute_set.rb', line 72

def size
  @target.size
end

#syntax_objectObject



271
272
273
# File 'lib/ldaptic/attribute_set.rb', line 271

def syntax_object
  @syntax && @syntax.object.new(@entry)
end

#to_aObject Also known as: to_ary



17
18
19
# File 'lib/ldaptic/attribute_set.rb', line 17

def to_a
  typecast(@target)
end

#to_sObject



259
260
261
# File 'lib/ldaptic/attribute_set.rb', line 259

def to_s
  @target.join("\n")
end

#unshift(*values) ⇒ Object



193
194
195
# File 'lib/ldaptic/attribute_set.rb', line 193

def unshift(*values)
  insert(0, *values)
end