Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/3af245a5455f8a2a17d7 to your computer and use it in GitHub Desktop.
Save krisleech/3af245a5455f8a2a17d7 to your computer and use it in GitHub Desktop.
# DoSomething has dependency on Configuration and pulls default from Configuration
#
module MyThingy
class DoSomething
def call(input, suffix = nil)
input + (suffix || default_suffix)
end
private
def default_suffix
MyThing.configuration.default_suffix
end
end
def configuration
@configuration ||= Configuration.new
end
class Configuration
attr_accessor :default_suffix
def default_suffix
@default_suffix ||= "_world"
end
end
end
# Configuration has dependency on DoSomething and pushes default to DoSomething
#
module MyThingy
class DoSomething
def call(input, suffix = nil)
input + (suffix || default_suffix)
end
class << self
attr_accessor :default_suffix
end
private
def default_suffix
self.class.default_suffix || '_world'
end
end
def configuration
@configuration ||= Configuration.new
end
class Configuration
attr_accessor :default_suffix
def default_suffix=(new_suffix)
DoSomething.default_suffix = new_suffix
end
end
end
MyThingy.configuration.default_suffix = '_planet'
command = MyThing::DoSomething.new
command.call("mars") # => "mars_planet"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment