Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Created November 24, 2018 18:27
Show Gist options
  • Save joegaudet/02e80edc890a9d0642bf1d2f98596e72 to your computer and use it in GitHub Desktop.
Save joegaudet/02e80edc890a9d0642bf1d2f98596e72 to your computer and use it in GitHub Desktop.
configure_protocol
# If a dependency exposes the interface #configure, call it on instantiation with with the dependant
# example
class Utils
class Logger
attr_accessor :log_subject
dependency :logger, Logger, instance: Rails.logger
def configure(log_subject)
@log_subkect = log_subject.class.name.dasherize
end
def info(msg)
logger.info "[#{@log_subject}] #{msg}"
end
def debug(msg)
logger.debug "[#{@log_subject}] #{msg}"
end
def trace(msg)
logger.trace "[#{@log_subject}] #{msg}"
end
def error(msg)
logger.error "[#{@log_subject}] #{msg}"
end
end
end
module DependencySupport
def dependency(name, klass)
# normal dependency initialization code
# if the dependency implements the configuration protocol, configure it.
dependency_instance.configure(self) if dependency_instance.respond_to? :configre
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment