Module: Papercraft::ProcExtensions
- Defined in:
- lib/papercraft/proc_ext.rb
Overview
Extensions to the Proc class.
Instance Method Summary collapse
-
#apply(*pos1, **kw1, &block) ⇒ Proc
Returns a proc that applies the given arguments to the original proc.
-
#ast ⇒ Prism::Node
Returns the AST for the proc.
-
#compile(mode: :html) ⇒ Proc
Compiles the proc into the compiled form.
-
#compiled! ⇒ self
Marks the proc as compiled, i.e.
-
#compiled? ⇒ bool
Returns true if proc is marked as compiled.
-
#compiled_code ⇒ String
Returns the compiled form code for the proc.
-
#compiled_proc(mode: :html) ⇒ Proc
Returns the compiled proc for the given proc.
-
#render(*a, **b, &c) ⇒ String
Renders the proc to HTML with the given arguments.
-
#render_cached(*args, **kargs, &block) ⇒ String
Caches and returns the rendered HTML for the template with the given arguments.
-
#render_to_buffer(buf, *a, **b, &c) ⇒ String
Renders the proc to HTML with the given arguments into the given buffer.
-
#render_xml(*a, **b, &c) ⇒ String
Renders the proc to XML with the given arguments.
-
#source_map ⇒ Array<String>
Returns the source map for the compiled proc.
Instance Method Details
#apply(*pos1, **kw1, &block) ⇒ Proc
Returns a proc that applies the given arguments to the original proc. The returned proc calls the compiled form of the proc, merging the positional and keywords parameters passed to ‘#apply` with parameters passed to the applied proc. If a block is given, it is wrapped in a proc that passed merged parameters to the block.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/papercraft/proc_ext.rb', line 103 def apply(*pos1, **kw1, &block) compiled = compiled_proc c_compiled = block&.compiled_proc ->(__buffer__, *pos2, **kw2, &block2) { c_proc = c_compiled && ->(__buffer__, *pos3, **kw3) { c_compiled.(__buffer__, *pos3, **kw3, &block2) }.compiled! compiled.(__buffer__, *pos1, *pos2, **kw1, **kw2, &c_proc) }.compiled! end |
#ast ⇒ Prism::Node
Returns the AST for the proc.
27 28 29 |
# File 'lib/papercraft/proc_ext.rb', line 27 def ast Sirop.to_ast(self) end |
#compile(mode: :html) ⇒ Proc
Compiles the proc into the compiled form.
60 61 62 63 64 |
# File 'lib/papercraft/proc_ext.rb', line 60 def compile(mode: :html) Papercraft::Compiler.compile(self, mode:).compiled! rescue Sirop::Error raise Papercraft::Error, "Dynamically defined procs cannot be compiled" end |
#compiled! ⇒ self
Marks the proc as compiled, i.e. can render directly and takes a string buffer as first argument.
42 43 44 45 |
# File 'lib/papercraft/proc_ext.rb', line 42 def compiled! @is_compiled = true self end |
#compiled? ⇒ bool
Returns true if proc is marked as compiled.
34 35 36 |
# File 'lib/papercraft/proc_ext.rb', line 34 def compiled? @is_compiled end |
#compiled_code ⇒ String
Returns the compiled form code for the proc.
11 12 13 |
# File 'lib/papercraft/proc_ext.rb', line 11 def compiled_code Papercraft::Compiler.compile_to_code(self).last end |
#compiled_proc(mode: :html) ⇒ Proc
Returns the compiled proc for the given proc. If marked as compiled, returns self.
52 53 54 |
# File 'lib/papercraft/proc_ext.rb', line 52 def compiled_proc(mode: :html) @compiled_proc ||= @is_compiled ? self : compile(mode:) end |
#render(*a, **b, &c) ⇒ String
Renders the proc to HTML with the given arguments.
69 70 71 72 73 |
# File 'lib/papercraft/proc_ext.rb', line 69 def render(*a, **b, &c) compiled_proc.(+'', *a, **b, &c) rescue Exception => e e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e)) end |
#render_cached(*args, **kargs, &block) ⇒ String
Caches and returns the rendered HTML for the template with the given arguments.
120 121 122 123 124 |
# File 'lib/papercraft/proc_ext.rb', line 120 def render_cached(*args, **kargs, &block) @render_cache ||= {} key = args.empty? && kargs.empty? && !block ? nil : [args, kargs, block&.source_location] @render_cache[key] ||= render(*args, **kargs, &block) end |
#render_to_buffer(buf, *a, **b, &c) ⇒ String
Renders the proc to HTML with the given arguments into the given buffer.
88 89 90 91 92 |
# File 'lib/papercraft/proc_ext.rb', line 88 def render_to_buffer(buf, *a, **b, &c) compiled_proc.(buf, *a, **b, &c) rescue Exception => e raise Papercraft.translate_backtrace(e) end |
#render_xml(*a, **b, &c) ⇒ String
Renders the proc to XML with the given arguments.
78 79 80 81 82 |
# File 'lib/papercraft/proc_ext.rb', line 78 def render_xml(*a, **b, &c) compiled_proc(mode: :xml).(+'', *a, **b, &c) rescue Exception => e e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e)) end |
#source_map ⇒ Array<String>
Returns the source map for the compiled proc.
18 19 20 21 22 |
# File 'lib/papercraft/proc_ext.rb', line 18 def source_map loc = source_location fn = compiled? ? loc.first : Papercraft::Compiler.source_location_to_fn(loc) Papercraft::Compiler.source_map_store[fn] end |