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 "Unknown type for attribute #@name"
  elsif @syntax.nil?
    @entry.logger "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. Currently, a duplicate is determined by == (case sensitive) rather than by the server (typically case insensitive). All arrays are flattened.



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

def add(*attributes)
  dest = @target.dup
  safe_array(attributes).each do |attribute|
    dest.push(attribute) unless include?(attribute)
  end
  replace(dest)
end

#add!(*attributes) ⇒ Object

Add the desired attributes to the LDAP server immediately.



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

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

#as_json(*args) ⇒ Object

:nodoc:



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

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



115
116
117
118
# File 'lib/ldaptic/attribute_set.rb', line 115

def clear
  replace([])
  self
end

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



155
156
157
# File 'lib/ldaptic/attribute_set.rb', line 155

def collect!(&block)
  replace(to_a.collect(&block))
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.

Two passes are made to find each element, one case sensitive and one ignoring case, before giving up.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ldaptic/attribute_set.rb', line 125

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| x.downcase == attribute.downcase}
      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.



150
151
152
153
# File 'lib/ldaptic/attribute_set.rb', line 150

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

#delete_if(&block) ⇒ Object



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

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

#forbidden?Boolean

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

Returns:

  • (Boolean)


202
203
204
# File 'lib/ldaptic/attribute_set.rb', line 202

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

#human_nameObject

Invokes human_attribute_name on the attribute’s name.



249
250
251
# File 'lib/ldaptic/attribute_set.rb', line 249

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

#insert(index, *objects) ⇒ Object



160
161
162
163
164
# File 'lib/ldaptic/attribute_set.rb', line 160

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

#inspectObject



236
237
238
# File 'lib/ldaptic/attribute_set.rb', line 236

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

#mandatory?Boolean

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

Returns:

  • (Boolean)


207
208
209
# File 'lib/ldaptic/attribute_set.rb', line 207

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

#no_user_modification?Boolean

Returns true for read only attributes.

Returns:

  • (Boolean)


217
218
219
# File 'lib/ldaptic/attribute_set.rb', line 217

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

#oneObject

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



222
223
224
225
226
227
228
# File 'lib/ldaptic/attribute_set.rb', line 222

def one
  if single_value?
    first
  else
    self
  end
end

#reject!(&block) ⇒ Object



170
171
172
173
174
175
# File 'lib/ldaptic/attribute_set.rb', line 170

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.



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

def replace(*attributes)
  attributes = safe_array(attributes)
  user_modification_guard
  @target.replace(attributes)
  self
end

#replace!(*attributes) ⇒ Object

Replace the entire attribute at the LDAP server immediately.



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

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)


212
213
214
# File 'lib/ldaptic/attribute_set.rb', line 212

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



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

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



232
233
234
# File 'lib/ldaptic/attribute_set.rb', line 232

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

#unshift(*values) ⇒ Object



166
167
168
# File 'lib/ldaptic/attribute_set.rb', line 166

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