class Pokedex::Filter

Attributes

exclude_keys[RW]
include_keys[RW]
pokemons[RW]

Public Class Methods

new() click to toggle source
# File lib/pokedex/filter.rb, line 7
def initialize
  @pokemons = Pokedex.all
  @include_keys = nil
  @exclude_keys = nil
end

Public Instance Methods

except(*params) click to toggle source
# File lib/pokedex/filter.rb, line 142
def except(*params)
  @exclude_keys = params
  self
end
fuzzy(*params, lang: nil) click to toggle source
# File lib/pokedex/filter.rb, line 50
def fuzzy(*params, lang: nil)
  results = []

  params.each do |fuzzy_name|
    results += @pokemons.select do |pokemon|
      if lang
        pokemon['name'][lang].downcase.include?(fuzzy_name.downcase)
      else
        pokemon['name'].keys.find do |key|
          pokemon['name'][key].downcase.include?(fuzzy_name.downcase)
        end
      end
    end
  end

  @pokemons = results
  self
end
ichooseyou!() click to toggle source
# File lib/pokedex/filter.rb, line 118
def ichooseyou!
  random(1).take
end
id(*params) click to toggle source
# File lib/pokedex/filter.rb, line 13
def id(*params)
  results = []

  params.flatten.each do |id|
    case id
    when Integer
      results += @pokemons.select { |pokemon| pokemon['id'] == id }
    when Range
      results += @pokemons.select { |pokemon| pokemon['id'] >= id.first && pokemon['id'] <= id.last }
    else
      raise
    end
  end

  @pokemons = results
  self
end
name(*params, lang: nil) click to toggle source
# File lib/pokedex/filter.rb, line 31
def name(*params, lang: nil)
  results = []

  params.each do |name|
    results += @pokemons.select do |pokemon|
      if lang
        pokemon['name'][lang].casecmp(name).zero?
      else
        pokemon['name'].keys.find do |key|
          pokemon['name'][key].casecmp(name).zero?
        end
      end
    end
  end

  @pokemons = results
  self
end
only(*params) click to toggle source
# File lib/pokedex/filter.rb, line 137
def only(*params)
  @include_keys = params
  self
end
random(num = 1) click to toggle source
# File lib/pokedex/filter.rb, line 113
def random(num = 1)
  @pokemons = @pokemons.sample(num)
  self
end
region(*params, lang: nil) click to toggle source
# File lib/pokedex/filter.rb, line 94
def region(*params, lang: nil)
  region_range = []

  params.each do |reg|
    if lang
      region_range += Pokedex.region.select { |region| region['name'][lang].casecmp(reg).zero? }.map { |r| r['range'] }
    else
      region_range += Pokedex.region.select { |region| region['name'].keys.find { |key| region['name'][key].casecmp(reg).zero? } }.map { |r| r['range'] }
    end
  end

  region_range.map! do |range|
    range['from']..range['to']
  end

  @pokemons = id(region_range).take
  self
end
reset() click to toggle source
# File lib/pokedex/filter.rb, line 147
def reset
  @pokemons = Pokedex.all
  self
end
take() click to toggle source
# File lib/pokedex/filter.rb, line 122
def take
  raise "Both `only' and `except' are called. Choose either one." if @include_keys && @exclude_keys

  if @include_keys
    @pokemons.each do |pokemon|
      exclude_keys = pokemon.keys - @include_keys
      exclude_keys.each { |key| pokemon.delete(key) }
    end
  elsif @exclude_keys
    @exclude_keys.each { |key| @pokemons.each { |pokemon| pokemon.delete(key) } }
  end

  @pokemons
end
type(*params) click to toggle source

'water'

'grass', 'poison'

'grass', 'poison'

'grass'], ['poison'

['grass'], 'poison'

'grass', 'poison'], ['fire', 'flying'

FIX: cannot find pokemons when order is opposite it can find: ['fire', 'flying'] but it cannot find: ['flying', 'fire']

# File lib/pokedex/filter.rb, line 79
def type(*params)
  @pokemons = params.map do |type|
    case type
    when String
      @pokemons.select { |pokemon| pokemon['type'].map(&:downcase).include?(type.downcase) }
    when Array
      @pokemons.select { |pokemon| pokemon['type'].map(&:downcase) == type.map(&:downcase) }
    else
      raise
    end
  end.flatten

  self
end