Last active
October 13, 2026 05:14
-
-
Save fsubal/f956aed1bcbf6372ae669d64dc90c9c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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