Class: Smogon::Type::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/smogon/types/base.rb

Direct Known Subclasses

Ability, Item, Move, Moveset, Pokemon

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
# File 'lib/smogon/types/base.rb', line 23

def initialize(response = nil)
  return unless response

  attributes.each do |attr|
    public_send("#{attr}=", response[attr])
  end
end

Instance Method Details

#to_hObject



31
32
33
34
35
# File 'lib/smogon/types/base.rb', line 31

def to_h
  attributes.zip(
    attributes.map { |attr| public_send(attr) }
  ).to_h
end

#to_sObject



37
38
39
40
41
42
43
44
# File 'lib/smogon/types/base.rb', line 37

def to_s
  attributes.map do |attr|
    key = attr.capitalize.gsub('_', ' ')
    val = public_send(attr)
    val = val.join(', ') if val.is_a?(Array)
    val == '' ? nil : "#{key}: #{val}"
  end.compact.join("\n")
end

#urlObject



46
47
48
49
50
51
52
53
# File 'lib/smogon/types/base.rb', line 46

def url
  [
    'http://www.smogon.com/dex',
    ::Smogon::API::METAGAME,
    self.class::TYPE,
    name.gsub(' ', '_')
  ].join('/')
end