Class: Bridgetown::Configuration::ConfigurationDSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Utils::RubyFrontMatter

#each, #initialize, #to_h

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

rubocop:disable Style/MissingRespondToMissing



79
80
81
82
83
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 79

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 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 6

def context
  @context
end

Instance Method Details

#_in_require_denylist?(name) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 145

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

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



134
135
136
137
138
139
140
141
142
143
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 134

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, &block) ⇒ Object



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

def builder(klass = nil, &block)
  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, &block).tap(&:register)
  )
end

#except(*context, &block) ⇒ Object



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

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

  instance_exec(&block)
end

#get(key) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 85

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, &block) ⇒ Object



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

def hook(
  owner,
  event,
  priority: Bridgetown::Hooks::DEFAULT_PRIORITY,
  &block
)
  Bridgetown::Hooks.register_one(owner, event, priority: priority, reloadable: false, &block)
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
# File '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: name, require_gem: require_gem, require_initializer: 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].symbolize_keys)
  initializer.completed = true
end

#only(*context, &block) ⇒ Object



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

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

  instance_exec(&block)
end

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



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

def reflect(name, require_gem: true, require_initializer: true)
  initializer = _setup_initializer(
    name: name, require_gem: require_gem, require_initializer: 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



71
72
73
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 71

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

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



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

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



55
56
57
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 55

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

#timezone(tz) ⇒ Object

rubocop:disable Naming/MethodParameterName



75
76
77
# File 'lib/bridgetown-core/configuration/configuration_dsl.rb', line 75

def timezone(tz) # rubocop:disable Naming/MethodParameterName
  Bridgetown.set_timezone(tz)
end