Class: TypeScopes::String

Inherits:
TypeScopes show all
Defined in:
lib/type_scopes/string.rb

Constant Summary

Constants inherited from TypeScopes

VERSION

Class Method Summary collapse

Methods inherited from TypeScopes

append_scope, inject, support?

Class Method Details

.escape(string) ⇒ Object



6
7
8
9
10
# File 'lib/type_scopes/string.rb', line 6

def self.escape(string)
  string = string.gsub("%".freeze, "[%]".freeze)
  string.gsub!("_".freeze, "[_]".freeze)
  string
end

.inject_for_column(model, name) ⇒ Object



12
13
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
40
41
42
43
44
45
46
47
48
49
# File 'lib/type_scopes/string.rb', line 12

def self.inject_for_column(model, name)
  column = model.arel_table[name]
  append_scope(model, :"#{name}_like", lambda { |str, sensitive: true| where(column.matches(str, nil, sensitive)) })
  append_scope(model, :"#{name}_not_like", lambda { |str, sensitive: true| where(column.does_not_match(str, nil, sensitive)) })
  append_scope(model, :"#{name}_ilike", lambda { |str| where(column.matches(str)) })
  append_scope(model, :"#{name}_not_ilike", lambda { |str| where(column.does_not_match(str)) })

  append_scope(model, :"#{name}_contains", lambda { |str, sensitive: true|
    send("#{name}_like", "%#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_contain", lambda { |str, sensitive: true|
    send("#{name}_not_like", "%#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_contain", lambda { |str, sensitive: true|
    send("#{name}_like", "%#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_starts_with", lambda { |str, sensitive: true|
    send("#{name}_like", "#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_start_with", lambda { |str, sensitive: true|
    send("#{name}_not_like", "#{TypeScopes::String.escape(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_ends_with", lambda { |str, sensitive: true|
    send("#{name}_like", "%#{TypeScopes::String.escape(str)}", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_end_with", lambda { |str, sensitive: true|
    send("#{name}_not_like", "%#{TypeScopes::String.escape(str)}", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_matches", lambda { |str, sensitive: true| where(column.matches_regexp(str, sensitive)) })
  append_scope(model, :"#{name}_does_not_match", lambda { |str, sensitive: true| where(column.does_not_match_regexp(str, sensitive)) })
end

.typesObject



2
3
4
# File 'lib/type_scopes/string.rb', line 2

def self.types
  ["character", "text", "varchar"].freeze
end