Class: Stretchy::Builders::FilterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/stretchy/builders/filter_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilterBuilder

Returns a new instance of FilterBuilder.



7
8
9
10
11
12
# File 'lib/stretchy/builders/filter_builder.rb', line 7

def initialize
  @terms    = Hash.new { [] }
  @ranges   = Hash.new { [] }
  @geos     = Hash.new { [] }
  @exists   = []
end

Instance Attribute Details

#existsObject (readonly)

Returns the value of attribute exists.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def exists
  @exists
end

#geosObject (readonly)

Returns the value of attribute geos.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def geos
  @geos
end

#inverseObject (readonly)

Returns the value of attribute inverse.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def inverse
  @inverse
end

#rangesObject (readonly)

Returns the value of attribute ranges.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def ranges
  @ranges
end

#shouldObject (readonly)

Returns the value of attribute should.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def should
  @should
end

#termsObject (readonly)

Returns the value of attribute terms.



5
6
7
# File 'lib/stretchy/builders/filter_builder.rb', line 5

def terms
  @terms
end

Instance Method Details

#add_exists(fields) ⇒ Object



31
32
33
34
# File 'lib/stretchy/builders/filter_builder.rb', line 31

def add_exists(fields)
  @exists += clean_string_array(fields)
  @exists.uniq!
end

#add_geo(field, distance, geo_point) ⇒ Object



27
28
29
# File 'lib/stretchy/builders/filter_builder.rb', line 27

def add_geo(field, distance, geo_point)
  @geos[field] = Filters::GeoFilter.new(field, distance, geo_point)
end

#add_range(field, options) ⇒ Object



23
24
25
# File 'lib/stretchy/builders/filter_builder.rb', line 23

def add_range(field, options)
  @ranges[field] = Filters::RangeFilter.new(field, options)
end

#add_terms(field, terms) ⇒ Object



18
19
20
21
# File 'lib/stretchy/builders/filter_builder.rb', line 18

def add_terms(field, terms)
  @terms[field] += Array(terms)
  @terms[field].uniq!
end

#any?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/stretchy/builders/filter_builder.rb', line 14

def any?
  @terms.any? || @ranges.any? || @geos.any? || @exists.any?
end

#to_filtersObject



36
37
38
39
40
41
42
43
44
# File 'lib/stretchy/builders/filter_builder.rb', line 36

def to_filters
  filters = @ranges.values + @geos.values
  filters += @terms.map do |field, values|
    Filters::TermsFilter.new(field, values)
  end

  filters += @exists.map {|field| Filters::ExistsFilter.new(field) }
  filters
end