Class: ChefZero::Solr::Query::RangeQuery

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

Constant Summary collapse

DEFAULT_FIELD =
"text".freeze

Instance Method Summary collapse

Constructor Details

#initialize(from, to, from_inclusive, to_inclusive) ⇒ RangeQuery

Returns a new instance of RangeQuery.



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

def initialize(from, to, from_inclusive, to_inclusive)
  @from = from
  @to = to
  @from_inclusive = from_inclusive
  @to_inclusive = to_inclusive
end

Instance Method Details

#matches_doc?(doc) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/chef_zero/solr/query/range_query.rb', line 38

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

#matches_values?(values) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chef_zero/solr/query/range_query.rb', line 16

def matches_values?(values)
  values.any? do |value|
    unless @from == "*"
      case @from <=> value
      when -1
        return false
      when 0
        return false unless @from_inclusive
      end
    end
    unless @to == "*"
      case value <=> @to
      when 1
        return false
      when 0
        return false unless @to_inclusive
      end
    end
    return true
  end
end

#to_sObject



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

def to_s
  "#{@from_inclusive ? "[" : "{"}#{@from} TO #{@to}#{@to_inclusive ? "]" : "}"}"
end