Skip to content

Instantly share code, notes, and snippets.

@roobert
Last active November 25, 2016 14:55
Show Gist options
  • Save roobert/34fbc56f007d3a3d2ab325b8a02c4086 to your computer and use it in GitHub Desktop.
Save roobert/34fbc56f007d3a3d2ab325b8a02c4086 to your computer and use it in GitHub Desktop.
require "awesome_print"
module Config
def self.config
@config ||= {}
end
# this allows classes which include this module to access config
def config
Config.config
end
# this allows callers to use Class.config to access config
def self.included(klass)
klass.extend Config
end
end
class App
include Config
# exposing config method as instance method
def get_config
# accessing the Config singleton directly
Config.config[:blah] = "foo"
# accessing via mixin
config
end
# changing Config module singleton class variable value
def change_config
config[:changed] = true
end
# example of doing something with the apps config from a sub module
def sub_module_method
SubModule.test
end
module SubModule
def self.test
App.config
end
end
end
a = App.new
b = App.new
ap a.get_config
a.change_config
ap b.get_config
ap a.sub_module_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment