Sha256: becb8cf8bf576b04c5cc57976574c9a9d9767962e33826c2b0c5bc48466f6b0d

Contents?: true

Size: 927 Bytes

Versions: 2

Compression:

Stored size: 927 Bytes

Contents

# frozen_string_literal: true

module Pursuit
  # Transform for a list of terms.
  #
  class TermTransform < Parslet::Transform
    # String Types

    rule(string_double_quotes: [])             { '' }
    rule(string_double_quotes: simple(:value)) { value.to_s.gsub(/\\(.)/, '\1') }

    rule(string_single_quotes: [])             { '' }
    rule(string_single_quotes: simple(:value)) { value.to_s.gsub(/\\(.)/, '\1') }

    rule(string_no_quotes: simple(:value)) { value.to_s }

    # Terms

    rule(term: simple(:term)) do |context|
      value = ActiveRecord::Base.sanitize_sql_like(context[:term])
      value = "%#{value}%"

      context[:attributes].inject(nil) do |previous_node, attribute|
        node = attribute.matches(value)
        next node unless previous_node

        previous_node.or(node)
      end
    end

    # Joins

    rule(left: simple(:left), right: simple(:right)) { left.and(right) }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pursuit-1.1.0 lib/pursuit/term_transform.rb
pursuit-1.0.1 lib/pursuit/term_transform.rb