Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created May 25, 2016 18:28
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 iloveitaly/cf504c4710286b3850801264279e0b79 to your computer and use it in GitHub Desktop.
Save iloveitaly/cf504c4710286b3850801264279e0b79 to your computer and use it in GitHub Desktop.
Easily setup class-level configurations with accessors and defaults
module ClassConfiguration
extend ActiveSupport::Concern
module ClassMethods
def class_configuration(key, default = nil)
class_attribute :"_#{key}"
define_singleton_method(key) do |*args|
if args.empty?
return self.send(:"_#{key}") || default
end
self.send(:"_#{key}=", args.first)
end
define_method(key) do
self.send(:"_#{key}") || default
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment