-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemon.rb
195 lines (175 loc) · 3.96 KB
/
pokemon.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require 'sqlite3'
class Pokemon
# Attributes
attr_reader :id
attr_accessor :pokemon_number
attr_accessor :name
# Returns a new instance.
#
# The constructor parameters can be passed in a hash or as a block.
#
# Example – Hash:
#
# pikachu = Pokemon.new(pokemon_number: 25, name: 'Pikachu')
#
# Example – Block:
#
# pikachu = Pokemon.new do |pokemon|
# pokemon.pokemon_number = 25
# pokemon.name = 'Pikachu'
# end
def initialize(id: nil, pokemon_number: nil, name: nil, &block)
end
# Creates and saves a new record into the database.
#
# Does .new and .save.
def self.create(...)
end
# Finds a record by its ID.
#
# Example:
#
# pikachu = Pokemon.find(25)
def self.find(id)
end
# Returns all records.
#
# Example:
#
# pokemons = Pokemon.all
def self.all
end
# Returns all records matching the specified conditions.
#
# The clause is a string passed to the query constructor as an SQL fragment, and used in the WHERE clause of the query.
#
# If no record is found, returns an empty array.
#
# Example – Positional placeholders:
#
# pokemons = Pokemon.where('name = ?', 'Pikachu')
#
# Example – Named placeholders:
#
# pokemons = Pokemon.where('name = :name', name: 'Pikachu')
def self.where(clause, *bind_variables)
end
# Finds the first record matching the specified conditions.
#
# The clause is a string passed to the query constructor as an SQL fragment, and used in the WHERE clause of the query with a LIMIT set to 1.
#
# If no record is found, returns nil.
#
# Example – Positional placeholders:
#
# pikachu = Pokemon.find_by('name = ?', 'Pikachu')
#
# Example – Named placeholders:
#
# pikachu = Pokemon.find_by('name = :name', name: 'Pikachu')
def self.find_by(clause, *bind_variables)
end
# Counts the number of records.
#
# Example:
#
# pokemon_count = Pokemon.count
# ⇒ 151
def self.count
end
# Saves (creates or updates) a record into the database.
#
# Called on .create and #update.
#
# Example – Create:
#
# pikachu = Pokemon.new(pokemon_number: 25, name: 'Pikachu')
# pikachu.save
#
# Example – Update:
#
# pikachu.name = 'Pika'
# pikachu.save
def save
end
# Updates attributes and saves the record.
#
# Updates and .save.
def update(pokemon_number: @pokemon_number, name: @name)
end
# Returns true if this object hasn’t been saved yet.
# Otherwise, returns false.
def new_record?
end
# Inserts the record into the database,
# and assigns the ID set by SQLite.
#
# Private method called on #save.
private def save_new_record
end
# Updates the record to the database.
#
# Private method called on #save.
private def save_record
end
# Deletes the record from the database.
#
# Note: The record is frozen, but still valid.
#
# Example:
#
# pikachu = Pokemon.find(25)
# pikachu.destroy
#
# pikachu.name
# ⇒ Pikachu
#
# pikachu.name = 'Pika'
# ⇒ Raise an error, because the object has been frozen.
def destroy
end
# Reloads the Pokémon from the database.
# Returns a new instance.
#
# Example:
#
# pokemon = pokemon.reload
def reload
end
# Checks a Pokémon exists in the database.
#
# Example:
#
# Pokemon.exists?(25)
# ⇒ true
def self.exists?(id)
end
# Checks the Pokémon is persisted in the database.
#
# Example:
#
# pikachu = Pokemon.find(25)
# pikachu.persisted?
# ⇒ true
#
# pikachu.destroy
# pikachu.persisted?
# ⇒ false
def persisted?
end
# Creates a new instance from a database row.
#
# Calls .new with named arguments.
def self.build_record(row)
end
# Creates a new instance from a database row as array.
#
# Internal method called on .build_record.
def self.build_record_from_array(row)
end
# Creates a new instance from a database row as hash.
#
# Internal method called on .build_record.
def self.build_record_from_hash(row)
end
end