Module: Docscribe::CLI::ConfigBuilder

Defined in:
lib/docscribe/cli/config_builder.rb

Overview

Build and override effective config from CLI flags.

Class Method Summary collapse

Class Method Details

.apply_file_filters(raw, options) ⇒ void

Note:

module_function: when included, also defines #apply_file_filters (instance visibility: private)

This method returns an undefined value.

Merge CLI file include/exclude patterns into the raw config hash.

Parameters:

  • raw (Hash)

    raw config hash

  • options (Hash)

    parsed CLI options



109
110
111
112
113
# File 'lib/docscribe/cli/config_builder.rb', line 109

def apply_file_filters(raw, options)
  files = raw['filter']['files'] ||= {} #: Hash[String, untyped]
  files['include'] = Array(files['include']) + options[:include_file]
  files['exclude'] = Array(files['exclude']) + options[:exclude_file]
end

.apply_filter_overrides(raw, options) ⇒ void

Note:

module_function: when included, also defines # (instance visibility: private)

This method returns an undefined value.

Apply method and file filter overrides to the raw config.

Parameters:

  • raw (Hash)

    raw config hash

  • options (Hash)

    parsed CLI options



86
87
88
89
# File 'lib/docscribe/cli/config_builder.rb', line 86

def apply_filter_overrides(raw, options)
  apply_method_filters(raw, options)
  apply_file_filters(raw, options)
end

.apply_method_filters(raw, options) ⇒ void

Note:

module_function: when included, also defines #apply_method_filters (instance visibility: private)

This method returns an undefined value.

Merge CLI method include/exclude patterns into the raw config hash.

Parameters:

  • raw (Hash)

    raw config hash

  • options (Hash)

    parsed CLI options



97
98
99
100
101
# File 'lib/docscribe/cli/config_builder.rb', line 97

def apply_method_filters(raw, options)
  raw['filter'] ||= {}
  raw['filter']['include'] = Array(raw['filter']['include']) + options[:include]
  raw['filter']['exclude'] = Array(raw['filter']['exclude']) + options[:exclude]
end

.apply_rbs_collection(raw) ⇒ void

Note:

module_function: when included, also defines #apply_rbs_collection (instance visibility: private)

This method returns an undefined value.

Resolve and apply the RBS collection path into the raw config hash.

Parameters:

  • raw (Hash)

    raw config hash



137
138
139
140
141
142
143
144
145
146
# File 'lib/docscribe/cli/config_builder.rb', line 137

def apply_rbs_collection(raw)
  require 'docscribe/types/rbs/collection_loader'
  collection_path = Docscribe::Types::RBS::CollectionLoader.resolve
  if collection_path
    raw['rbs']['collection_dirs'] = Array(raw['rbs']['collection_dirs']) + [collection_path]
  else
    warn 'Docscribe: rbs_collection.lock.yaml not found or collection not installed. ' \
         'Run `bundle exec rbs collection install` first.'
  end
end

.apply_rbs_overrides(raw, options) ⇒ void

Note:

module_function: when included, also defines # (instance visibility: private)

This method returns an undefined value.

Apply RBS-related CLI overrides to the raw config.

Parameters:

  • raw (Hash)

    raw config hash

  • options (Hash)

    parsed CLI options



122
123
124
125
126
127
128
129
130
# File 'lib/docscribe/cli/config_builder.rb', line 122

def apply_rbs_overrides(raw, options)
  raw['rbs'] ||= {}
  raw['rbs']['enabled'] = true
  raw['rbs']['sig_dirs'] = Array(raw['rbs']['sig_dirs']) + options[:sig_dirs] if options[:sig_dirs].any?

  return unless options[:rbs_collection]

  apply_rbs_collection(raw)
end

.apply_sorbet_overrides(raw, options) ⇒ void

Note:

module_function: when included, also defines # (instance visibility: private)

This method returns an undefined value.

Apply Sorbet-related CLI overrides to the raw config.

Parameters:

  • raw (Hash)

    raw config hash

  • options (Hash)

    parsed CLI options



155
156
157
158
159
160
161
# File 'lib/docscribe/cli/config_builder.rb', line 155

def apply_sorbet_overrides(raw, options)
  raw['sorbet'] ||= {}
  raw['sorbet']['enabled'] = true
  return unless options[:rbi_dirs].any?

  raw['sorbet']['rbi_dirs'] = Array(raw['sorbet']['rbi_dirs']) + options[:rbi_dirs]
end

.build(base, options) ⇒ Docscribe::Config

Note:

module_function: when included, also defines #build (instance visibility: private)

Build an effective config by applying CLI overrides on top of a base config.

CLI overrides currently affect:

  • method/file include and exclude filters

  • RBS enablement and additional signature directories

  • Sorbet enablement and RBI directories

If no relevant CLI override is present, the original config is returned unchanged.

Parameters:

  • base (Docscribe::Config)

    base config loaded from YAML/defaults

  • options (Hash)

    parsed CLI options

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/docscribe/cli/config_builder.rb', line 24

def build(base, options)
  return base unless needs_override?(options)

  raw = Marshal.load(Marshal.dump(base.raw))
  apply_filter_overrides(raw, options)
  apply_rbs_overrides(raw, options) if options[:rbs] || options[:rbs_collection] || options[:sig_dirs].any?
  apply_sorbet_overrides(raw, options) if options[:sorbet] || options[:rbi_dirs].any?
  Docscribe::Config.new(raw)
end

.filter_overrides?(options) ⇒ Boolean

Note:

module_function: when included, also defines #filter_overrides? (instance visibility: private)

Whether any method or file filter CLI options were provided.

Parameters:

  • options (Hash)

    parsed CLI options

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/docscribe/cli/config_builder.rb', line 51

def filter_overrides?(options)
  options[:include].any? ||
    options[:exclude].any?      ||
    options[:include_file].any? ||
    options[:exclude_file].any?
end

.needs_override?(options) ⇒ Boolean

Note:

module_function: when included, also defines # (instance visibility: private)

Whether any CLI override is present.

Parameters:

  • options (Hash)

    parsed CLI options

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/docscribe/cli/config_builder.rb', line 40

def needs_override?(options)
  filter_overrides?(options) ||
    rbs_overrides?(options) ||
    sorbet_overrides?(options)
end

.rbs_overrides?(options) ⇒ Boolean

Note:

module_function: when included, also defines #rbs_overrides? (instance visibility: private)

Whether any RBS-related CLI options were provided.

Parameters:

  • options (Hash)

    parsed CLI options

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/docscribe/cli/config_builder.rb', line 63

def rbs_overrides?(options)
  options[:rbs] ||
    options[:rbs_collection] ||
    options[:sig_dirs].any?
end

.sorbet_overrides?(options) ⇒ Boolean

Note:

module_function: when included, also defines #sorbet_overrides? (instance visibility: private)

Whether any Sorbet-related CLI options were provided.

Parameters:

  • options (Hash)

    parsed CLI options

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/docscribe/cli/config_builder.rb', line 74

def sorbet_overrides?(options)
  options[:sorbet] ||
    options[:rbi_dirs].any?
end