Class: Coradoc::Html::ConverterBase::ConfigurationBase

Inherits:
Object
  • Object
show all
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

Spa::Configuration, Static::Configuration

Class Method Summary collapse

Instance Method Summary collapse

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(**options)
  self.class.configuration_attributes.each do |name, default|
    value = options.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_attributesObject

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

.defaultsObject



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_hObject



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