Skip to content

Commit 06fc764

Browse files
committed
Add nullable and value objct docs
1 parent 108a594 commit 06fc764

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

pyvalueobjects/abstract/nullablevalueobject.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
21
def build_nullable(cls):
2+
"""
3+
Dynamically creates a nullable version of the given value object class.
4+
This allows the value to be either the specified type or None.
5+
6+
:param cls: The class to create a nullable version of (typically a subclass of ValueObject).
7+
:return: A new class Nullable that wraps the input class, allowing None as a valid value.
8+
"""
39
class Nullable(cls):
410

511
def __init__(self, value):

pyvalueobjects/abstract/valueobject.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,53 @@
44

55

66
class ValueObject(ABC):
7+
"""
8+
Abstract base class for all value objects. Ensures that values are validated and encapsulated.
9+
"""
710

811
def __init__(self, value):
12+
"""
13+
Initialize the value object with the given value and validate it.
14+
:param value: The value to be stored and validated.
15+
"""
916
self._validate(value)
1017
self._value = value
1118

1219
def value(self):
20+
"""
21+
Return the raw value stored in this value object.
22+
:return: The stored value.
23+
"""
1324
return self._value
1425

1526
def str_value(self) -> str:
27+
"""
28+
Return the string representation of the stored value.
29+
:return: A string representation of the value.
30+
"""
1631
return str(self._value)
1732

1833
def _validate(self, value):
34+
"""
35+
Validate the given value. This method should be overridden by subclasses to enforce specific constraints.
36+
:param value: The value to validate.
37+
"""
1938
pass
2039

2140
def __hash__(self):
41+
"""
42+
Return a hash of the stored value, allowing the object to be used in hash-based collections.
43+
:return: The hash of the value.
44+
"""
2245
return hash(self._value)
2346

2447
def __eq__(self, other):
48+
"""
49+
Compare this value object with another. Raises a ValueObjectError if the other object is not a ValueObject.
50+
:param other: The other value object to compare with.
51+
:return: True if the values are equal, False otherwise.
52+
:raises: ValueObjectError if the other object is not a ValueObject.
53+
"""
2554
if not isinstance(other, ValueObject):
2655
raise ValueObjectError('Comparing with a non value object.')
2756
return self._value == other.value()

0 commit comments

Comments
 (0)