Class: Pokemon

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

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Pokemon

Returns a new instance of Pokemon.



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

def initialize(name)
  @name = name
  @type = Scraper.get_pokemon_by_name(name)["types"][0]["type"]["name"]
  @@all << self
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/pokemon.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/pokemon.rb', line 4

def type
  @type
end

Class Method Details

.allObject



21
22
23
# File 'lib/pokemon.rb', line 21

def self.all
  @@all
end

.find_or_create_by_name(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/pokemon.rb', line 25

def self.find_or_create_by_name(name)
  if Scraper.pokemons.include?(name)
    return self.all.detect{|a| a.name == name} if self.all.collect{|a| a.name}.include?(name)
    return Pokemon.new(name)
  end
  return nil
end

Instance Method Details

#can_learn_move?(move) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/pokemon.rb', line 17

def can_learn_move?(move)
  self.get_moves.collect{|m| m.name}.include?(move.name)
end

#get_movesObject



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

def get_moves
  Scraper.get_pokemon_by_name(@name)["moves"].collect{|move_hash| Move.new(move_hash["move"]["name"])}
end