Add BookBuilder

- Init ABC.py
- Init functions.py
- init builders.py
- Add BookCreator ABC class
- Add create_book static function
- Add BookBuilder implementaion of BookCreator
This commit is contained in:
trueold89 2024-07-19 21:38:09 +03:00
parent 4f6aa01202
commit c023193605
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
4 changed files with 108 additions and 9 deletions

23
openbookr/ABC.py Normal file
View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
###########################################
# Abstractions and interfaces description #
###########################################
from abc import ABC, abstractmethod
from openbookr.models import Book
class BookCreator(ABC):
"""
An abstract class that describes the creation of a book object
"""
book: Book
@abstractmethod
def _create_book(self, title: str, hashkey: str) -> None:
raise NotImplementedError
def __book__(self) -> Book:
return self.book

67
openbookr/builders.py Normal file
View File

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
###################
# Object builders #
###################
# Book
from typing import Self
from openbookr.models import Book
from openbookr.ABC import BookCreator
class BookBuilder(BookCreator):
"""
Builder pattern for creating Book object
"""
def __init__(self, title: str, hashkey: str):
"""
:param title: Book title
:param hashkey: Book file hash
"""
self._create_book(title, hashkey)
def set_author(self, author: str) -> Self:
"""
Set book author
:param: author: Book author name
"""
self.book.author = author
return self
def set_description(self, description: str) -> Self:
"""
Set book description
:param description: Book description
"""
self.book.description = description
return self
def add_tag(self, tag: str) -> Self:
"""
Add tag to book
:param tag: Tag name
"""
tags = list(self.book.tags)
tags.append(tag)
self.book.tags = tags.__iter__()
return self
def add_genre(self, genre: str) -> Self:
"""
Add genre to book
:param genre: Book genre
"""
genres = list(self.book.genres)
genres.append(genre)
self.book.genres = genres.__iter__()
return self
def _create_book(self, title: str, hashkey: str) -> None:
self.book = Book(name=title, hashkey=hashkey)

17
openbookr/functions.py Normal file
View File

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
####################
# Static functions #
####################
from openbookr.ABC import BookCreator
from openbookr.models import Book
def create_book(creator: BookCreator) -> Book:
"""
Creates book object
:param creator: Book creator iterface
"""
return creator.__book__()

View File

@ -4,7 +4,7 @@
# Classes description # # Classes description #
####################### #######################
from collections.abc import Iterable from typing import Iterable
from pydantic import BaseModel as BM from pydantic import BaseModel as BM
@ -13,14 +13,6 @@ class Book(BM):
Book model class Book model class
""" """
def __init__(self, name: str, hashkey: str) -> None:
"""
:param name: Book title
:param hashkey: Book file hash
"""
self.name = name
self.hashkey = hashkey
name: str name: str
hashkey: str hashkey: str
author: str | None = None author: str | None = None