Class: Pokedex::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



7
8
9
10
11
# File 'lib/pokedex/filter.rb', line 7

def initialize
  @pokemons = Pokedex.all
  @include_keys = nil
  @exclude_keys = nil
end

Instance Attribute Details

#exclude_keysObject

Returns the value of attribute exclude_keys.



5
6
7
# File 'lib/pokedex/filter.rb', line 5

def exclude_keys
  @exclude_keys
end

#include_keysObject

Returns the value of attribute include_keys.



5
6
7
# File 'lib/pokedex/filter.rb', line 5

def include_keys
  @include_keys
end

#pokemonsObject

Returns the value of attribute pokemons.



5
6
7
# File 'lib/pokedex/filter.rb', line 5

def pokemons
  @pokemons
end

Instance Method Details

#except(*params) ⇒ Object



142
143
144
145
# File 'lib/pokedex/filter.rb', line 142

def except(*params)
  @exclude_keys = params
  self
end

#fuzzy(*params, lang: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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!Object



118
119
120
# File 'lib/pokedex/filter.rb', line 118

def ichooseyou!
  random(1).take
end

#id(*params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



137
138
139
140
# File 'lib/pokedex/filter.rb', line 137

def only(*params)
  @include_keys = params
  self
end

#random(num = 1) ⇒ Object



113
114
115
116
# File 'lib/pokedex/filter.rb', line 113

def random(num = 1)
  @pokemons = @pokemons.sample(num)
  self
end

#region(*params, lang: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

#resetObject



147
148
149
150
# File 'lib/pokedex/filter.rb', line 147

def reset
  @pokemons = Pokedex.all
  self
end

#takeObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object

‘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’]



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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