Class: Proc

Inherits:
Object
  • Object
show all
Defined in:
lib/p2/proc_ext.rb

Overview

Extensions to the Proc class.

Instance Method Summary collapse

Instance Method Details

#apply(*a, **b, &c) ⇒ Proc

Returns a proc that applies the given arguments to the original proc.

Returns:

  • (Proc)

    applied 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

#astPrism::Node

Returns the AST for the proc.

Returns:

  • (Prism::Node)

    AST root



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.

Parameters:

  • mode (Symbol) (defaults to: :html)

    compilation mode (:html, :xml)

Returns:

  • (Proc)

    compiled proc



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.

Returns:

  • (self)


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.

Returns:

  • (bool)

    is the proc marked as compiled



33
34
35
# File 'lib/p2/proc_ext.rb', line 33

def compiled?
  @is_compiled
end

#compiled_codeString

Returns the compiled form code for the proc.

Returns:

  • (String)

    compiled proc code



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.

Parameters:

  • mode (Symbol) (defaults to: :html)

    compilation mode (:html, :xml)

Returns:

  • (Proc)

    compiled proc or 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.

Returns:

  • (String)

    HTML string



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.

Returns:

  • (String)

    HTML string



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.

Parameters:

  • buf (String)

    buffer

Returns:

  • (String)

    HTML string



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.

Returns:

  • (String)

    XML string



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_mapArray<String>

Returns the source map for the compiled proc.

Returns:

  • (Array<String>)

    source map



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