Skip to content

Commit

Permalink
Adds lazy loading for the lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo committed Aug 28, 2020
1 parent 9743ca9 commit ef850e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function getRandomSlug(): string
{
$slug = '';

$lists = $this->getLinnaeusOptions()->lists;
$lists = $this->getLinnaeusOptions()->getLists();

foreach ($this->getLinnaeusOptions()->structure as $slug_part) {
switch ($slug_part) {
Expand Down
23 changes: 20 additions & 3 deletions src/LinnaeusOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LinnaeusOptions
public string $separator;
public bool $create;
public bool $update;
public array $lists;
protected array $lists = [];
public array $invalid_animals;
public array $invalid_adjectives;
public array $invalid_colors;
Expand All @@ -31,8 +31,6 @@ public function __construct()
$this->invalid_animals = config('linnaeus.invalid_animals', []);
$this->invalid_adjectives = config('linnaeus.invalid_adjectives', []);
$this->invalid_colors = config('linnaeus.invalid_colors', []);

$this->updateLists();
}

public static function create(): self
Expand Down Expand Up @@ -83,6 +81,25 @@ public function withInvalidColors(array $invalid): self
return $this;
}

public function getLists(): array
{
if (empty($this->lists)) {
$this->updateLists();
}

return $this->lists;
}

public function getRawLists(): array
{
return $this->lists;
}

public function getList(string $list_name): array
{
return $this->getLists()[$list_name];
}

private function updateLists()
{
$this->lists = [];
Expand Down
41 changes: 27 additions & 14 deletions tests/LinnaeusOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function it_has_defaults()
$this->assertEquals('-', $data->separator);
$this->assertTrue($data->create);
$this->assertFalse($data->update);
$this->assertNotEmpty($data->lists);
$this->assertCount(2, $data->lists);
foreach ($data->lists as $list) {
$this->assertNotEmpty($data->getLists());
$this->assertCount(2, $data->getLists());
foreach ($data->getLists() as $list) {
$this->assertNotEmpty($list);
}
$this->assertEmpty($data->invalid_animals);
Expand All @@ -42,9 +42,9 @@ public function it_has_defaults_when_missing_config()
$this->assertEquals('-', $data->separator);
$this->assertTrue($data->create);
$this->assertFalse($data->update);
$this->assertNotEmpty($data->lists);
$this->assertCount(2, $data->lists);
foreach ($data->lists as $list) {
$this->assertNotEmpty($data->getLists());
$this->assertCount(2, $data->getLists());
foreach ($data->getLists() as $list) {
$this->assertNotEmpty($list);
}
$this->assertEmpty($data->invalid_animals);
Expand Down Expand Up @@ -125,10 +125,10 @@ public function it_sets_invalid_colors()
public function it_updates_the_lists()
{
$data = LinnaeusOptions::create();
$this->assertCount(2, $data->lists);
$this->assertCount(2, $data->getLists());

$data->withStructure(['color', 'adjective', 'adjective', 'animal']);
$this->assertCount(3, $data->lists);
$this->assertCount(3, $data->getLists());
}

/** @test */
Expand All @@ -141,8 +141,8 @@ public function it_populates_animals_list_correctly()
$data = LinnaeusOptions::create()
->withStructure(['animal']);

$this->assertCount(1, $data->lists['animals']);
$this->assertEquals(['aardvark' => 'Aardvark'], $data->lists['animals']);
$this->assertCount(1, $data->getList('animals'));
$this->assertEquals(['aardvark' => 'Aardvark'], $data->getList('animals'));
}

/** @test */
Expand All @@ -155,8 +155,8 @@ public function it_populates_adjectives_list_correctly()
$data = LinnaeusOptions::create()
->withStructure(['adjective']);

$this->assertCount(1, $data->lists['adjectives']);
$this->assertEquals(['zany' => 'Zany'], $data->lists['adjectives']);
$this->assertCount(1, $data->getList('adjectives'));
$this->assertEquals(['zany' => 'Zany'], $data->getList('adjectives'));
}

/** @test */
Expand All @@ -169,7 +169,20 @@ public function it_populates_colors_list_correctly()
$data = LinnaeusOptions::create()
->withStructure(['color']);

$this->assertCount(1, $data->lists['colors']);
$this->assertEquals(['blue' => 'Blue'], $data->lists['colors']);
$this->assertCount(1, $data->getList('colors'));
$this->assertEquals(['blue' => 'Blue'], $data->getList('colors'));
}

/** @test */
public function it_lazy_loads_the_lists()
{
$data = LinnaeusOptions::create();

$this->assertEmpty($data->getRawLists());

$data->getList('animals');

$this->assertNotEmpty($data->getRawLists());
$this->assertCount(2, $data->getRawLists());
}
}

0 comments on commit ef850e1

Please sign in to comment.