Last active
November 4, 2019 17:39
-
-
Save bill-transue/ceb18cb77223b209068cb6187da9a4b7 to your computer and use it in GitHub Desktop.
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
# Before - NoteTextStorage is | |
# tightly coupled | |
class NotePresenter | |
def initialize | |
@note_storage = NoteTextStorage.new | |
end | |
def pending_notes | |
notes = @note_storage.get_all | |
notes.select { |n| n.pending? } | |
end | |
end | |
# After - storage is passed on | |
# initialization | |
class NotePresenter | |
def initialize(storage) | |
@note_storage = storage | |
end | |
def pending_notes | |
notes = @note_storage.get_all | |
notes.select { |n| n.pending? } | |
end | |
end | |
# Dependecy Injection via dry-container | |
# and dry-auto_inject | |
class NotePresenter | |
include AutoInject['note_storage'] | |
def pending_notes | |
notes = note_storage.get_all | |
notes.select { |n| n.pending? } | |
end | |
end | |
dependency_container = Dry::Container.new | |
dependency_container.register('note_storage', -> { NoteTextStorage.new }) | |
AutoInject = Dry::AutoInject(dependency_container) | |
dependency_container = Dry::Container.new | |
dependency_container.register('note_storage', -> { TestStorage.new }) | |
AutoInject = Dry::AutoInject(dependency_container) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment