Class: Proc
- Inherits:
-
Object
- Object
- Proc
- Defined in:
- lib/p2/proc_ext.rb
Overview
Extensions to the Proc class.
Instance Method Summary collapse
-
#apply(*a, **b, &c) ⇒ 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(*a, **b, &c) ⇒ Proc
Returns a proc that applies the given arguments to the original proc.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/p2/proc_ext.rb', line 96 def apply(*a, **b, &c) compiled = compiled_proc c_compiled = c&.compiled_proc ->(__buffer__, *x, **y, &z) { c_proc = c_compiled && ->(__buffer__, *d, **e) { c_compiled.(__buffer__, *a, *d, **b, **e, &z) }.compiled! compiled.(__buffer__, *a, *x, **b, **y, &c_proc) }.compiled! end |
#ast ⇒ Prism::Node
Returns the AST for the proc.
26 27 28 |
# File 'lib/p2/proc_ext.rb', line 26 def ast Sirop.to_ast(self) end |
#compile(mode: :html) ⇒ Proc
Compiles the proc into the compiled form.
59 60 61 62 63 |
# File 'lib/p2/proc_ext.rb', line 59 def compile(mode: :html) P2::Compiler.compile(self, mode:).compiled! rescue Sirop::Error raise P2::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.
41 42 43 44 |
# File 'lib/p2/proc_ext.rb', line 41 def compiled! @is_compiled = true self end |
#compiled? ⇒ bool
Returns true if proc is marked as compiled.
33 34 35 |
# File 'lib/p2/proc_ext.rb', line 33 def compiled? @is_compiled end |
#compiled_code ⇒ String
Returns the compiled form code for the proc.
10 11 12 |
# File 'lib/p2/proc_ext.rb', line 10 def compiled_code P2::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.
51 52 53 |
# File 'lib/p2/proc_ext.rb', line 51 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.
68 69 70 71 72 |
# File 'lib/p2/proc_ext.rb', line 68 def render(*a, **b, &c) compiled_proc.(+'', *a, **b, &c) rescue Exception => e e.is_a?(P2::Error) ? raise : raise(P2.translate_backtrace(e)) end |
#render_cached(*args, **kargs, &block) ⇒ String
Caches and returns the rendered HTML for the template with the given arguments.
113 114 115 116 117 |
# File 'lib/p2/proc_ext.rb', line 113 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.
87 88 89 90 91 |
# File 'lib/p2/proc_ext.rb', line 87 def render_to_buffer(buf, *a, **b, &c) compiled_proc.(buf, *a, **b, &c) rescue Exception => e raise P2.translate_backtrace(e) end |
#render_xml(*a, **b, &c) ⇒ String
Renders the proc to XML with the given arguments.
77 78 79 80 81 |
# File 'lib/p2/proc_ext.rb', line 77 def render_xml(*a, **b, &c) compiled_proc(mode: :xml).(+'', *a, **b, &c) rescue Exception => e e.is_a?(P2::Error) ? raise : raise(P2.translate_backtrace(e)) end |
#source_map ⇒ Array<String>
Returns the source map for the compiled proc.
17 18 19 20 21 |
# File 'lib/p2/proc_ext.rb', line 17 def source_map loc = source_location fn = compiled? ? loc.first : P2::Compiler.source_location_to_fn(loc) P2::Compiler.source_map_store[fn] end |