Sha256: b0e419fd56a54a4e9d34840c6547776f0ee63a73376ecd82aa95d9af95185196

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

module ActiveRecordSeek
  module Operators
    class CiRegexpBaseOperator < BaseOperator

    end

    {
      ci_regexp:     :matches_regexp,
      ci_regexp_all: :matches_regexp,
      ci_regexp_any: :matches_regexp,
      not_ci_regexp:     :does_not_match_regexp,
      not_ci_regexp_all: :does_not_match_regexp,
      not_ci_regexp_any: :does_not_match_regexp,
    }.each do |seek_operator, arel_operator|
      operator_class = Class.new(RegexpBaseOperator) do
        define_method(:arel_operation) do
          operation = nil
          # must manually define _all and _any operations because arel only has matches_regexp
          if seek_operator =~ /_all\z/
            arel_value.each do |and_arel_value|
              and_operation = arel_column.send(arel_operator, and_arel_value)
              operation = operation ? operation.and(and_operation) : and_operation
            end
          elsif seek_operator =~ /_any\z/
            arel_value.each do |or_arel_value|
              or_operation = arel_column.send(arel_operator, or_arel_value)
              operation = operation ? operation.or(or_operation) : or_operation
            end
          else
            operation = arel_column.send(arel_operator, arel_value)
          end
          # switch to ~ operator
          operation.each do |operation_part|
            if operation_part.class.in?([Arel::Nodes::Regexp, Arel::Nodes::NotRegexp])
              operation_part.case_sensitive = false
            end
          end
          operation
        end
      end

      const_set("#{seek_operator.to_s.camelcase}Operator", operator_class)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record_seek-0.0.5 lib/active_record_seek/operators/ci_regexp_base_operator.rb
active_record_seek-0.0.4 lib/active_record_seek/operators/ci_regexp_base_operator.rb
active_record_seek-0.0.3 lib/active_record_seek/operators/ci_regexp_base_operator.rb
active_record_seek-0.0.2 lib/active_record_seek/operators/ci_regexp_base_operator.rb
active_record_seek-0.0.1 lib/active_record_seek/operators/ci_regexp_base_operator.rb