Class: MoveToGo::Organizations

Inherits:
Hash
  • Object
show all
Defined in:
lib/move-to-go/model/organizations.rb

Defined Under Namespace

Classes: DuplicateSet, DuplicateSetArray

Instance Method Summary collapse

Constructor Details

#initialize(rootmodel) ⇒ Organizations

Returns a new instance of Organizations.



4
5
6
# File 'lib/move-to-go/model/organizations.rb', line 4

def initialize(rootmodel)
    @rootmodel = rootmodel
end

Instance Method Details

#find_duplicates_by(*raw_fields_to_check) ⇒ Object

Finds duplicates based on supplied fields. Returns an DuplicateSetArray



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
# File 'lib/move-to-go/model/organizations.rb', line 42

def find_duplicates_by(*raw_fields_to_check)
    # map fields to instance variable name or to class. For example :name
    # or "visiting_address.city" => [:visiting_address, :city]
    fields_to_check = raw_fields_to_check.map{ |field|
        fields = field.to_s.split(".")
        case fields.length
            when 1 then :"@#{field}"
            when 2 then [:"@#{fields[0]}",:"@#{fields[1]}"]
            else raise
        end
    } 
    # Find all possible duplicates and collect them to sets.
    possible_duplicate_sets = self
    .values
    .group_by{ |org|
        fields_to_check.map{ |field|
        case field # Some fields (Address) are actually class objects, check what we are dealing with
            when Symbol then val = org.instance_variable_get(field)
            when Array then val = org.instance_variable_get(field[0]).instance_variable_get(field[1])
        end
        val != nil ? val.to_s.downcase.strip : val = ''
        }
    }
    .select { |k, v| v.size > 1 }
    .values
        
    return DuplicateSetArray.new(@rootmodel, possible_duplicate_sets)
end