Class: Rokaki::FilterModel::BasicFilter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys:, prefix:, infix:, like_semantics:, i_like_semantics:, db:) ⇒ BasicFilter

Returns a new instance of BasicFilter.



6
7
8
9
10
11
12
13
14
# File 'lib/rokaki/filter_model/basic_filter.rb', line 6

def initialize(keys:, prefix:, infix:, like_semantics:, i_like_semantics:, db:)
  @keys = keys
  @prefix = prefix
  @infix = infix
  @like_semantics = like_semantics
  @i_like_semantics = i_like_semantics
  @db = db
  @filter_query = nil
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#filter_methodObject

Returns the value of attribute filter_method.



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

def filter_method
  @filter_method
end

#filter_queryObject (readonly)

Returns the value of attribute filter_query.



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

def filter_query
  @filter_query
end

#filter_templateObject

Returns the value of attribute filter_template.



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

def filter_template
  @filter_template
end

#i_like_semanticsObject (readonly)

Returns the value of attribute i_like_semantics.



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

def i_like_semantics
  @i_like_semantics
end

#infixObject (readonly)

Returns the value of attribute infix.



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

def infix
  @infix
end

#keysObject (readonly)

Returns the value of attribute keys.



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

def keys
  @keys
end

#like_semanticsObject (readonly)

Returns the value of attribute like_semantics.



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

def like_semantics
  @like_semantics
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#_chain_filter_type(key) ⇒ Object



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

def _chain_filter_type(key)
  filter = "#{prefix}#{key}"
  query  = ''

  if like_semantics && mode = like_semantics[key]
    query = build_like_query(
      type: 'LIKE',
      query: query,
      filter: filter,
      mode: mode,
      key: key
    )
  elsif i_like_semantics && mode = i_like_semantics[key]
    query = build_like_query(
      type: 'ILIKE',
      query: query,
      filter: filter,
      mode: mode,
      key: key
    )
  else
    query = "@model.where(#{key}: #{filter})"
  end

  @filter_query = query
end

#build_like_query(type:, query:, filter:, mode:, key:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rokaki/filter_model/basic_filter.rb', line 63

def build_like_query(type:, query:, filter:, mode:, key:)
  if db == :postgres
    query = "@model.where(\"#{key} #{type} ANY (ARRAY[?])\", "
    query += "prepare_terms(#{filter}, :#{mode}))"
  else
    query = "@model.where(\"#{key} #{type} :query\", "
    query += "query: \"%\#{#{filter}}%\")" if mode == :circumfix
    query += "query: \"%\#{#{filter}}\")" if mode == :prefix
    query += "query: \"\#{#{filter}}%\")" if mode == :suffix
  end

  query
end

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rokaki/filter_model/basic_filter.rb', line 18

def call
  first_key = keys.shift
  filter = "#{prefix}#{first_key}"
  name = first_key

  keys.each do |key|
    filter += "#{infix}#{key}"
    name += "#{infix}#{key}"
  end

  @filter_method = "def #{prefix}filter_#{name};" \
    "#{_chain_filter_type(name)} end;"

  # class_eval filter_method, __FILE__, __LINE__ - 2

  @filter_template = "@model = #{prefix}filter_#{name} if #{filter};"
end