Skip to content

Commit

Permalink
Minor fixes to container
Browse files Browse the repository at this point in the history
  • Loading branch information
Suszyński Krzysztof committed Jul 10, 2017
1 parent 186cd92 commit 8ae340b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
47 changes: 20 additions & 27 deletions puppeter/container.py
Expand Up @@ -4,39 +4,34 @@ class NamedBean(cls):
def __init__(self, *args):
self.wrapped = cls(*args)

def get_bean_name(self):
def bean_name(self):
return bean_name

def __repr__(self):
return '@Named(\'%s\') %s' % (bean_name, repr(self.wrapped))

def __getattr__(self, name):
return getattr(self.wrapped, name)
return NamedBean
return named_decorator


def bind(cls, impl_cls):
try:
lst = __beans[cls]
except:
lst = []
__beans[cls] = lst
lst.append(__Bean(cls, impl_cls=impl_cls))
beans = __get_all_beans(cls)
beans.append(__Bean(cls, impl_cls=impl_cls))


def bind_to_instance(cls, impl):
try:
lst = __beans[cls]
except:
lst = []
__beans[cls] = lst
lst.append(__Bean(cls, impl=impl))
beans = __get_all_beans(cls)
beans.append(__Bean(cls, impl=impl))


def get_all(cls):
beans = __get_all_beans(cls)
try:
beans = __beans[cls]
return list(map(lambda bean: bean.impl(), beans))
except:
return []
return tuple(map(lambda bean: bean.impl(), beans))
except Exception:
return tuple()


def get(cls):
Expand All @@ -63,8 +58,10 @@ def get_named(cls, bean_name):
def __get_all_beans(cls):
try:
return __beans[cls]
except:
return []
except KeyError:
lst = []
__beans[cls] = lst
return lst


class __Bean:
Expand All @@ -79,13 +76,9 @@ def impl_cls_name(self):
return self.__impl_cls

def name(self):
if self.__impl is not None:
return self.__impl.get_bean_name()
else:
return self.__impl_cls().get_bean_name()
return self.impl().bean_name()

def impl(self):
if self.__impl is not None:
return self.__impl
else:
return self.__impl_cls()
if self.__impl is None:
self.__impl = self.__impl_cls()
return self.__impl
1 change: 0 additions & 1 deletion puppeter/domain/__init__.py
@@ -1 +0,0 @@
PRELOADED = True
4 changes: 3 additions & 1 deletion puppeter/domain/model/installer.py
Expand Up @@ -13,9 +13,11 @@ def __init__(self):
self.__mode = Mode.Agent

def raw_options(self):
# noinspection PyUnresolvedReferences
installer_type = self.bean_name()
return {
'mode': self.__mode.name,
'type': self.get_bean_name()
'type': installer_type
}

def read_raw_options(self, options):
Expand Down
1 change: 0 additions & 1 deletion puppeter/persistence/__init__.py
@@ -1 +0,0 @@
PRELOADED = True
1 change: 0 additions & 1 deletion puppeter/presentation/__init__.py
@@ -1 +0,0 @@
PRELOADED = True
17 changes: 15 additions & 2 deletions tests/domain/model/test_installer.py
Expand Up @@ -3,7 +3,7 @@ def test_rubygems_installer_named_bean():
from puppeter.domain.model.installer import RubygemsInstaller
installer = RubygemsInstaller()
# when
bean_name = installer.get_bean_name()
bean_name = installer.bean_name()
# then
assert bean_name is 'gem'

Expand Down Expand Up @@ -60,4 +60,17 @@ def test_getting_impl_from_container():

# then
assert installer.mode() is Mode.Agent
assert installer.get_bean_name() is 'pc3x'
assert installer.bean_name() is 'pc3x'


def test_getting_all_impls_from_container():
# given
from puppeter.domain.model.installer import Installer, Collection5xInstaller
from puppeter import container

# when
installers = container.get_all(Installer)

# then
assert len(installers) >= 4
assert len(tuple(filter(lambda cls: isinstance(cls, Collection5xInstaller), installers))) == 1

0 comments on commit 8ae340b

Please sign in to comment.