Skip to content

Commit

Permalink
Merge pull request #149 from MasoniteFramework/add-subclass-collection
Browse files Browse the repository at this point in the history
added collection of subclasses to container
  • Loading branch information
josephmancuso committed May 27, 2018
2 parents c0b4dd9 + 1e93b7a commit e165ac7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
40 changes: 23 additions & 17 deletions masonite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,30 @@ def resolve(self, obj):
return obj(*provider_list)

def collect(self, search):
# Search Can Be:
# '*ExceptionHook'
# 'Sentry*'
# 'Sentry*Hook'
provider_list = {}
for key, value in self.providers.items():
if search.startswith('*'):
if key.endswith(search.split('*')[1]):
provider_list.update({key: value})
elif search.endswith('*'):
if key.startswith(search.split('*')[0]):
provider_list.update({key: value})
elif '*' in search:
split_search = search.split('*')
if key.startswith(split_search[0]) and key.endswith(split_search[1]):
provider_list.update({key: value})
else:
raise AttributeError("There is no '*' in your collection search")
if isinstance(search, str):
# Search Can Be:
# '*ExceptionHook'
# 'Sentry*'
# 'Sentry*Hook'
for key, value in self.providers.items():
if search.startswith('*'):
if key.endswith(search.split('*')[1]):
provider_list.update({key: value})
elif search.endswith('*'):
if key.startswith(search.split('*')[0]):
provider_list.update({key: value})
elif '*' in search:
split_search = search.split('*')
if key.startswith(split_search[0]) and key.endswith(split_search[1]):
provider_list.update({key: value})
else:
raise AttributeError("There is no '*' in your collection search")
else:
for provider_key, provider_class in self.providers.items():
if inspect.isclass(provider_class) and issubclass(provider_class, search):
provider_list.update({provider_key: provider_class})

return provider_list

def _find_parameter(self, parameter):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'masonite.contracts',
'masonite.helpers',
],
version='1.6.9',
version='1.6.10',
install_requires=[
'validator.py==1.2.5',
'cryptography==2.2.2',
Expand Down
19 changes: 7 additions & 12 deletions tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,10 @@ def test_container_collects_correct_objects(self):
assert self.app.collect('Sentry*Hook') == {'SentryExceptionHook': object}
with pytest.raises(AttributeError):
self.app.collect('Sentry')













def test_container_collects_correct_subclasses_of_objects(self):
self.app.bind('GetAnotherObject', GetAnotherObject)
objects = self.app.collect(MockObject)

assert 'GetAnotherObject' in objects
assert 'GetObject' in objects

0 comments on commit e165ac7

Please sign in to comment.