Class: FamilyReunion::TopNode

Inherits:
Object
  • Object
show all
Defined in:
lib/family-reunion/top_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TopNode

Returns a new instance of TopNode.



5
6
7
8
9
10
11
12
# File 'lib/family-reunion/top_node.rb', line 5

def initialize(data)
  @data = data
  @valid_names_hash = nil
  @valid_names_duplicates = nil
  @synonyms_hash = nil
  @ids_hash = nil
  @paths_hash = nil
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/family-reunion/top_node.rb', line 3

def data
  @data
end

Instance Method Details

#ids_hashObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/family-reunion/top_node.rb', line 48

def ids_hash
  return @ids_hash if @ids_hash
  @ids_hash = valid_names_hash.inject({}) do |res, key_val|
    res[key_val[1][:id].to_s.to_sym] = key_val[1]
    res
  end
  data[:empty_nodes].each do |node|
    @ids_hash[node[:id].to_s.to_sym] = node
  end
  @ids_hash
end

#paths_hashObject



41
42
43
44
45
46
# File 'lib/family-reunion/top_node.rb', line 41

def paths_hash
  unless @paths_hash
    valid_names_hash
  end
  @paths_hash
end

#populate_paths_hash(path, path_ids) ⇒ Object



87
88
89
90
91
92
# File 'lib/family-reunion/top_node.rb', line 87

def populate_paths_hash(path, path_ids)
  name = path[-1]
  unless @paths_hash[name]
    @paths_hash[name] = [path.dup, path_ids.dup]
  end
end

#root_pathsObject



94
95
96
97
98
99
100
# File 'lib/family-reunion/top_node.rb', line 94

def root_paths
  unless @root_name && @root_id
    @root_id = data[:leaves][0][:path_ids][0].to_s.to_sym
    @root_name = data[:leaves][0][:path][0]
  end
  [[@root_name], [@root_id]]
end

#synonyms_hashObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/family-reunion/top_node.rb', line 60

def synonyms_hash
  return @synonyms_hash if @synonyms_hash
  @synonyms_hash = {}
  @valid_names_hash.keys.each do |name|
    synonyms = @valid_names_hash[name][:synonyms]
    synonyms.each do |syn|
      @synonyms_hash.has_key?(syn[:canonical_name]) ? @synonyms_hash[syn[:canonical_name]] << @valid_names_hash[name] : @synonyms_hash[syn[:canonical_name]] = [@valid_names_hash[name]]
    end
  end
  @synonyms_hash
end

#update_paths_hash(node) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/family-reunion/top_node.rb', line 77

def update_paths_hash(node)
  path = node[:path].map { |n| n.to_sym }
  path_ids = node[:path_ids].map { |i| i.to_s.to_sym }
  until path.empty?
    populate_paths_hash(path, path_ids)
    path.pop
    path_ids.pop
  end
end

#valid_names_duplicatesObject



72
73
74
75
# File 'lib/family-reunion/top_node.rb', line 72

def valid_names_duplicates
  valid_names_hash unless @valid_names_duplicates
  @valid_names_duplicates
end

#valid_names_hashObject



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
# File 'lib/family-reunion/top_node.rb', line 14

def valid_names_hash
  return @valid_names_hash if @valid_names_hash #TODO: make it more robust for situations with exceptions etc.
  @valid_names_duplicates = {}
  @valid_names_hash = {}
  @paths_hash = {}
  @data[:leaves].each do |row|
    canonical = row[:valid_name][:canonical_name]
    update_paths_hash(row)
    if @valid_names_hash.has_key?(canonical)
      if @valid_names_duplicates.has_key?(canonical)
        @valid_names_duplicates[canonical] << row
      else
        @valid_names_duplicates[canonical] = [row]
      end
    else
      @valid_names_hash[canonical] = row
    end
  end
  @valid_names_duplicates.keys.each do |k|
    @valid_names_duplicates[k] << @valid_names_hash.delete(k)
  end
  data[:empty_nodes].each do |row|
    update_paths_hash(row)
  end
  @valid_names_hash
end