Skip to content

Instantly share code, notes, and snippets.

@bill-transue
Last active November 4, 2019 17:39
Show Gist options
  • Save bill-transue/ceb18cb77223b209068cb6187da9a4b7 to your computer and use it in GitHub Desktop.
Save bill-transue/ceb18cb77223b209068cb6187da9a4b7 to your computer and use it in GitHub Desktop.
# 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