Module: Bridgetown::Routes::CodeBlocks

Defined in:
lib/bridgetown-routes/code_blocks.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.blocksObject

Returns the value of attribute blocks.



7
8
9
# File 'lib/bridgetown-routes/code_blocks.rb', line 7

def blocks
  @blocks
end

Class Method Details

.add_route(name, file_code = nil, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/bridgetown-routes/code_blocks.rb', line 9

def add_route(name, file_code = nil, &block)
  block.instance_variable_set(:@_route_file_code, file_code) if file_code

  @blocks ||= {}
  @blocks[name] = block
end

.eval_route_file(file, file_slug, app) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bridgetown-routes/code_blocks.rb', line 25

def eval_route_file(file, file_slug, app) # rubocop:disable Lint/UnusedMethodArgument
  if Bridgetown.env.production? && Bridgetown::Routes::CodeBlocks.route_defined?(file_slug)
    # we don't need to re-eval the file in production because it won't be changing underfoot
    return
  end

  code = File.read(file)
  code_postmatch = nil
  ruby_content = code.match(Bridgetown::FrontMatterImporter::RUBY_BLOCK)
  if ruby_content
    code = ruby_content[1]
    code_postmatch = ruby_content.post_match
  end

  code = "    add_route(\#{file_slug.inspect}, \#{code_postmatch.inspect}) do |r|\n      \#{code}\n    end\n  RUBY\n  instance_eval(code, file, ruby_content ? 1 : 0)\nend\n"

.route_block(name) ⇒ Object



21
22
23
# File 'lib/bridgetown-routes/code_blocks.rb', line 21

def route_block(name)
  blocks[name] if route_defined?(name)
end

.route_defined?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


17
18
19
# File 'lib/bridgetown-routes/code_blocks.rb', line 17

def route_defined?(name)
  blocks&.key?(name)
end