Class: Bridgetown::Configuration::ConfigurationDSL

Inherits:
FrontMatter::RubyFrontMatter show all
Defined in:
bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FrontMatter::RubyFrontMatter

#each, #initialize, #to_h

Constructor Details

This class inherits a constructor from Bridgetown::FrontMatter::RubyFrontMatter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *value, &block) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



82
83
84
85
86
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 82

def method_missing(key, *value, &block) # rubocop:disable Style/MissingRespondToMissing
  return get(key) if value.empty? && block.nil?

  set(key, value[0], &block)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 6

def context
  @context
end

Instance Method Details

#_in_require_denylist?(name) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 148

def _in_require_denylist?(name)
  REQUIRE_DENYLIST.include?(name.to_sym)
end

#_run_builtins!Object

Initializers that are part of the Bridgetown boot sequence. Site owners can override defaults by running any of these manually…init is no-op if the initializer was already run.



154
155
156
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 154

def _run_builtins!
  init :streamlined
end

#_setup_initializer(name:, require_gem:, require_initializer:) ⇒ Bridgetown::Configuration::Initializer



137
138
139
140
141
142
143
144
145
146
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 137

def _setup_initializer(name:, require_gem:, require_initializer:)
  Bridgetown::PluginManager.require_gem(name) if require_gem && !_in_require_denylist?(name)

  if require_initializer
    init_file_name = File.join(@scope.root_dir, "config", "#{name}.rb")
    load(init_file_name) if File.exist?(init_file_name)
  end

  @scope.initializers[name.to_sym]
end

#builder(klass = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 61

def builder(klass = nil, &)
  return klass.register if klass.is_a?(Class) && klass < Bridgetown::Builder

  unless klass.is_a?(Symbol)
    raise "You must supply a constant symbol to register an inline builder"
  end

  Object.const_set(
    klass, Class.new(Bridgetown::Builder, &).tap(&:register)
  )
end

#except(*context) ⇒ Object



42
43
44
45
46
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 42

def except(*context, &)
  return if context.any? { _1 == @context }

  instance_exec(&)
end

#get(key) ⇒ Object



88
89
90
91
92
93
94
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 88

def get(key)
  unless @data.key?(key)
    Bridgetown.logger.debug("Initializing:", "Uh oh, missing key `#{key}' in configuration")
  end

  super
end

#hook(owner, event, priority: Bridgetown::Hooks::DEFAULT_PRIORITY) ⇒ Object



48
49
50
51
52
53
54
55
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 48

def hook(
  owner,
  event,
  priority: Bridgetown::Hooks::DEFAULT_PRIORITY,
  &
)
  Bridgetown::Hooks.register_one(owner, event, priority:, reloadable: false, &)
end

#init(name, require_gem: true, require_initializer: true, **kwargs, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 9

def init(name, require_gem: true, require_initializer: true, **kwargs, &block) # rubocop:todo Metrics
  return if @scope.initializers.key?(name.to_sym) &&
    @scope.initializers[name.to_sym].completed

  initializer = _setup_initializer(
    name:, require_gem:, require_initializer:
  )

  return unless initializer.nil? || initializer.completed == false

  set :init_params do
    block ? set(name, &block) : set(name, kwargs)
  end

  if initializer.nil?
    Bridgetown.logger.warn("Initializing:",
                           "The `#{name}' initializer could not be found")
    return
  end

  Bridgetown.logger.debug "Initializing:", name
  @scope.initializers[name.to_sym].block.(
    self, **@scope.init_params[name].transform_keys(&:to_sym)
  )
  initializer.completed = true
end

#only(*context) ⇒ Object



36
37
38
39
40
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 36

def only(*context, &)
  return unless context.any? { _1 == @context }

  instance_exec(&)
end

#reflect(name, require_gem: true, require_initializer: true) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 113

def reflect(name, require_gem: true, require_initializer: true)
  initializer = _setup_initializer(
    name:, require_gem:, require_initializer:
  )

  if initializer.nil?
    Bridgetown.logger.info("Reflection:",
                           "The `#{name}' initializer could not be found")
    return
  end

  Bridgetown.logger.info(
    "Reflection:",
    "The #{name.to_s.yellow} initializer accepts the following options:"
  )
  initializer.block.parameters.each do |param|
    next if param[0] == :opt

    Bridgetown.logger.info("",
                           "* #{param[1].to_s.cyan}#{" (required)" if param[0] == :keyreq}")
  end
end

#roda(&block) ⇒ Object



73
74
75
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 73

def roda(&block)
  @scope.roda_initializers << block
end

#set(key, value = nil, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 96

def set(key, value = nil, &block)
  # Handle nested data within a block
  if block
    value = self.class.new(scope: @scope).tap do |fm|
      fm.instance_exec(&block)
    end.to_h
  end

  key = key.to_s.delete_suffix("=") if key.to_s.ends_with?("=")

  @data[key] = if @data[key].is_a?(Hash) && value.is_a?(Hash)
                 Bridgetown::Utils.deep_merge_hashes(@data[key], value)
               else
                 value
               end
end

#source_manifest(**kwargs) ⇒ Object



57
58
59
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 57

def source_manifest(**kwargs)
  @scope.source_manifests << SourceManifest.new(**kwargs)
end

#timezone(new_timezone) ⇒ Object



77
78
79
80
# File 'bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb', line 77

def timezone(new_timezone)
  @data[:timezone] = new_timezone
  Bridgetown.set_timezone(new_timezone)
end