Skip to content

Commit

Permalink
Introduce camel_case_name property in Domain
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Jul 12, 2024
1 parent b782f13 commit 2605387
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/protean/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from uuid import uuid4

from inflection import parameterize, transliterate, underscore
from inflection import parameterize, titleize, transliterate, underscore

from protean.adapters import Brokers, Caches, EmailProviders, Providers
from protean.adapters.event_store import EventStore
Expand Down Expand Up @@ -189,6 +189,23 @@ def __init__(
# FIXME Should all protean elements be subclassed from a base element?
self._pending_class_resolutions: dict[str, Any] = defaultdict(list)

@property
@lru_cache()
def camel_case_name(self) -> str:
"""Return the CamelCase name of the domain.
The CamelCase name is the name of the domain with the first letter capitalized.
Examples:
- `my_domain` -> `MyDomain`
- `my_domain_1` -> `MyDomain1`
- `my_domain_1_0` -> `MyDomain10`
"""
# Transliterating the name to remove any special characters and camelize
formatted_string = titleize(transliterate(self.name).replace("-", " "))

# Eliminate non-alphanumeric characters
return "".join(filter(str.isalnum, formatted_string))

@property
@lru_cache()
def normalized_name(self) -> str:
Expand Down
27 changes: 26 additions & 1 deletion tests/domain/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_domain_name_string():


def test_normalized_domain_name():
# A few test cases to check if domain names are normalized correctly
# Test cases to check if domain names are normalized correctly
# Each item is a tuple of (name, normalized_name)
data = [
("Foo", "foo"),
Expand All @@ -50,6 +50,31 @@ def test_normalized_domain_name():
assert domain.normalized_name == result, f"Failed for {name}"


def test_camel_case_domain_name():
# Test cases to check if domain names are converted to camel case correctly
# Each item is a tuple of (name, camel_case_name)
data = [
("My Domain", "MyDomain"),
("my_domain", "MyDomain"),
("my-domain", "MyDomain"),
("foo", "Foo"),
("foo_bar", "FooBar"),
("foo_bar_baz", "FooBarBaz"),
("foo-bar-baz", "FooBarBaz"),
("foo-bar", "FooBar"),
("foo_bar_baz", "FooBarBaz"),
("foo-bar-baz", "FooBarBaz"),
("donald_e_knuth", "DonaldEKnuth"),
("donald-e-knuth", "DonaldEKnuth"),
("Donald E. Knuth", "DonaldEKnuth"),
("My Domain 1", "MyDomain1"),
("My Domain 1.0", "MyDomain10"),
]
for name, result in data:
domain = Domain(__file__, name, load_toml=False)
assert domain.camel_case_name == result, f"Failed for {name}"


class TestElementRegistration:
def test_that_only_recognized_element_types_can_be_registered(self, test_domain):
with pytest.raises(NotSupportedError) as exc:
Expand Down

0 comments on commit 2605387

Please sign in to comment.