Skip to content

Commit

Permalink
Merge pull request #9 from quiqueporta/3.0.0
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
quiqueporta authored Apr 13, 2024
2 parents 9fb2986 + 9daf415 commit 941e173
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 479 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -22,4 +22,4 @@ jobs:
pip install -r requirements-test.txt
- name: Test with mamba
run: |
PYTHONPATH=$PYTHONPATH:. mamba -f documentation
PYTHONPATH=$PYTHONPATH:. mamba -f documentation
41 changes: 16 additions & 25 deletions CHANGES.rst → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Changelog
=========
# Changelog

2.0.0 (2022-10-27)
------------------
## 3.0.0 (2024-04-13)

- Use python dataclass to create ValueObject

## 2.0.0 (2022-10-27)

- Rename CannotBeChangeException to CannotBeChanged
- Rename InvariantReturnValueException to InvariantMustReturnBool
Expand All @@ -11,55 +13,44 @@ Changelog
- Simplifly invariants now receives `self` attribute only
- Fix replace_mutable_kwargs_with_immutable_types for `set` kwargs


1.5.0 (2020-12-07)
------------------
## 1.5.0 (2020-12-07)

- Allow None as params. You can control it with invariants.

1.4.0 (2020-06-13)
------------------
## 1.4.0 (2020-06-13)

- Removing deprecation warnings
- Change license to MIT/X11

1.3.0 (2019-04-29)
------------------
## 1.3.0 (2019-04-29)

- Allow mutable data types but restricted for modifications.

1.2.0 (2019-01-15)
------------------
## 1.2.0 (2019-01-15)

- Reduced the creation time

1.1.1 (2015-09-05)
------------------
## 1.1.1 (2015-09-05)

- Fix hash for value objects within value objects.

1.1.0 (2018-09-04)
------------------
## 1.1.0 (2018-09-04)

- Fix value objects within value objects.
- Add value objects representation.

1.0.1 (2018-06-22)
------------------
## 1.0.1 (2018-06-22)

- Mutable types are not allowed.

0.2.1 (2015-10-05)
------------------
## 0.2.1 (2015-10-05)

- Removed unnecessary code.

0.2.0 (2015-10-03)
------------------
## 0.2.0 (2015-10-03)

- Created a invariant decorator.

0.1.0 (2015-10-01)
------------------
## 0.1.0 (2015-10-01)

- Initial release.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10-alpine
FROM python:3.12-alpine

WORKDIR /app

Expand Down
160 changes: 160 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Value Object

![Version number](https://img.shields.io/badge/version-3.0.0-blue.svg) ![License MIT](https://img.shields.io/github/license/quiqueporta/simple-value-object) ![Python Version](https://img.shields.io/badge/python-3.7,_3.8,_3.9,_3.10,3.11,3.12-blue.svg)

Based on Ruby Gem by [NoFlopSquad](https://github.com/noflopsquad/value-object)

A **value object** is a small object that represents a simple entity whose equality isn't based on identity:
i.e. two value objects are equal when they have the same value, not necessarily being the same object.

[Wikipedia](http://en.wikipedia.org/wiki/Value_object)

## New version 3.0

This new version is a complete rewrite of the library, now it uses **dataclasses** to define the value objects.
With this change we can use **type hints** to define the fields and the library will take care of the rest.
Now you have autocomplete and **type checking** in your IDE. With the previous version you had not autocomplete nor type checking.
You should be able to use this library with any version of python **3.7 or higher**.

## Installation

```sh
pip install simple-value-object
```

## Usage

### Constructor and field readers

```python
from simple_value_object import ValueObject

class Point(ValueObject):
x: int
y: int

point = Point(1, 2)

point.x
# 1

point.y
# 2

point.x = 5
# CannotBeChanged: You cannot change values from a Value Object, create a new one

class Date(ValueObject):
day: int
month: int
year: int

date = Date(1, 10, 2015)

date.day
# 1

date.month
# 10

date.year
# 2015

date.month = 5
# CannotBeChanged: You cannot change values from a Value Object, create a new one
```

### Equality based on field values

```python
from simple_value_object import ValueObject

class Point(ValueObject):
x: int
y: int


a_point = Point(5, 3)

same_point = Point(5, 3)

a_point == same_point
# True

a_different_point = Point(6, 3)

a_point == a_different_point
# False
```

### Hash code based on field values

```python
from simple_value_object import ValueObject

class Point(ValueObject):
x: int
y: int

a_point = Point(5, 3)

same_point = Point(5, 3)

a_point.hash == same_point.hash
# True

a_different_point = Point.new(6, 3)

a_point.hash == a_different_point.hash
# False
```

### Invariants

Invariants **must** return a boolean value.

```python
from simple_value_object import ValueObject, invariant

class Point(ValueObject):
x: int
y: int

@invariant
def inside_first_quadrant(self):
return self.x > 0 and self.y > 0

@invariant
def x_lower_than_y(self):
return self.x < self.y

Point(-5, 3)
#InvariantViolation: inside_first_cuadrant

Point(6, 3)
#InvariantViolation: x_lower_than_y

Point(1,3)
#<__main__.Point at 0x7f2bd043c780>
```

### ValueObject within ValueObject

```python
from simple_value_object import ValueObject, invariant

class Currency(ValueObject):
symbol: str

class Money(ValueObject):
amount: Decimal
currency: Currency

Money(amount=Decimal("100"), currency=Currency(symbol=""))
```

## Tests

```sh
docker/test
```
Loading

0 comments on commit 941e173

Please sign in to comment.