Module: DeepCloneableChecked::DeepCloneChecked

Defined in:
lib/deep_cloneable_checked/deep_clone_checked.rb

Instance Method Summary collapse

Instance Method Details

#deep_clone_checked(options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/deep_cloneable_checked/deep_clone_checked.rb', line 5

def deep_clone_checked(options)
  missing = validate_clone_check(options)
  
  unless missing.empty?
    raise MissingAssociationError, "Not all associations cloned: #{missing.inspect}"
  end
  
  deep_clone(options)
end

#validate_clone_check(options) ⇒ Object



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
# File 'lib/deep_cloneable_checked/deep_clone_checked.rb', line 15

def validate_clone_check(options)
  checked = Set[]

  check_missing = lambda do |klass, includes, excludes|
    includes = DeepCloneableChecked.options_to_hash(includes)
    excludes = DeepCloneableChecked.options_to_hash(excludes)
    # puts "-----------\n klass: #{klass.name}, includes: #{includes.inspect}, excludes: #{excludes.inspect}"

    # If we have already checked this class then we assume we don't
    # clone it in a different manner. This assumption might not be valid
    # but assuming otherwise would be complex and YAGNI (hopefully).
    return nil if checked.include? klass

    checked << klass
    associations = klass.reflections

    associations.map do |association_name, reflection|
      next nil if reflection.is_a? ActiveRecord::Reflection::ThroughReflection
      next_excludes = excludes[association_name.to_sym]
      next_includes = includes[association_name.to_sym]
  
      if next_includes
        # puts "inclusion: #{inclusion.inspect}, association_name: #{association_name}"
        result = check_missing[reflection.klass, next_includes, next_excludes]
        if !result.blank?
          # puts "missing from #{reflection.klass.name}, #{association_name}"
          Hash[association_name.to_sym, result]
        end
      else
        # puts "No inclusion for #{association_name}, inclusion: #{inclusion.inspect}, to_exclude: #{to_exclude.inspect}"
        if next_excludes
          nil
        else
          # puts "No exclusion either"
          association_name.to_sym
        end
      end
    end.compact
  end
  
  check_missing[self.class, options[:include] || [], options[:exclude] || []]
end