Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,43 @@ It helps you to create objects between project layers (data layer, service layer

**Note:** You can find more examples in tests package

5. **Mapping with nested objects**

Suppose you have two classes `Person` and `MyPerson`, each one of them contains an `address` field of types `Address` and `MyAdress` respectively. And you want to map from `Person` to `MyPerson`. Initialization of the `ObjectMapper` will be:

```
class MyPerson:
def __init__(self):
self.address = None
class MyAdress:
def __init__(self):
self.city = None
self.street = None
class Address:
def __init__(self):
self.line = "Amman-Maka Street"
class Person:
def __init__(self):
self.address = Address()

mapper = ObjectMapper()
mapper.create_map(
Address,
MyAdress,
{
'city': lambda add: add.line.split('-')[0],
'street': lambda add: add.line.split('-')[1]
}
)
mapper.create_map(Person, MyPerson)
person = mapper.map(Person(), MyPerson)
assert type(person.address) is MyAdress
assert '{}-{}'.format(person.address.city, person.address.street) == Person().address.line
```

## Installation

* Download this project
* Download from Pypi: https://pypi.python.org/pypi/object-mapper

### ENJOY IT!
### ENJOY IT!