diff --git a/openbookr/ABC.py b/openbookr/ABC.py new file mode 100644 index 0000000..6d065df --- /dev/null +++ b/openbookr/ABC.py @@ -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 diff --git a/openbookr/builders.py b/openbookr/builders.py new file mode 100644 index 0000000..393642a --- /dev/null +++ b/openbookr/builders.py @@ -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) diff --git a/openbookr/functions.py b/openbookr/functions.py new file mode 100644 index 0000000..0c75c96 --- /dev/null +++ b/openbookr/functions.py @@ -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__() diff --git a/openbookr/models.py b/openbookr/models.py index d1c6f08..57c905b 100644 --- a/openbookr/models.py +++ b/openbookr/models.py @@ -4,7 +4,7 @@ # Classes description # ####################### -from collections.abc import Iterable +from typing import Iterable from pydantic import BaseModel as BM @@ -13,14 +13,6 @@ class Book(BM): 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 hashkey: str author: str | None = None