Class: Coradoc::Html::ConverterBase::ConfigurationBase
- Inherits:
-
Object
- Object
- Coradoc::Html::ConverterBase::ConfigurationBase
- Defined in:
- lib/coradoc/html/converter_base.rb
Overview
Base class for output converter configurations.
Provides shared ‘merge` and `defaults` patterns. Subclasses define `initialize`, `to_h`, and `validate!`.
Direct Known Subclasses
Class Method Summary collapse
-
.attribute(name, default: nil) ⇒ Object
Declare a configuration attribute with an optional default.
-
.configuration_attributes ⇒ Object
Registry of declared attributes and their defaults.
- .defaults ⇒ Object
Instance Method Summary collapse
-
#initialize(**options) ⇒ ConfigurationBase
constructor
A new instance of ConfigurationBase.
- #merge(other) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(**options) ⇒ ConfigurationBase
Returns a new instance of ConfigurationBase.
50 51 52 53 54 55 |
# File 'lib/coradoc/html/converter_base.rb', line 50 def initialize(**) self.class.configuration_attributes.each do |name, default| value = .fetch(name) { default.respond_to?(:call) ? default.call : default } public_send(:"#{name}=", value) end end |
Class Method Details
.attribute(name, default: nil) ⇒ Object
Declare a configuration attribute with an optional default. Replaces manual attr_accessor + initialize + to_h boilerplate.
34 35 36 37 38 |
# File 'lib/coradoc/html/converter_base.rb', line 34 def attribute(name, default: nil) attr_accessor name configuration_attributes[name] = default end |
.configuration_attributes ⇒ Object
Registry of declared attributes and their defaults
41 42 43 |
# File 'lib/coradoc/html/converter_base.rb', line 41 def configuration_attributes @configuration_attributes ||= {} end |
.defaults ⇒ Object
45 46 47 |
# File 'lib/coradoc/html/converter_base.rb', line 45 def defaults new end |
Instance Method Details
#merge(other) ⇒ Object
63 64 65 66 |
# File 'lib/coradoc/html/converter_base.rb', line 63 def merge(other) other_hash = other.is_a?(self.class) ? other.to_h : other.to_h.transform_keys(&:to_sym) self.class.new(**to_h, **other_hash) end |
#to_h ⇒ Object
57 58 59 60 61 |
# File 'lib/coradoc/html/converter_base.rb', line 57 def to_h self.class.configuration_attributes.each_with_object({}) do |(name, _), hash| hash[name] = public_send(name) end end |