Class: ChefZero::Solr::Query::RegexpableQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_zero/solr/query/regexpable_query.rb

Direct Known Subclasses

Phrase, Term

Constant Summary collapse

DEFAULT_FIELD =
"text".freeze
WORD_CHARACTER =
"[A-Za-z0-9@._':\-]".freeze
NON_WORD_CHARACTER =
"[^A-Za-z0-9@._':\-]".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp_string, literal_string) ⇒ RegexpableQuery

Returns a new instance of RegexpableQuery.



5
6
7
8
9
10
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 5

def initialize(regexp_string, literal_string)
  @regexp_string = regexp_string
  # Surround the regexp with word boundaries
  @regexp = Regexp.new("(^|#{NON_WORD_CHARACTER})#{regexp_string}($|#{NON_WORD_CHARACTER})", true)
  @literal_string = literal_string
end

Instance Attribute Details

#literal_stringObject (readonly)

Returns the value of attribute literal_string.



12
13
14
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 12

def literal_string
  @literal_string
end

#regexpObject (readonly)

Returns the value of attribute regexp.



14
15
16
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 14

def regexp
  @regexp
end

#regexp_stringObject (readonly)

Returns the value of attribute regexp_string.



13
14
15
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 13

def regexp_string
  @regexp_string
end

Instance Method Details

#matches_doc?(doc) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 16

def matches_doc?(doc)
  matches_values?(doc[DEFAULT_FIELD])
end

#matches_values?(values) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/chef_zero/solr/query/regexpable_query.rb', line 20

def matches_values?(values)
  values.any? { |value| !@regexp.match(value).nil? }
end