# -*- coding: utf-8 -*-
"""
Django forms data generators

"""
from django import forms
from django_any import xunit

class FormFieldDataFactory(object):
    """
    Registry storage for form field data functions

    Works like one parameter multimethod
    """
    def __init__(self):
        self.registry = {}

    def register(self, field_type, impl=None):
        """
        Register form field data function.
        
        Could be used as decorator
        """
        def _wrapper(func):
            self.registry[field_type] = func
            return func

        if impl:
            return _wrapper(func)
        return _wrapper
    
    def decorator(self, impl=None):
        """
        Decorator for register decorators
        """
        self.__call__ = impl(self.__call__)

    def __call__(self, *args, **kwargs):
        if not len(args):
            raise TypeError('Field instance are not provided')

        field_type = args[0].__class__

        function = self.registry.get(field_type)

        if function is None:
            raise TypeError("no match %s" % field_type)

        return function(*args, **kwargs)

any_form_field = FormFieldDataFactory()


@any_form_field.register(forms.BooleanField)
def boolean_field_data(field, **kwargs):
    """
    Return random value for BooleanField

    >>> result = any_form_field(forms.BooleanField())
    >>> type(result)
    <type 'bool'>
    """
    return xunit.any_boolean()


@@ -55,16 +55,30 @@
 rs%0A%0A%22%22%22%0A
+import random%0A
 from dja
@@ -681,29 +681,24 @@
 r(self, impl
-=None
 ):%0A        %22
@@ -797,16 +797,36 @@
 _call__)
+%0A        return impl
 %0A%0A    de
@@ -1168,16 +1168,16 @@
 wargs)%0A%0A
-
 any_form
@@ -1210,16 +1210,442 @@
 ory()%0A%0A%0A
+@any_form_field.decorator%0Adef field_required_attribute(function):%0A    %22%22%22%0A    Sometimes return None if field is not required%0A%0A    %3E%3E%3E result = any_form_field(forms.BooleanField(required=False))%0A    %3E%3E%3E result in %5BNone, True, False%5D%0A    True%0A    %22%22%22%0A    def _wrapper(field, **kwargs):%0A        if not field.required and random.random %3C 0.1:%0A            return None%0A        return function(field, **kwargs)%0A    return _wrapper%0A%0A%0A
 @any_for

Handle non required form fields