Skip to content

Commit ae29192

Browse files
author
Kenneth Reitz
committed
awesome pattern!
1 parent 22d4662 commit ae29192

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

github3/models.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
github3.models
3+
~~~~~~~~~~~~~~
4+
5+
This module provides the Github3 object model.
6+
"""
7+
8+
from .helpers import to_python, to_api
9+
10+
11+
class BaseResource(object):
12+
"""A BaseResource object."""
13+
14+
_strings = []
15+
_ints = []
16+
_datetimes = []
17+
_booleans = []
18+
_map = {}
19+
20+
21+
def __init__(self):
22+
self._bootstrap()
23+
super(BaseResource, self).__init__()
24+
25+
26+
def __dir__(self):
27+
d = self.__dict__.copy()
28+
29+
try:
30+
del d['_gh']
31+
except KeyError:
32+
pass
33+
34+
return d.keys()
35+
36+
37+
def _bootstrap(self):
38+
"""Bootstraps the model object based on configured values."""
39+
40+
for attr in (self._strings + self._ints + self._datetimes + self._booleans + self._map.keys()):
41+
setattr(self, attr, None)
42+
43+
@classmethod
44+
def new_from_dict(cls, d, gh=None):
45+
46+
return to_python(
47+
obj=cls(), in_dict=d,
48+
string_keys = cls._strings,
49+
date_keys = cls._datetimes,
50+
_gh = gh
51+
)
52+
53+
54+
class User(BaseResource):
55+
"""Github User object model."""
56+
57+
_strings = ['login', 'gravatar_url', 'url', 'name', 'company',
58+
'blog', 'location', 'email', 'bio', 'html_url']
59+
60+
_ints = ['id', 'public_repos', 'public_gists', 'followers', 'following']
61+
_datetimes = ['created_at',]
62+
_booleans = ['hireable', ]
63+
_map = {}
64+
65+
66+
def __init__(self):
67+
super(User, self).__init__()
68+
69+
def __repr__(self):
70+
return '<user {0}>'.format(self.login)

0 commit comments

Comments
 (0)