Module: Roda::RodaPlugins::BridgetownRoutes::InstanceMethods

Defined in:
bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb

Instance Method Summary collapse

Instance Method Details

#front_matter(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 59

def front_matter(&block)
  b = block.binding
  denylisted = %i(r argv)
  data = b.local_variables.filter_map do |key|
    next if denylisted.any? key

    [key, b.local_variable_get(key)]
  end.to_h

  Bridgetown::FrontMatter::RubyFrontMatter.new(data:, scope: self)
    .tap { _1.instance_exec(&block) }.to_h
end

#renderObject



103
104
105
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 103

def render(...)
  view.render(...)
end

#render_with(data: {}) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 72

def render_with(data: {}, &) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  data = front_matter(&) if data.empty? && block_given?
  path = Kernel.caller_locations(1, 1).first.path
  source_path = Pathname.new(path).relative_path_from(
    bridgetown_site.in_source_dir("_routes")
  )
  code = response._route_file_code

  unless code&.length&.positive?
    raise Bridgetown::Errors::FatalException,
          "`render_with' method must be called from a template-based file in `src/_routes'"
  end

  data = Bridgetown::Model::BuilderOrigin.new(
    Bridgetown::Model::BuilderOrigin.id_for_builder_path(
      self, Addressable::URI.encode(source_path.to_s)
    )
  ).read do
    data[:_collection_] = bridgetown_site.collections.pages
    data[:_original_path_] = path
    data[:_relative_path_] = source_path
    data[:_front_matter_line_count_] = response._front_matter_line_count
    data[:_content_] = code
    data
  end

  Bridgetown::Model::Base.new(data).to_resource.tap do |resource|
    resource.roda_app = self
  end.read!
end

#routes_manifestObject



34
35
36
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 34

def routes_manifest
  self.class.opts[:routes_manifest]
end

#run_file_route(file, slug:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 38

def run_file_route(file, slug:)
  response["X-Bridgetown-Routes"] = "1"
  # eval_route_file caches when Bridgetown.env.production?
  Bridgetown::Routes::CodeBlocks.eval_route_file file, slug, self

  # set route locale
  locale = routes_manifest.locale_for(slug)
  I18n.locale = request.params[:locale] = locale

  # get the route block extracted from the file at slug
  route_block = Bridgetown::Routes::CodeBlocks.route_block(slug)
  response.instance_variable_set(
    :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
  ) # could be nil
  response.instance_variable_set(
    :@_front_matter_line_count,
    route_block.instance_variable_get(:@_front_matter_line_count)
  ) # could be nil
  instance_exec(request, &route_block)
end

#viewObject



107
108
109
# File 'bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb', line 107

def view(*)
  Bridgetown::TemplateView.tap { _1.virtual_view.resource.roda_app = self }
end