Class: Rokaki::FilterModel::NestedLikeFilters

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_key_object:, prefix:, infix:, db:, mode: :and, or_key: :or, type: :like) ⇒ NestedLikeFilters

Returns a new instance of NestedLikeFilters.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 7

def initialize(filter_key_object:, prefix:, infix:, db:, mode: :and, or_key: :or, type: :like)
  @filter_key_object = filter_key_object
  @prefix = prefix
  @infix = infix
  @db = db
  @mode = mode
  @or_key = or_key
  @type = type

  @names = []
  @filter_methods = []
  @templates = []
  @filter_queries = []
  @method_names = []
  @filter_names = []
  @join_key_paths = []
  @key_paths = []
  @search_modes = []
  @modes = []
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def db
  @db
end

#filter_key_objectObject (readonly)

Returns the value of attribute filter_key_object.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def filter_key_object
  @filter_key_object
end

#filter_methodsObject

Returns the value of attribute filter_methods.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def filter_methods
  @filter_methods
end

#filter_namesObject

Returns the value of attribute filter_names.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def filter_names
  @filter_names
end

#filter_queriesObject (readonly)

Returns the value of attribute filter_queries.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def filter_queries
  @filter_queries
end

#i_like_semanticsObject (readonly)

Returns the value of attribute i_like_semantics.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def i_like_semantics
  @i_like_semantics
end

#infixObject (readonly)

Returns the value of attribute infix.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def infix
  @infix
end

#join_key_pathsObject

Returns the value of attribute join_key_paths.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def join_key_paths
  @join_key_paths
end

#key_pathsObject

Returns the value of attribute key_paths.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def key_paths
  @key_paths
end

#like_semanticsObject (readonly)

Returns the value of attribute like_semantics.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def like_semantics
  @like_semantics
end

#method_namesObject

Returns the value of attribute method_names.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def method_names
  @method_names
end

#modeObject (readonly)

Returns the value of attribute mode.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def mode
  @mode
end

#modesObject

Returns the value of attribute modes.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def modes
  @modes
end

#namesObject

Returns the value of attribute names.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def names
  @names
end

#or_keyObject (readonly)

Returns the value of attribute or_key.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def or_key
  @or_key
end

#prefixObject (readonly)

Returns the value of attribute prefix.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def prefix
  @prefix
end

#search_modesObject

Returns the value of attribute search_modes.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def search_modes
  @search_modes
end

#templatesObject

Returns the value of attribute templates.



29
30
31
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 29

def templates
  @templates
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 27

def type
  @type
end

Instance Method Details

#build_filters_dataObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 36

def build_filters_data
  results = filter_key_object.key_paths.each do |key_path|
    if key_path.is_a?(Symbol)
      build_filter_data(key_path)
    else
      if key_path.include? or_key
        build_filter_data(key_path.dup, mode: or_key)
      else
        build_filter_data(key_path.dup)
      end
    end
  end
end

#callObject



31
32
33
34
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 31

def call
  build_filters_data
  compound_filters
end

#compound_filtersObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rokaki/filter_model/nested_like_filters.rb', line 50

def compound_filters
  # key_paths represents a structure like
  # [
  #   [ # this is an or
  #     [:articles, :title],
  #     [:articles, :authors, :first_name],
  #     [:articles, :authors, :reviews, :title],
  #     [:articles, :authors, :reviews, :content]
  #   ],
  #   [:articles, :content] # this is an and
  # ]
  #
  # Each item in the array represents a compounded filter
  #
  key_paths.each_with_index do |key_path_item, index|
    base_names = get_name(index)
    join_map = JoinMap.new(join_key_paths[index])
    join_map.call

    if key_path_item.first.is_a?(Array)
      item_search_modes = search_modes[index]

      base_name = base_names.shift
      method_name = prefix.to_s + ([:filter].push base_name).compact.join(infix.to_s)
      method_name += (infix.to_s+'or'+infix.to_s) + (base_names).join(infix.to_s+'or'+infix.to_s)
      item_filter_names = [prefix.to_s + base_name]

      base_names.each do |filter_base_name|
        item_filter_names << (prefix.to_s + filter_base_name)
      end

      base_modes = modes[index]
      key_path_item.each_with_index do |key_path, kp_index|

        build_query(keys: key_path.dup, join_map: join_map.result, mode: base_modes[kp_index], filter_name: item_filter_names[kp_index], search_mode: item_search_modes[kp_index])
      end

      item_filter_queries = filter_queries[index]
      first_query = item_filter_queries.shift

      ored = item_filter_queries.map do |query|
        ".or(#{query})"
      end

      filter_conditions = item_filter_names.join(' || ')

      @filter_methods << "def #{method_name}; #{first_query + ored.join}; end;"
      @templates << "@model = #{method_name} if #{filter_conditions};"
    else

      base_name = get_name(index)
      filter_name = "#{prefix}#{get_filter_name(index)}"

      method_name = ([prefix, :filter, base_name]).compact.join(infix.to_s)

      build_query(keys: key_path_item.dup, join_map: join_map.result, filter_name: filter_name, search_mode: search_modes[index])

      @filter_methods << "def #{method_name}; #{filter_queries[index]}; end;"
      @templates << "@model = #{method_name} if #{filter_name};"
    end
  end
end