Skip to content

Documented every method #7

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

# General information about the project.
project = u'REST ORM'
copyright = u'2016, Colton Allen'
copyright = u'Code Axis Inc. d/b/a Caxiam'
author = u'Colton Allen'

# The version info for the project you're documenting, acts as replacement for
Expand Down
18 changes: 18 additions & 0 deletions rest_orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def deserialize(self, data):
return value

def _deserialize(self, value):
"""Return the value as it was initially parsed."""
return value

def _validate(self, value):
"""Call the validate function reference if it exists."""
if self.validate is not None:
self.validate(value)
return None
Expand All @@ -81,49 +83,63 @@ class AdaptedBoolean(AdaptedField):
"""Parse an adapted field into the boolean type."""

def _deserialize(self, value):
"""Return boolean value."""
return bool(value)


class AdaptedDate(AdaptedField):
"""Parse an adapted field into the datetime type."""

def __init__(self, *args, **kwargs):
"""Parse an adapted field into the datetime type.

:param date_format: A valid strptime string format.
"""
self.date_format = kwargs.pop('date_format', '%Y-%m-%d')
super(AdaptedDate, self).__init__(*args, **kwargs)

def _deserialize(self, value):
"""Return datetime value."""
return datetime.strptime(value, self.date_format)


class AdaptedDecimal(AdaptedField):
"""Parse an adapted field into the decimal type."""

def _deserialize(self, value):
"""Return decimal value."""
return Decimal(value)


class AdaptedInteger(AdaptedField):
"""Parse an adapted field into the integer type."""

def _deserialize(self, value):
"""Return integer value."""
return int(value)


class AdaptedFunction(AdaptedField):
"""Parse an adapted field into a specified function's output."""

def __init__(self, f, *args, **kwargs):
"""Parse an adapted field into a specified function's output.

:param f: Function reference.
"""
self.f = f
super(AdaptedFunction, self).__init__(*args, **kwargs)

def _deserialize(self, value):
"""Return value of a function."""
return self.f(value)


class AdaptedList(AdaptedField):
"""Parse an adapted field into the list type."""

def _deserialize(self, value):
"""Return list value."""
if not isinstance(value, list):
return [value]
return value
Expand All @@ -148,6 +164,7 @@ def model(self):
return self.nested_model

def _deserialize(self, value):
"""Return AdaptedModel value."""
if isinstance(value, list):
return [self.model().load(val) for val in value]
return self.model().load(value)
Expand All @@ -157,4 +174,5 @@ class AdaptedString(AdaptedField):
"""Parse an adapted field into the string type."""

def _deserialize(self, value):
"""Return string value."""
return str(value)
4 changes: 4 additions & 0 deletions rest_orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def post_load(self):
pass

def _do_load(self, data):
"""Replace `AdaptedField`s with appropriately typed members.

:param data: Parsed dictionary output of JSON response data.
"""
for field_name in dir(self):
field = getattr(self, field_name)
if not isinstance(field, AdaptedField):
Expand Down
3 changes: 3 additions & 0 deletions rest_orm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class ModelRegistry(type):
"""Meta class for used to register classes within the registry."""

registry = {}

def __new__(cls, name, bases, attrs):
Expand All @@ -11,4 +13,5 @@ def __new__(cls, name, bases, attrs):


def get_class(name):
"""Return class reference from class name."""
return ModelRegistry.registry[name]