class Starline::Entities::DistanceFilter

Constants

FILTERS

Supported filter names

OPERATORS

Mapping between filter names and its ruby functions

Attributes

type[R]
value[R]

Public Class Methods

new(search_params_hash) click to toggle source

Initialize DistanceFilter object from Hash.

Usage

DistanceFilter.new(gt: 5.4)

Supports only one filter as search parameterfor now.

# File lib/starline/entities/distance_filter.rb, line 26
def initialize(search_params_hash)
  raise_invalid_argument_type_error unless search_params_hash.is_a?(Hash)
  raise_unknown_filter_error if search_params_hash.empty?
  raise_ambiguous_filter_error if search_params_hash.size > 1

  @type  = search_params_hash.keys.first
  @value = search_params_hash.values.first

  raise_unknown_filter_error unless supported_filter?
  raise_invalid_value_error unless valid_value?
end

Public Instance Methods

apply_to(collection) click to toggle source

Applies filter to a given collection.

Usage

filter.apply_to(tracks)

Returns a collection with items which satisfy the current filter

# File lib/starline/entities/distance_filter.rb, line 45
def apply_to(collection)
  collection.select { |item| item.distance.send(operator, @value) }
end

Private Instance Methods

operator() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 58
def operator
  OPERATORS[type]
end
raise_ambiguous_filter_error() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 71
def raise_ambiguous_filter_error
  raise ArgumentError, "Multiple search parameters are not supported"
end
raise_invalid_argument_type_error() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 62
def raise_invalid_argument_type_error
  raise ArgumentError,
    "Invalid argument type. Search filter must be a Hash"
end
raise_invalid_value_error() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 75
def raise_invalid_value_error
  raise ArgumentError, "Filter value must be numeric typed"
end
raise_unknown_filter_error() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 67
def raise_unknown_filter_error
  raise ArgumentError, "Filter type is unknown"
end
supported_filter?() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 50
def supported_filter?
  FILTERS.include?(type)
end
valid_value?() click to toggle source
# File lib/starline/entities/distance_filter.rb, line 54
def valid_value?
  value.is_a?(Numeric)
end