Tadeas Kosek
apply generated DDD abstraction
92fd1a7
"""File size value object."""
from dataclasses import dataclass
from ..exceptions.domain_exceptions import ValidationError
@dataclass(frozen=True)
class FileSize:
"""Value object representing file size."""
bytes: int
def __post_init__(self):
if self.bytes < 0:
raise ValidationError("File size cannot be negative")
@property
def megabytes(self) -> float:
"""Get size in megabytes."""
return self.bytes / (1024 * 1024)
@property
def gigabytes(self) -> float:
"""Get size in gigabytes."""
return self.bytes / (1024 * 1024 * 1024)
def is_within_limit(self, max_mb: float) -> bool:
"""Check if file size is within specified limit in MB."""
return self.megabytes <= max_mb
def __str__(self) -> str:
"""Human-readable string representation."""
if self.gigabytes >= 1:
return f"{self.gigabytes:.2f} GB"
elif self.megabytes >= 1:
return f"{self.megabytes:.1f} MB"
else:
return f"{self.bytes} bytes"
def __lt__(self, other: 'FileSize') -> bool:
return self.bytes < other.bytes