File: collection.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (110 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'active_ldap/association/proxy'

module ActiveLdap
  module Association
    class Collection < Proxy
      include Enumerable

      def to_ary
        load_target
        @target.to_ary
      end

      def reset
        @target = []
        @loaded = false
      end

      def <<(*entries)
        add_entries(*entries)
      end
      alias_method(:push, :<<)
      alias_method(:concat, :<<)

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

      def delete(*entries)
        entries = flatten_deeper(entries).reject do |entry|
          @target.delete(entry) if entry.new_entry?
          entry.new_entry?
        end
        return if entries.empty?

        delete_entries(entries)
        entries.each do |entry|
          @target.delete(entry)
        end
      end

      def replace(others)
        load_target

        entry = @target.first
        if entry.nil?
          deleted_entries = []
          added_entries = others
        else
          base_class = entry.class
          others = others.collect do |other|
            other = base_class.find(other) unless other.is_a?(base_class)
            other
          end
          deleted_entries = @target - others
          added_entries = others - @target
        end

        delete(deleted_entries)
        concat(added_entries)
      end

      def exists?
        load_target
        not @target.empty?
      end

      private
      def flatten_deeper(array)
        array.collect do |element|
          element.respond_to?(:flatten) ? element.flatten : element
        end.flatten
      end

      def normalize_entry(entry)
        entry
      end

      def insert_entry(entry)
        entry[@options[:foreign_key_name]] = @owner[@options[:local_key_name]]
        entry.save
      end

      def add_entries(*entries)
        result = true
        load_target

        flatten_deeper(entries).each do |entry|
          entry = normalize_entry(entry)
          unless @owner.new_entry?
            infect_connection(entry)
            result &&= insert_entry(entry)
          end
          @target << entry
        end

        result && self
      end

      def dn_values_to_string_values(values)
        values.collect do |value|
          if value.is_a?(DN)
            value.to_s
          else
            value
          end
        end
      end
    end
  end
end