Class: Docsmith::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/docsmith/configuration.rb

Overview

Global configuration object. Set via Docsmith.configure { |c| ... }.

Constant Summary collapse

DEFAULTS =

Gem-level defaults — final fallback in resolution order. debounce stored as Integer (seconds); Duration values normalized via .to_i at resolve time.

{
  content_field:     :body,
  content_type:      :markdown,
  auto_save:         true,
  debounce:          30,
  max_versions:      nil,
  content_extractor: nil
}.freeze
GLOBAL_KEY_MAP =

Maps ClassConfig keys to their global Configuration attribute names.

{
  content_field:     :default_content_field,
  content_type:      :default_content_type,
  auto_save:         :auto_save,
  debounce:          :default_debounce,
  max_versions:      :max_versions,
  content_extractor: :content_extractor
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/docsmith/configuration.rb', line 66

def initialize
  @default_content_field = DEFAULTS[:content_field]
  @default_content_type  = DEFAULTS[:content_type]
  @auto_save             = DEFAULTS[:auto_save]
  @default_debounce      = DEFAULTS[:debounce]
  @max_versions          = DEFAULTS[:max_versions]
  @content_extractor     = DEFAULTS[:content_extractor]
  @table_prefix          = "docsmith"
  @html_sanitizer        = nil
  @hooks                 = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#auto_saveObject

Returns the value of attribute auto_save.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def auto_save
  @auto_save
end

#content_extractorObject

Returns the value of attribute content_extractor.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def content_extractor
  @content_extractor
end

#default_content_fieldObject

Returns the value of attribute default_content_field.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def default_content_field
  @default_content_field
end

#default_content_typeObject

Returns the value of attribute default_content_type.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def default_content_type
  @default_content_type
end

#default_debounceObject

Returns the value of attribute default_debounce.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def default_debounce
  @default_debounce
end

#html_sanitizernil, ...

Controls how HtmlRenderer emits stored content for html documents.

Stored HTML is untrusted: rendering it verbatim is stored XSS whenever the content originated from a user. Docsmith ships no sanitizer of its own — safe sanitizing needs a real HTML parser, and vendoring one would break the gem's zero-system-dependency guarantee.

nil (default) — escape the content. Safe, and visibly wrong, so it gets noticed. #call(html) — your sanitizer. Rails apps already have one via ActionView: config.html_sanitizer = ->(html) { Rails::HTML5::SafeListSanitizer.new.sanitize(html) } :unsafe_raw — verbatim passthrough. Only for content you produce yourself.

Returns:

  • (nil, #call, :unsafe_raw)


64
65
66
# File 'lib/docsmith/configuration.rb', line 64

def html_sanitizer
  @html_sanitizer
end

#max_versionsObject

Returns the value of attribute max_versions.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def max_versions
  @max_versions
end

#table_prefixObject

Returns the value of attribute table_prefix.



45
46
47
# File 'lib/docsmith/configuration.rb', line 45

def table_prefix
  @table_prefix
end

Class Method Details

.resolve(class_settings, global_config) ⇒ Hash

Merge per-class settings over global config over gem defaults. Resolution is at read time — global changes after class definition still apply for keys the class does not override.

Parameters:

Returns:

  • (Hash)

    fully resolved config



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/docsmith/configuration.rb', line 97

def self.resolve(class_settings, global_config)
  DEFAULTS.each_with_object({}) do |(key, default_val), result|
    global_key = GLOBAL_KEY_MAP[key]
    global_val = global_config&.public_send(global_key)

    result[key] = if class_settings.key?(key)
                    class_settings[key]
                  elsif !global_val.nil?
                    global_val
                  else
                    default_val
                  end
  end.tap { |r| r[:debounce] = r[:debounce].to_i }
end

Instance Method Details

#hooks_for(event_name) ⇒ Array<Proc>

Parameters:

  • event_name (Symbol)

Returns:

  • (Array<Proc>)


87
88
89
# File 'lib/docsmith/configuration.rb', line 87

def hooks_for(event_name)
  @hooks[event_name]
end

#on(event_name) {|Docsmith::Events::Event| ... } ⇒ Object

Register a synchronous callback for a named event.

Parameters:

  • event_name (Symbol)

    e.g. :version_created

Yields:



81
82
83
# File 'lib/docsmith/configuration.rb', line 81

def on(event_name, &block)
  @hooks[event_name] << block
end