Class: Pokeedex::Pokemon::Model::Base

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

Overview

Base class for the Pokemon model. It holds the database connection and the methods to interact with the database table for the Pokemon model

Constant Summary collapse

CACHE_TTL =
3600

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.search(query) ⇒ Object

Search for a Pokemon by number or name



23
24
25
# File 'lib/pokeedex/pokemon/model/base.rb', line 23

def self.search(query)
  where(Sequel.|({ number: query }, Sequel.ilike(:name, query)))
end

Instance Method Details

#persisted?Boolean

Return true when the Pokemon model is persisted in the database

Returns:

  • (Boolean)


37
38
39
# File 'lib/pokeedex/pokemon/model/base.rb', line 37

def persisted?
  !id.nil?
end

#validateObject

Validate the Pokemon model attributes



29
30
31
32
33
# File 'lib/pokeedex/pokemon/model/base.rb', line 29

def validate
  super
  validates_presence %i[number name]
  validates_unique :number
end