Class: Babik::Selection::Operation::Regex
- Defined in:
- lib/babik/queryset/lib/selection/operation/regex.rb
Overview
Match by case sensitive regex
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
#field, #sql_operation, #sql_operation_template, #value
Instance Method Summary collapse
- #_mysql2_convert_regex(value) ⇒ Object
-
#initialize(field, value) ⇒ Regex
constructor
A new instance of Regex.
- #operator ⇒ Object
Methods inherited from Base
#_init_sql_operation, date_special_cases, #db_engine, escape, factory, initialize_operation, initialize_operators, operator?, special_cases, #to_s
Constructor Details
#initialize(field, value) ⇒ Regex
Returns a new instance of Regex.
12 13 14 15 16 17 |
# File 'lib/babik/queryset/lib/selection/operation/regex.rb', line 12 def initialize(field, value) value = value.inspect[1..-2] if value.class == Regexp value = value[1..-2] if value.class == String && value[0] == '/' && value[-1] == '/' value = _mysql2_convert_regex(value) if db_engine == 'mysql2' super(field, "?field #{operator} ?value", value) end |
Instance Method Details
#_mysql2_convert_regex(value) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/babik/queryset/lib/selection/operation/regex.rb', line 27 def _mysql2_convert_regex(value) replacements = { '\\d' => '[0-9]', '\\w' => '[a-zA-Z]' } replacements.each do |pcre_pattern, mysql_pattern| value = value.gsub(pcre_pattern, mysql_pattern) end value end |
#operator ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/babik/queryset/lib/selection/operation/regex.rb', line 19 def operator dbms_adapter = db_engine return 'REGEXP' if dbms_adapter == 'mysql2' return '~' if dbms_adapter == 'postgresql' return 'REGEXP' if dbms_adapter == 'sqlite3' raise NotImplementedError, "Invalid dbms #{dbms_adapter}. Only mysql, postgresql, and sqlite3 are accepted" end |