source: main/waeup.sirp/trunk/src/waeup/sirp/university/coursecontainer.txt @ 6222

Last change on this file since 6222 was 6217, checked in by Henrik Bettermann, 14 years ago

Fix tests.

File size: 4.3 KB
RevLine 
[4920]1:mod:`waeup.sirp.university.coursecontainer` -- Course containers
2*****************************************************************
[4261]3
[4920]4.. module:: waeup.sirp.university.coursecontainer
[4364]5
[4261]6Containers for courses.
7
[5140]8.. :doctest:
9.. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer
[4261]10
[4364]11
12Content Classes (models and containers)
13=======================================
14
15:class:`CourseContainer`
16------------------------
17
18.. class:: CourseContainer()
19
20   Create a course container instance.
21
22   .. attribute:: grok.implements(ICourseContainer)
23
24   .. attribute:: grok.require('waeup.manageUniversity')
25
26   :class:`CourseContainer` instances are instances of
27   :class:`grok.Container` that hold and manage instances of
[4920]28   :class:`waeup.sirp.university.course.Course`.
[4364]29
[4920]30     >>> from waeup.sirp.university.coursecontainer import CourseContainer
[4364]31     >>> mycontainer = CourseContainer()
32     >>> mycontainer
[4920]33     <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...>
[4364]34
35   Course containers provide `ICourseContainer`:
36
[5005]37     >>> from waeup.sirp.university.interfaces import ICourseContainer
[4364]38     >>> ICourseContainer.providedBy(mycontainer)
39     True
40
41   We can be sure, that the full interface is supported by the
42   CourseContainer class:
43
44     >>> from zope.interface.verify import verifyClass
45     >>> verifyClass(ICourseContainer, CourseContainer)
46     True
47
48   As normal ``grok.Container``, :class:`CourseContainer` instances
49   can be used like dicts:
50
51     >>> list(mycontainer.keys())
52     []
53
54   .. method:: addCourse(course)
55
56      Add a course.
57
58      *course* must be an instance of
[4920]59      :class:`waeup.sirp.university.course.Course`:
[4364]60
[4920]61        >>> from waeup.sirp.university.coursecontainer import CourseContainer
[4364]62        >>> mycontainer = CourseContainer()
63        >>> mycontainer.addCourse('blah')
64        Traceback (most recent call last):
65        ...
66        TypeError: CourseContainers contain only ICourse instances
67
68      The key of added items is the ``code`` attribute:
69
[4920]70        >>> from waeup.sirp.university.course import Course
[4364]71        >>> mycourse = Course(code='COURSE1')
72        >>> mycontainer.addCourse(mycourse)
[6217]73        'Course added (for tests only).'
[4364]74        >>> list(mycontainer.keys())
75        [u'COURSE1']
76
77   .. method:: clear()
78
79      Remove all contained courses:
80
81        >>> len(mycontainer.items())
82        1
83
84        >>> mycontainer.clear()
85        >>> len(mycontainer.items())
86        0
87
88Utilities
89=========
90
91:class:`CourseContainerFactory`
92-------------------------------
93
94.. class:: CourseContainerFactory()
95
96   .. attribute:: grok.name(u'waeup.CourseContainer')
97
98   .. attribute:: grok.implements(IFactory)
99
100   A named utility to deliver new instances of :class:`CourseContainer`
101   without the need to import the implementation before:
102
103     >>> from zope.component import createObject
104     >>> mycoursecontainer = createObject(u'waeup.CourseContainer')
105     >>> mycoursecontainer
[4920]106     <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...>
[4364]107
108   The factory complies with the specifications from the
109   :class:`IFactory` insterface:
110
111     >>> from zope.interface.verify import verifyClass
112     >>> from zope.component.interfaces import IFactory
[4920]113     >>> from waeup.sirp.university.coursecontainer import CourseContainerFactory
[4364]114     >>> verifyClass(IFactory, CourseContainerFactory)
115     True
116
117   This means also, that we can get the interfaces of the created
118   object from the factory:
119
120     >>> coursecontainer_factory = CourseContainerFactory()
121     >>> coursecontainer_factory.getInterfaces()
[4920]122     <implementedBy waeup.sirp.university.coursecontainer.CourseContainer>
[4364]123
124
125Examples
126========
127
[4261]128Getting a course container
[4364]129--------------------------
[4261]130
131We can easily create `CourseContainers`:
132
[4920]133    >>> from waeup.sirp.university.coursecontainer import CourseContainer
[4261]134    >>> mycontainer = CourseContainer()
135
136
137Another way to get a course container -- without importing the class
138-- is via factories. We registered a factory for course containers
[4364]139under the name ``waeup.CourseContainer``. Now we can ask for an object
140by calling the appropriate factory:
[4261]141
142    >>> from zope.component import createObject
143    >>> createObject(u'waeup.CourseContainer')
[4920]144    <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...>
[4261]145
146This way we get a thing that implements ICourseContainer without
147imports or similar.
148
Note: See TracBrowser for help on using the repository browser.