@ -144,18 +144,19 @@ This might seem relatively minor, but it makes more sense to store the data this
## Part 2 - code along
## Part 2 - code along
First, let's define a new method `data_transform`. It will take a single hash inside of an array called `person_array`:
First, let's define a new function `dataTransform`. It will take a single associative array inside of an indexed array called `person_array`:
```rb
```php
def data_transform(person_array)
function dataTransform($person_array){
end
}
```
```
Now let's create a fake `people` array, containing a single hash. This will represent what the PG gem will give us from Postgres when we use it in rails. Next, call `data_transform` and pass that fake `people` array in as a parameter:
Now let's create a fake `$people` array, containing a single associative array that represents one person. This `$people` array is similar to what PHP will give us after it retrieves data from Postgres. In reality, the `$people` array would contain multiple people (associative arrays), but for now let's just put one in. Next, call `dataTransform` and pass that "fake" `$people` array in as a parameter:
```rb
```php
people = [
$people = [
{
[
"name"=>"Chase",
"name"=>"Chase",
"age"=>"30",
"age"=>"30",
"home_id"=>"1",
"home_id"=>"1",
@ -163,60 +164,74 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=>"10019"
"home_zip_code"=>"10019"
}
]
]
];
function dataTransform($person_array){
def data_transform(person_array)
}
end
dataTransform($people);
```
Second, inside of `dataTransform`, let's return an empty associative array called `$person`:
p data_transform people
```php
function dataTransform($person_array){
$person = [];
return $person;
}
```
```
Second, inside of `data_transform`, let's make a new object called `person`
Set the result of `dataTransform($people);` to a variable and then `var_dump()` it:
```rb
```php
def data_transform(person_array)
$result = dataTransform($people);
person = {}
var_dump($result);
end
```
```
We can make whatever keys we want for our new object:
We can make whatever keys we want for our new object:
```rb
```php
def data_transform(person_array)
function dataTransform($person_array){
person = {
$person = [
"name" => "Chase",
"name" => "Chase",
"age" => "30"
"age" => "30"
}
];
end
return $person;
}
```
```
And we can ALMOST place our values from our `person_array` into our new hash
And we can ALMOST place our values from our `$person_array` into our new hash
```rb
```php
person = {
function dataTransform($person_array){
"name" => person_array["age"],
$person = [
"age" => person_array["age"]
"name" => $person_array["name"],
"age" => $person_array["age"]
];
return $person;
}
}
```
```
Check it! This won't quite work because our `person_array` is an array. We can easily take the single hash out of the array using a ruby method `.first`
Check it! This won't quite work because our `$person_array` is an associative array inside an indexed array (remember, `$person_array` will eventually contain multiple people). We can easily take the associative array out of the indexed array though:
```rb
```php
def data_transform(person_array)
function dataTransform($person_array){
person = person_array.first
$first_person = $person_array[0];
return {
$person = [
"name" => person["name"],
"name" => $first_person["name"],
"age" => person["age"]
"age" => $first_person["age"]
}
];
end
return $person;
}
```
```
Now try again! The full code:
Now try again! The full code:
```rb
```php
people = [
$people = [
{
[
"name"=>"Chase",
"name"=>"Chase",
"age"=>"30",
"age"=>"30",
"home_id"=>"1",
"home_id"=>"1",
@ -224,76 +239,115 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=>"10019"
"home_zip_code"=>"10019"
}
]
]
];
def data_transform(person_array)
function dataTransform($person_array){
person = person_array.first
$first_person = $person_array[0];
return {
$person = [
"name" => person["name"],
"name" => $first_person["name"],
"age" => person["age"]
"age" => $first_person["age"]
}
];
end
return $person;
}
p data_transform people
$result = dataTransform($people);
var_dump($result);
```
```
should print a hash containing just the name and the age of the person:
This should print a single associative array containing just the name and the age of the person:
```rb
```php
{"name"=>"Chase", "age"=>"30"}
array(2) {
["name"]=>
string(5) "Chase"
["age"]=>
string(2) "30"
}
```
```
It *ALMOST* matches the **New Format**, but the `age` is a string. We can use a method to fix that in a snap!
It *ALMOST* matches the **New Format**, but the `age` is a string. We can use a special PHP function called `intval()` to fix that:
```rb
```php
return {
$person = [
"name" => person["name"],
"name" => $first_person["name"],
"age" => person["age"].to_i
"age" => intval($first_person["age"])
}
];
```
```
Check it again!
Check it again!
```rb
```php
{"name"=>"Chase", "age"=>30}
array(2) {
["name"]=>
string(5) "Chase"
["age"]=>
int(30)
}
```
```
## Part 3 - build it
## Part 3 - build it
Don't forget, we want our `data_transform` function to return an object that also includes the other information about our person.
Don't forget, we want our `dataTransform` function to return an object that also includes the other information about our person.
```rb
```php
{
[
"name"=>"Chase",
"name"=>"Chase",
"age"=>30,
"age"=>30,
"home"=> {
"home"=> [
"id"=> 1,
"id"=> 1,
"street"=>"1600 Broadway",
"street"=>"1600 Broadway",
"city"=>"New York",
"city"=>"New York",
"state"=>"NY",
"state"=>"NY",
"zip_code"=> 10019
"zip_code"=> 10019
}
]
}
]
```
```
How do we get that nested `home`hash in the results?
How do we get that nested `home`associative array in the results?
## Part 4 - build it
## Part 4 - build it
As we get new clients, not all of them will have a home right away. We need to add a way to check `if` a person has a home address, and if true, return the reshaped hash `else` just return the `person` hash
As we get new clients, not all of them will have a home right away. We need to add a way to check `if` a person has a home address, and `if` that's the case, `return` the reshaped associative array `else` just `return` the `name` and `age` properties like we did in part 2.
Don't forget to test it, there is sample 'data' available in Part 1 for you to use
Don't forget to test it with something like this:
```php
$people = [
[
"name"=>"Chase",
"age"=>"30",
"home_id"=>"1",
"home_street"=>"1600 Broadway",
"home_city"=>"New York",
"home_state"=>"NY",
"home_zip_code"=>"10019"
]
];
$result = dataTransform($people);
var_dump($result);
$people2 = [
[
"name" => "Gert",
"age" => "23"
]
];
$result2 = dataTransform($people2);
var_dump($result2);
```
## Part 5 - build it
## Part 5 - build it
HELLO already has three tenants! Converting each object's format one by one is a drag. Write a new function `data_transforms` that converts an array of many `people` hashes into the **New Format** shape:
HELLO already has three tenants! Converting each object's format one by one is a drag. Update `dataTransform` so that it converts an array of many `people` into the **New Format** shape:
### Original Format
### Original Format
```rb
```php
people = [
$people = [
{
[
"name"=>"Gert",
"name"=>"Gert",
"age"=>"23",
"age"=>"23",
"home_id"=> "1",
"home_id"=> "1",
@ -301,8 +355,8 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=> "10019"
"home_zip_code"=> "10019"
},
],
{
[
"name"=>"Alex",
"name"=>"Alex",
"age"=>"42",
"age"=>"42",
"home_id"=> "1",
"home_id"=> "1",
@ -310,64 +364,52 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=> "10019"
"home_zip_code"=> "10019"
},
],
{
[
"name"=>"Nico",
"name"=>"Nico",
"age"=>"61",
"age"=>"61"
"home_id"=> "1",
]
"home_street"=>"1600 Broadway",
];
"home_city"=>"New York",
"home_state"=>"NY",
"home_zip_code"=> "10019"
}
]
```
```
Check yourself: does your method still work if a person doesn't yet have a home address?
Check yourself: does your method still work if a person doesn't yet have a home address?
### New Format
### New Format
```rb
```php
people = [
[
{
[
"name"=>"Gert",
"name"=>"Gert",
"age"=>23,
"age"=>23,
"home"=> {
"home"=> [
"id"=> 1,
"id"=> 1,
"street"=>"1600 Broadway",
"street"=>"1600 Broadway",
"city"=>"New York",
"city"=>"New York",
"state"=>"NY",
"state"=>"NY",
"zip_code"=> 10019
"zip_code"=> 10019
}
]
},
],
{
[
"name"=>"Alex",
"name"=>"Alex",
"age"=>42,
"age"=>42,
"home"=> {
"home"=> [
"id"=> 1,
"id"=> 1,
"street"=>"1600 Broadway",
"street"=>"1600 Broadway",
"city"=>"New York",
"city"=>"New York",
"state"=>"NY",
"state"=>"NY",
"zip_code"=> 10019
"zip_code"=> 10019
}
]
},
],
{
[
"name"=>"Nico",
"name"=>"Nico",
"age"=> 61,
"age"=> 61
"home"=> {
]
"id"=> 1,
"street"=>"1600 Broadway",
"city"=>"New York",
"state"=>"NY",
"zip_code"=> 10019
}
}
]
]
```
```
## Part 6
## Part 6
For this part, write a brand new `data_transforms_locations` method. Save your previous one(s)
For this part, write a brand new `dataTransformLocations` method. Save your previous one(s)
HELLO is growing rapidly and has acquired an amazing high profile space for their next luxury dorm experience. So next, we'd like to be able to show a list of all our addresses with their corresponding `tenants`:
HELLO is growing rapidly and has acquired an amazing high profile space for their next luxury dorm experience. So next, we'd like to be able to show a list of all our addresses with their corresponding `tenants`:
@ -375,9 +417,9 @@ HELLO is growing rapidly and has acquired an amazing high profile space for thei
**NOTE** assume all people with `home_id` of 1 are next to each other in the array. Same with `home_id` of 2, 3, etc
**NOTE** assume all people with `home_id` of 1 are next to each other in the array. Same with `home_id` of 2, 3, etc
```rb
```php
people = [
$people = [
{
[
"name"=>"Gert",
"name"=>"Gert",
"age"=>"23",
"age"=>"23",
"home_id"=> "1",
"home_id"=> "1",
@ -385,8 +427,8 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=> "10019"
"home_zip_code"=> "10019"
},
],
{
[
"name"=>"Alex",
"name"=>"Alex",
"age"=>"42",
"age"=>"42",
"home_id"=> "1",
"home_id"=> "1",
@ -394,8 +436,8 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=> "10019"
"home_zip_code"=> "10019"
},
],
{
[
"name"=>"Nico",
"name"=>"Nico",
"age"=>"61",
"age"=>"61",
"home_id"=> "1",
"home_id"=> "1",
@ -403,8 +445,8 @@ people = [
"home_city"=>"New York",
"home_city"=>"New York",
"home_state"=>"NY",
"home_state"=>"NY",
"home_zip_code"=> "10019"
"home_zip_code"=> "10019"
},
],
{
[
"name"=>"Molly",
"name"=>"Molly",
"age"=>"55",
"age"=>"55",
"home_id"=> "2",
"home_id"=> "2",
@ -412,8 +454,8 @@ people = [
"home_city"=>"San Francisco",
"home_city"=>"San Francisco",
"home_state"=>"CA",
"home_state"=>"CA",
"home_zip_code"=> "94133"
"home_zip_code"=> "94133"
},
],
{
[
"name"=>"Karolina",
"name"=>"Karolina",
"age"=>"82",
"age"=>"82",
"home_id"=> "2",
"home_id"=> "2",
@ -421,83 +463,83 @@ people = [
"home_city"=>"San Francisco",
"home_city"=>"San Francisco",
"home_state"=>"CA",
"home_state"=>"CA",
"home_zip_code"=> "94133"
"home_zip_code"=> "94133"
}
]
]
];
```
```
### New Format
### New Format
```rb
```php
[
[
{
[
"id"=> 1,
"id"=> 1,
"street"=>"1600 Broadway",
"street"=>"1600 Broadway",
"city"=>"New York",
"city"=>"New York",
"state"=>"NY",
"state"=>"NY",
"zip_code"=> 10019,
"zip_code"=> 10019,
"tenants" => [
"tenants" => [
{
[
"name"=>"Gert",
"name"=>"Gert",
"age"=>23
"age"=>23
},
],
{
[
"name"=>"Alex",
"name"=>"Alex",
"age"=>42
"age"=>42
},
],
{
[
"name"=>"Nico",
"name"=>"Nico",
"age"=>61
"age"=>61
}
]
]
},
]
{
],
[
"id"=> 2,
"id"=> 2,
"street"=>"1 Alcatraz Island",
"street"=>"1 Alcatraz Island",
"city"=>"San Francisco",
"city"=>"San Francisco",
"state"=>"CA",
"state"=>"CA",
"zip_code"=> 94133,
"zip_code"=> 94133,
"tenants" => [
"tenants" => [
{
[
"name"=>"Molly",
"name"=>"Molly",
"age"=>23
"age"=>23
},
],
{
[
"name"=>"Karolina",
"name"=>"Karolina",
"age"=>42
"age"=>42
}
]
]
}
]
]
]
]
```
```
## Part 7 - Hungry for more
## Part 7 - Hungry for more
Create the ability for `data_transform_location` to optionally return a specific home (with its `tenants`), based on an optional second `home_id` param:
Create the ability for `dataTransformLocations` to optionally return a specific home (with its `tenants`), based on an optional second `home_id` param:
```rb
```php
p data_transform_location people, 2
$result = dataTransform($people, 2);
```
```
should return
should return
```rb
```php
{
[
"id"=> 2,
"id"=> 2,
"street"=>"1 Alcatraz Island",
"street"=>"1 Alcatraz Island",
"city"=>"San Francisco",
"city"=>"San Francisco",
"state"=>"CA",
"state"=>"CA",
"zip_code"=> 94133,
"zip_code"=> 94133,
"tenants" => [
"tenants" => [
{
[
"name"=>"Molly",
"name"=>"Molly",
"age"=>23
"age"=>23
},
],
{
[
"name"=>"Karolina",
"name"=>"Karolina",
"age"=>42
"age"=>42
}
]
]
}
]
]
```
```
You did it! You started building your own [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping) (Object Relational Mapping)
You did it! You started building your own [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping) (Object Relational Mapping)