Skip to content

Instantly share code, notes, and snippets.

@fsubal
Last active October 13, 2026 05:14
Show Gist options
  • Select an option

  • Save fsubal/f956aed1bcbf6372ae669d64dc90c9c2 to your computer and use it in GitHub Desktop.

Select an option

Save fsubal/f956aed1bcbf6372ae669d64dc90c9c2 to your computer and use it in GitHub Desktop.
# MyClass.configure { ... } および config_accessor(:name) を提供するモジュール
#
# ```ruby
# class MyClass
# include Configurable
# config_accessor :name
# end
#
# MyClass.configure { |config| config.name = 'value' }
# MyClass.new.name # => 'value'
# ```
module Configurable
extend ActiveSupport::Concern
class_methods do
def config_accessor(*names)
names.each do |name|
delegate name, "#{name}=", to: :configuration
end
end
def configure
yield(configuration)
end
def configuration
@configuration ||= ActiveSupport::OrderedOptions.new
end
end
private
def configuration
self.class.configuration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment