Skip to content

Commit

Permalink
new wrap operation with support for building
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Mar 17, 2015
1 parent a7f4f24 commit c77f6ab
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/quorum/model.py
Expand Up @@ -294,6 +294,38 @@ def old(cls, model = None, form = True, safe = True, build = False):
new = False
)

@classmethod
def wrap(cls, models, build = True):
"""
"Wraps" the provided sequence (or single set) of model based data into a
sequence of models (or a single model) so that proper business logic may
be used for operations on top of that data.
This operation is specially useful for api based environments where client
side business logic is meant to be added to the static data.
:type models: List
:param models: Sequence (or single) set of models that are going to be wrapped
around instances of the current class.
:type build: bool
:param build: If the "custom" build operation should be performed after
the wrap operation is performed so that new custom attributes may be
injected into the resulting instance.
:rtype: List
:return: The sequence of models (or single model) representing the provided
set of dictionary models that were sent as arguments.
"""

is_sequence = type(models) in (list, tuple)
if not is_sequence: models = [models]
wrapping = []
for model in models:
_model = cls(model = model)
build and cls.build(_model.model, map = False)
wrapping.append(_model)
if is_sequence: return wrapping
else: return wrapping[0]

@classmethod
def singleton(
cls,
Expand Down

0 comments on commit c77f6ab

Please sign in to comment.