Class: Bridgetown::Utils::RubyFrontMatter

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/utils/ruby_front_matter.rb

Direct Known Subclasses

Configuration::ConfigurationDSL

Instance Method Summary collapse

Constructor Details

#initialize(scope: nil, data: {}) ⇒ RubyFrontMatter

Returns a new instance of RubyFrontMatter.



12
13
14
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 12

def initialize(scope: nil, data: {})
  @data, @scope = data, scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

rubocop:disable Metrics/CyclomaticComplexity, Style/MissingRespondToMissing



16
17
18
19
20
21
22
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 16

def method_missing(key, *value, &block) # rubocop:disable Metrics/CyclomaticComplexity, Style/MissingRespondToMissing
  return super if respond_to?(key) || (value.empty? && block.nil? && !@data.key?(key))

  return get(key) if value.empty? && block.nil? && @data.key?(key)

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

Instance Method Details

#each(&block) ⇒ Object



24
25
26
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 24

def each(&block)
  @data.each(&block)
end

#get(key) ⇒ Object



28
29
30
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 28

def get(key)
  @data[key]
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 32

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

  # Execute lambda value within the resolver
  if @scope && value.is_a?(Hash) && value[:from].is_a?(Proc)
    value = @scope.instance_exec(&value[:from])
  end

  @data[key] = value
end

#to_hObject



48
49
50
# File 'lib/bridgetown-core/utils/ruby_front_matter.rb', line 48

def to_h
  @data
end