Class: Underpass::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/underpass/filter.rb

Overview

Post-query filtering of Feature objects by tag properties.

Examples:

Filter restaurants by cuisine

filter = Underpass::Filter.new(features)
italian = filter.where(cuisine: 'italian')

Instance Method Summary collapse

Constructor Details

#initialize(features) ⇒ Filter

Creates a new filter for the given features.

Parameters:

  • features (Array<Feature>)

    the features to filter



13
14
15
# File 'lib/underpass/filter.rb', line 13

def initialize(features)
  @features = features
end

Instance Method Details

#reject(conditions = {}) ⇒ Array<Feature>

Returns features that do not match any of the given conditions.

Parameters:

  • conditions (Hash{Symbol => String}) (defaults to: {})

    tag conditions to reject

Returns:

  • (Array<Feature>)

    features not matching any condition



40
41
42
43
44
# File 'lib/underpass/filter.rb', line 40

def reject(conditions = {})
  @features.reject do |feature|
    conditions.any? { |key, value| feature.properties[key] == value.to_s }
  end
end

#where(conditions = {}) ⇒ Array<Feature>

Returns features whose properties match all given conditions.

Conditions can be exact values, regular expressions, or arrays of values.

Examples:

Exact match

filter.where(cuisine: 'italian')

Regex match

filter.where(name: /pizza/i)

Array inclusion

filter.where(cuisine: ['italian', 'mexican'])

Parameters:

  • conditions (Hash{Symbol => String, Regexp, Array}) (defaults to: {})

    tag conditions to match

Returns:

  • (Array<Feature>)

    features matching all conditions



30
31
32
33
34
# File 'lib/underpass/filter.rb', line 30

def where(conditions = {})
  @features.select do |feature|
    conditions.all? { |key, value| match_condition?(feature.properties[key], value) }
  end
end