Module: Bridgetown::Builders::DSL::Resources

Included in:
PluginBuilder
Defined in:
bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb

Instance Method Summary collapse

Instance Method Details

#add_resource(collection_name, path, &block) ⇒ Object

rubocop:todo Metrics/AbcSize



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb', line 11

def add_resource(collection_name, path, &block) # rubocop:todo Metrics/AbcSize
  data = Bridgetown::FrontMatter::RubyFrontMatter.new(scope: self).tap do |fm|
    fm.define_singleton_method(:___) do |hsh|
      hsh.each do |k, v|
        fm.set k, v
      end
    end
    fm.instance_exec(&block)
  end.to_h
  if data[:content]
    data[:_content_] = data[:content]
    data.delete :content
  end

  collection_name = collection_name.to_s
  unless @site.collections[collection_name]
    Bridgetown.logger.info(
      "#{self.class.name}:",
      "Creating `#{collection_name}' collection on the fly..."
    )
    collection = Collection.new(@site, collection_name)
    collection.["output"] = true
    @site.collections[collection_name] = collection
  end

  Bridgetown::Model::Base.build(
    self,
    collection_name,
    path,
    data
  ).as_resource_in_collection
end

#define_resource_method(method_name, class_scope: false, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb', line 44

def define_resource_method(method_name, class_scope: false, &block)
  unless block
    builder_self = self
    block = proc do |*args, **kwargs, &block2|
      prev_var = builder_self.instance_variable_get(:@resource)
      builder_self.instance_variable_set(:@resource, self)
      builder_self.send(method_name, *args, **kwargs, &block2).tap do
        builder_self.instance_variable_set(:@resource, prev_var)
      end
    end
  end

  m = Module.new
  m.define_method method_name, &block

  class_scope ? Bridgetown::Resource::Base.extend(m) : Bridgetown::Resource::Base.include(m)
end


62
63
64
65
66
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb', line 62

def permalink_placeholder(key, &block)
  Bridgetown::Resource::PermalinkProcessor.register_placeholder(
    key, block
  )
end

#placeholder_processorsObject



68
69
70
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb', line 68

def placeholder_processors
  Bridgetown::Resource::PermalinkProcessor.placeholder_processors
end

#resourceObject



7
8
9
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/resources.rb', line 7

def resource
  @resource # could be nil
end