Method: Forester::Validators#validate_uniqueness_of_fields

Defined in:
lib/forester/tree_node_ext/validators.rb

#validate_uniqueness_of_fields(fields, options = {}) ⇒ Object



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
# File 'lib/forester/tree_node_ext/validators.rb', line 7

def validate_uniqueness_of_fields(fields, options = {})
  options = default_validator_options.merge(options)

  failures = Hash.new(Hash.new([]))

  nodes_of_depth(options[:within_subtrees_of_depth]).each do |subtree|
    visited_nodes = []
    nodes_to_visit =
      if options[:among_siblings_of_depth] == :not_siblings
        subtree.each_node
      else
        nodes_of_depth(options[:among_siblings_of_depth])
      end

    nodes_to_visit.each  do |node|
      visited_nodes.each do |vn|
        fields.each      do |field|
          next unless all_have?(field, [vn, node])

          if same_values?(field, [vn, node])
            k = vn.get(field) # repeated value

            prepare_hash(failures, field, k)

            add_failure_if_new(failures, field, k, options[:as_failure].call(vn))
            add_failure(       failures, field, k, options[:as_failure].call(node))

            return result(failures) if options[:first_failure_only]
          end
        end
      end
      visited_nodes << node
    end
  end

  result(failures)
end