File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1-
21def 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 ):
Original file line number Diff line number Diff line change 44
55
66class 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 ()
You can’t perform that action at this time.
0 commit comments