Last active
June 18, 2020 02:48
-
-
Save davich/69d92bf24856a51744d8a97b214c5569 to your computer and use it in GitHub Desktop.
Which dependencies to inject?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InjectMe | |
def initialize( | |
kernel:, | |
name_utils_class:, | |
text_helper_class:, | |
email_class:, | |
car:, | |
pet:, | |
payment_processor:, | |
random_class:, | |
) | |
@kernel = kernel | |
@name_utils_class = name_utils_class | |
@text_helper_class = text_helper_class | |
@email_class = email_class | |
@pet = pet | |
@payment_processor = payment_processor | |
@random_class = random_class | |
end | |
def log_something | |
@kernel.puts "hello" | |
end | |
# class you instantiate | |
def reverse_name | |
@name_utils_class.new("my name").reverse | |
end | |
# class with static method | |
def format_address | |
@text_helper_class.format_address("2 new st, Canberra") | |
end | |
# Using constant from class | |
def valid_email?(email) | |
email.match(@email_class::VALID_EMAIL_REGEX) | |
end | |
# instance of a class with no state | |
def num_wheels | |
@car.default_wheels | |
end | |
# instance of a class that holds some state (that's not specific to this class) | |
def pet_sounds | |
@pet.sound | |
end | |
# instance that hits external system | |
def pay | |
@payment_processor.process_payment | |
end | |
# Standard ruby class | |
def uuid | |
@random_class.uuid | |
end | |
end | |
InjectMe.new( | |
kernel: Kernel, | |
name_utils_class: NameUtils, | |
text_helper_class: TextHelper, | |
email_class: EmailUtils, | |
car: Car.new, | |
pet: Pet.new("dog"), | |
payment_processor: Paypal.new, | |
random_class: SecureRandom, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment