Class: Rokaki::FilterModel::JoinMap

Inherits:
Object
  • Object
show all
Defined in:
lib/rokaki/filter_model/join_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_paths) ⇒ JoinMap

Returns a new instance of JoinMap.



15
16
17
18
# File 'lib/rokaki/filter_model/join_map.rb', line 15

def initialize(key_paths)
  @key_paths = key_paths
  @result = {}
end

Instance Attribute Details

#key_pathsObject (readonly)

Returns the value of attribute key_paths.



20
21
22
# File 'lib/rokaki/filter_model/join_map.rb', line 20

def key_paths
  @key_paths
end

#resultObject

Returns the value of attribute result.



21
22
23
# File 'lib/rokaki/filter_model/join_map.rb', line 21

def result
  @result
end

Instance Method Details

#callObject



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
# File 'lib/rokaki/filter_model/join_map.rb', line 23

def call
  key_paths.uniq.each do |key_path|
    current_key_path = []
    previous_key = nil

    if Symbol === key_path
      if key_paths.length == 1
        @result = key_paths
      else
        result[key_path] = {} unless result.keys.include? key_path
      end
    end

    if Array === key_path
      key_path.each do |key|
        current_path_length = current_key_path.length

        if current_path_length > 0 && result.dig(current_key_path).nil?

          if current_path_length == 1
            parent_result = result[previous_key]

            if Symbol === parent_result && parent_result != key
              result[previous_key] = [parent_result, key]
            elsif Array === parent_result

              parent_result.each_with_index do |array_item, index|
                if array_item == key
                  current_key_path << index
                end
              end

            else
              result[previous_key] = key unless result[previous_key] == key
            end

          else
            previous_key_path = current_key_path - [previous_key]
            previous_path_length = previous_key_path.length
            p current_key_path

            if previous_path_length == 1
              res = result.dig(*previous_key_path)

              if Symbol === res
                result[previous_key_path.first] = { previous_key => key }
              end
            elsif previous_path_length > 1
              res = result.dig(*previous_key_path)

              if Symbol === res
                base = previous_key_path.pop
                result.dig(*previous_key_path)[base] = { previous_key => key }
              end
            end

          end
        else
        end

        previous_key = key
        current_key_path << key
      end
    end
  end
  result
end