removing initialize from Person

master
Matt Huntington 8 years ago
parent 4f66e1a676
commit 6a192c4555

@ -41,11 +41,11 @@ class Company
last_company_id = result["id"] last_company_id = result["id"]
end end
if result["person_id"] if result["person_id"]
companies.last.employees.push(Person.new({ companies.last.employees.push({
"id" => result["person_id"], "id" => result["person_id"].to_i,
"name" => result["person_name"], "name" => result["person_name"],
"age" => result["age"] "age" => result["age"].to_i,
})) })
end end
end end
return companies return companies
@ -69,11 +69,11 @@ class Company
employees = []; employees = [];
results.each do |result| results.each do |result|
if result["person_id"] if result["person_id"]
employees.push(Person.new({ employees.push({
"id" => result["person_id"], "id" => result["person_id"].to_i,
"name" => result["person_name"], "name" => result["person_name"],
"age" => result["age"] "age" => result["age"].to_i,
})) })
end end
end end
return Company.new({ return Company.new({

@ -42,11 +42,11 @@ class Location
last_location_id = result["id"] last_location_id = result["id"]
end end
if result["person_id"] if result["person_id"]
new_person = Person.new({ new_person = {
"id" => result["person_id"], "id" => result["person_id"].to_i,
"name" => result["name"], "name" => result["name"],
"age" => result["age"], "age" => result["age"].to_i,
}) }
locations.last.inhabitants.push(new_person) locations.last.inhabitants.push(new_person)
end end
end end
@ -70,10 +70,10 @@ class Location
inhabitants = [] inhabitants = []
results.each do |result| results.each do |result|
if result["person_id"] if result["person_id"]
inhabitants.push Person.new({ inhabitants.push({
"id" => result["person_id"], "id" => result["person_id"].to_i,
"name" => result["name"], "name" => result["name"],
"age" => result["age"] "age" => result["age"].to_i,
}) })
end end
end end

@ -1,19 +1,6 @@
class Person class Person
attr_reader :id, :name, :age, :home, :employers
# connect to postgres # connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') DB = PG.connect({:host => "localhost", :port => 5432, :dbname => 'contacts'})
def initialize(opts = {})
@id = opts["id"].to_i
@name = opts["name"]
@age = opts["age"].to_i
if opts["home"]
@home = opts["home"]
end
if opts["employers"]
@employers = opts["employers"]
end
end
def self.all def self.all
results = DB.exec( results = DB.exec(
@ -50,15 +37,13 @@ class Person
} }
) )
end end
new_person = Person.new( new_person = {
{
"id" => result["id"], "id" => result["id"],
"name" => result["name"], "name" => result["name"],
"age" => result["age"], "age" => result["age"],
"home" => home, "home" => home,
"employers" => [] "employers" => []
} }
)
people.push(new_person) people.push(new_person)
end end
if result["company_id"] if result["company_id"]
@ -67,7 +52,7 @@ class Person
"name" => result["company"], "name" => result["company"],
"industry" => result["industry"] "industry" => result["industry"]
}) })
people.last.employers.push(employer) people.last["employers"].push(employer)
end end
end end
return people return people
@ -116,15 +101,13 @@ class Person
})) }))
end end
end end
person = Person.new( person = {
{
"id" => result["id"], "id" => result["id"],
"name" => result["name"], "name" => result["name"],
"age" => result["age"], "age" => result["age"],
"home" => home, "home" => home,
"employers" => employers "employers" => employers
} }
)
return person return person
end end
@ -136,12 +119,17 @@ class Person
RETURNING id, name, age, home_id; RETURNING id, name, age, home_id;
SQL SQL
) )
return Person.new(results.first) return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"age" => results.first["age"].to_i,
"home_id" => results.first["home_id"].to_i,
}
end end
def self.delete(id) def self.delete(id)
results = DB.exec("DELETE FROM people WHERE id=#{id};") results = DB.exec("DELETE FROM people WHERE id=#{id};")
return { deleted: true } return { "deleted" => true }
end end
def self.update(id, opts={}) def self.update(id, opts={})
@ -153,7 +141,12 @@ class Person
RETURNING id, name, age, home_id; RETURNING id, name, age, home_id;
SQL SQL
) )
return Person.new(results.first) return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"age" => results.first["age"].to_i,
"home_id" => results.first["home_id"].to_i,
}
end end
def self.setHome(person_id, home) def self.setHome(person_id, home)
@ -162,9 +155,14 @@ class Person
UPDATE people UPDATE people
SET home_id = #{home.id} SET home_id = #{home.id}
WHERE id = #{person_id} WHERE id = #{person_id}
RETURNING id, name, age; RETURNING id, name, age, home_id;
SQL SQL
) )
return Person.new(results.first) return {
"id" => results.first["id"].to_i,
"name" => results.first["name"],
"age" => results.first["age"].to_i,
"home_id" => results.first["home_id"].to_i,
}
end end
end end

Loading…
Cancel
Save