Module: ArelExtensions::StringFunctions

Includes:
Warning
Included in:
Arel::Nodes::Equality, Arel::Nodes::Function, Arel::Nodes::Grouping, Attributes, Nodes::Case
Defined in:
lib/arel_extensions/string_functions.rb

Instance Method Summary collapse

Methods included from Warning

#deprecated

Instance Method Details

#&(other) ⇒ Object

*FindInSet function .……



27
28
29
30
31
32
# File 'lib/arel_extensions/string_functions.rb', line 27

def &(other)
  ArelExtensions::Nodes::FindInSet.new [
    Arel.quoted(other.is_a?(Integer) ? other.to_s : other),
    self,
  ]
end

#[](start, end_ = nil) ⇒ Object

Note:

ind should be an [Integer|NilClass] if start is an [Integer]. It’s ignored in all other cases.

Return a [ArelExtensions::Nodes::Substring] if start is a [Range] or an [Integer].

Return the result to ‘self.send(start)` if it’s a [String|Symbol]. The assumption is that you’re trying to reach an [Arel::Table]‘s [Arel::Attribute].



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arel_extensions/string_functions.rb', line 72

def [](start, end_ = nil)
  if start.is_a?(String) || start.is_a?(Symbol)
    self.send(start)
  elsif start.is_a?(Range)
    ArelExtensions::Nodes::Substring.new [self, start.begin + 1, start.end - start.begin + 1]
  elsif start.is_a?(Integer) && !end_
    ArelExtensions::Nodes::Substring.new [self, start + 1, 1]
  elsif start.is_a?(Integer)
    start += 1
    ArelExtensions::Nodes::Substring.new [self, start, end_ - start + 1]
  else
    raise ArgumentError, 'unrecognized argument types; can accept integers, ranges, or strings.'
  end
end

#ai_collateObject



141
142
143
# File 'lib/arel_extensions/string_functions.rb', line 141

def ai_collate
  ArelExtensions::Nodes::Collate.new(self, nil, true, false)
end

#ai_imatches(other) ⇒ Object

accent insensitive & case insensitive



133
134
135
# File 'lib/arel_extensions/string_functions.rb', line 133

def ai_imatches other # accent insensitive & case insensitive
  ArelExtensions::Nodes::AiIMatches.new(self, other)
end

#ai_matches(other) ⇒ Object

accent insensitive & case sensitive



129
130
131
# File 'lib/arel_extensions/string_functions.rb', line 129

def ai_matches other # accent insensitive & case sensitive
  ArelExtensions::Nodes::AiMatches.new(self, other)
end

#blankObject



206
207
208
# File 'lib/arel_extensions/string_functions.rb', line 206

def blank
  ArelExtensions::Nodes::Blank.new [self]
end

#byte_lengthObject



40
41
42
43
# File 'lib/arel_extensions/string_functions.rb', line 40

def byte_length
  deprecated "Use `byte_size` instead. `byte_length` relies on the vendor's `LEN/LENGTH` implementation and it's not portable"
  ArelExtensions::Nodes::Length.new self, true
end

#byte_sizeObject



45
46
47
# File 'lib/arel_extensions/string_functions.rb', line 45

def byte_size
  ArelExtensions::Nodes::ByteSize.new self
end

#char_lengthObject



49
50
51
# File 'lib/arel_extensions/string_functions.rb', line 49

def char_length
  ArelExtensions::Nodes::CharLength.new self
end

#ci_collateObject



145
146
147
# File 'lib/arel_extensions/string_functions.rb', line 145

def ci_collate
  ArelExtensions::Nodes::Collate.new(self, nil, false, true)
end

#collate(ai = false, ci = false, option = nil) ⇒ Object



149
150
151
# File 'lib/arel_extensions/string_functions.rb', line 149

def collate ai = false, ci = false, option = nil
  ArelExtensions::Nodes::Collate.new(self, option, ai, ci)
end

#concat(other) ⇒ Object



166
167
168
# File 'lib/arel_extensions/string_functions.rb', line 166

def concat other
  ArelExtensions::Nodes::Concat.new [self, other]
end

#downcaseObject



198
199
200
# File 'lib/arel_extensions/string_functions.rb', line 198

def downcase
  ArelExtensions::Nodes::Downcase.new [self]
end

#edit_distance(other) ⇒ Object



223
224
225
# File 'lib/arel_extensions/string_functions.rb', line 223

def edit_distance other
  ArelExtensions::Nodes::LevenshteinDistance.new [self, other]
end

#group_concat(sep = nil, *orders, group: nil, order: nil) ⇒ Object

concat elements of a group, separated by sep and ordered by a list of Ascending or Descending



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/arel_extensions/string_functions.rb', line 171

def group_concat(sep = nil, *orders, group: nil, order: nil)
  if orders.present?
    deprecated 'Use the kwarg `order` instead.', what: 'orders'
  end
  order_tabs = [orders].flatten.map{ |o|
    if o.is_a?(Arel::Nodes::Ascending) || o.is_a?(Arel::Nodes::Descending)
      o
    elsif o.respond_to?(:asc)
      o.asc
    end
  }.compact
  ArelExtensions::Nodes::GroupConcat.new(self, sep, group: group, order: (order || order_tabs))
end

#idoes_not_match(others, escape = nil) ⇒ Object



117
118
119
# File 'lib/arel_extensions/string_functions.rb', line 117

def idoes_not_match others, escape = nil
  ArelExtensions::Nodes::IDoesNotMatch.new self, others, escape
end

#idoes_not_match_all(others, escape = nil) ⇒ Object



125
126
127
# File 'lib/arel_extensions/string_functions.rb', line 125

def idoes_not_match_all others, escape = nil
  grouping_all :idoes_not_match, others, escape
end

#idoes_not_match_any(others, escape = nil) ⇒ Object



121
122
123
# File 'lib/arel_extensions/string_functions.rb', line 121

def idoes_not_match_any others, escape = nil
  grouping_any :idoes_not_match, others, escape
end

#imatches(others, escape = nil) ⇒ Object



92
93
94
# File 'lib/arel_extensions/string_functions.rb', line 92

def imatches others, escape = nil
  ArelExtensions::Nodes::IMatches.new self, others, escape
end

#imatches_all(others, escape = nil) ⇒ Object

def grouping_any method, others, *extra

  puts "*******************"
  puts method
  puts others.inspect
  puts extra.inspect
  puts "-------------------"
  res = super(method,others,*extra)
  puts res.to_sql
  puts res.inspect
  puts "*******************"
  res
end


113
114
115
# File 'lib/arel_extensions/string_functions.rb', line 113

def imatches_all others, escape = nil
  grouping_all :imatches, others, escape, escape
end

#imatches_any(others, escape = nil) ⇒ Object



96
97
98
# File 'lib/arel_extensions/string_functions.rb', line 96

def imatches_any others, escape = nil
  grouping_any :imatches, others, escape
end

#lengthObject

LENGTH function returns the length (bytewise) of the value in a text field.



35
36
37
38
# File 'lib/arel_extensions/string_functions.rb', line 35

def length
  deprecated "Use `byte_size` or `char_length` instead. `length` relies on the vendor's `LEN/LENGTH` implementation and it's not portable"
  ArelExtensions::Nodes::Length.new self, true
end

#levenshtein_distance(other) ⇒ Object



219
220
221
# File 'lib/arel_extensions/string_functions.rb', line 219

def levenshtein_distance other
  ArelExtensions::Nodes::LevenshteinDistance.new [self, other]
end

#locate(val) ⇒ Object

LOCATE function returns the first starting position of a string in another string. If string1 or string2 is NULL then it returns NULL. If string1 not found in string2 then it returns 0.



55
56
57
# File 'lib/arel_extensions/string_functions.rb', line 55

def locate val
  ArelExtensions::Nodes::Locate.new [self, val]
end

#ltrim(other = ' ') ⇒ Object



190
191
192
# File 'lib/arel_extensions/string_functions.rb', line 190

def ltrim other = ' '
  ArelExtensions::Nodes::Ltrim.new [self, other]
end

#md5Object



227
228
229
# File 'lib/arel_extensions/string_functions.rb', line 227

def md5
  ArelExtensions::Nodes::MD5.new [self]
end

#not_blankObject Also known as: present



210
211
212
# File 'lib/arel_extensions/string_functions.rb', line 210

def not_blank
  ArelExtensions::Nodes::NotBlank.new [self]
end

#regexp_replace(pattern, substitute) ⇒ Object



162
163
164
# File 'lib/arel_extensions/string_functions.rb', line 162

def regexp_replace pattern, substitute
  ArelExtensions::Nodes::RegexpReplace.new self, pattern, substitute
end

#repeat(other = 1) ⇒ Object



215
216
217
# File 'lib/arel_extensions/string_functions.rb', line 215

def repeat other = 1
  ArelExtensions::Nodes::Repeat.new [self, other]
end

#replace(pattern, substitute) ⇒ Object

REPLACE function replaces a sequence of characters in a string with another set of characters, not case-sensitive.



154
155
156
157
158
159
160
# File 'lib/arel_extensions/string_functions.rb', line 154

def replace pattern, substitute
  if pattern.is_a? Regexp
    ArelExtensions::Nodes::RegexpReplace.new self, pattern, substitute
  else
    ArelExtensions::Nodes::Replace.new self, pattern, substitute
  end
end

#rtrim(other = ' ') ⇒ Object



194
195
196
# File 'lib/arel_extensions/string_functions.rb', line 194

def rtrim other = ' '
  ArelExtensions::Nodes::Rtrim.new [self, other]
end

#smatches(other) ⇒ Object

accent sensitive & case sensitive



137
138
139
# File 'lib/arel_extensions/string_functions.rb', line 137

def smatches other # accent sensitive & case sensitive
  ArelExtensions::Nodes::SMatches.new(self, other)
end

#soundexObject

SOUNDEX function returns a character string containing the phonetic representation of char.



88
89
90
# File 'lib/arel_extensions/string_functions.rb', line 88

def soundex
  ArelExtensions::Nodes::Soundex.new [self]
end

#substring(start, len = nil) ⇒ Object



59
60
61
# File 'lib/arel_extensions/string_functions.rb', line 59

def substring start, len = nil
  ArelExtensions::Nodes::Substring.new [self, start, len]
end

#trim(other = ' ') ⇒ Object

Function returns a string after removing left, right or the both prefixes or suffixes int argument



186
187
188
# File 'lib/arel_extensions/string_functions.rb', line 186

def trim other = ' '
  ArelExtensions::Nodes::Trim.new [self, other]
end

#upcaseObject



202
203
204
# File 'lib/arel_extensions/string_functions.rb', line 202

def upcase
  ArelExtensions::Nodes::Upcase.new [self]
end