Module: Bridgetown::Routes::CodeBlocks
- Defined in:
- lib/bridgetown-routes/code_blocks.rb
Class Attribute Summary collapse
-
.blocks ⇒ Object
Returns the value of attribute blocks.
Class Method Summary collapse
- .add_route(name, file_code = nil, front_matter_line_count = nil, &block) ⇒ Object
-
.eval_route_file(file, file_slug, app) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- .route_block(name) ⇒ Object
- .route_defined?(name) ⇒ Boolean
Class Attribute Details
.blocks ⇒ Object
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, front_matter_line_count = nil, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/bridgetown-routes/code_blocks.rb', line 9 def add_route(name, file_code = nil, front_matter_line_count = nil, &block) block.instance_variable_set(:@_route_file_code, file_code) if file_code if front_matter_line_count block.instance_variable_set(:@_front_matter_line_count, front_matter_line_count) end @blocks ||= {} @blocks[name] = block end |
.eval_route_file(file, file_slug, app) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bridgetown-routes/code_blocks.rb', line 28 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::FrontMatter::Loaders::Ruby::BLOCK) front_matter_line_count = nil if ruby_content code = ruby_content[1] code_postmatch = ruby_content.post_match.lstrip front_matter_line_count = code.lines.count - 1 if code.match?(%r!^\s*render_with(\s|\()!).! && code.match?(%r!r\.[a-z]+\s+do!).! code.concat("\nrender_with {}") end end # rubocop:disable Style/DocumentDynamicEvalDefinition, Style/EvalWithLocation code_proc = Kernel.eval( "proc {|r| #{code} }", TOPLEVEL_BINDING, file, ruby_content ? 2 : 1 ) add_route(file_slug, code_postmatch, front_matter_line_count, &code_proc) # rubocop:enable Style/DocumentDynamicEvalDefinition, Style/EvalWithLocation end |
.route_block(name) ⇒ Object
24 25 26 |
# File 'lib/bridgetown-routes/code_blocks.rb', line 24 def route_block(name) blocks[name] if route_defined?(name) end |
.route_defined?(name) ⇒ Boolean
20 21 22 |
# File 'lib/bridgetown-routes/code_blocks.rb', line 20 def route_defined?(name) blocks&.key?(name) end |