Class: ChewyQuery::Builder::Nodes::Regexp

Inherits:
Expr
  • Object
show all
Defined in:
lib/chewy_query/builder/nodes/regexp.rb

Constant Summary collapse

FLAGS =
%w(all anystring automaton complement empty intersection interval none)

Instance Method Summary collapse

Methods inherited from Expr

#!, #&, #|, #~

Methods inherited from Base

#eql?, #render

Constructor Details

#initialize(name, regexp, *args) ⇒ Regexp

Returns a new instance of Regexp.



7
8
9
10
11
12
13
14
15
# File 'lib/chewy_query/builder/nodes/regexp.rb', line 7

def initialize(name, regexp, *args)
  @name = name.to_s
  @regexp = regexp.respond_to?(:source) ? regexp.source : regexp.to_s
  @options = args.extract_options!

  if args.any? || @options[:flags].present?
    @options[:flags] = FLAGS & (args.any? ? args.flatten : @options[:flags]).map(&:to_s).map(&:downcase)
  end
end

Instance Method Details

#__render__Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chewy_query/builder/nodes/regexp.rb', line 17

def __render__
  body = @options[:flags] ?
    { value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|') } : @regexp

  filter = { @name => body }

  if @options.key?(:cache)
    filter[:_cache] = !!@options[:cache]
    filter[:_cache_key] = @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass) ?
      @regexp.underscore : @options[:cache]
  end
  { regexp: filter }
end