Module: Papercraft::ProcExtensions

Defined in:
lib/papercraft/proc_ext.rb

Overview

Extensions to the Proc class.

Instance Method Summary collapse

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.

Parameters:

  • *pos1 (Array<any>)

    applied positional parameters

  • **kw1 (Hash<any, any] applied keyword parameters)

    *kw1 [Hash<any, any] applied keyword parameters

Returns:

  • (Proc)

    applied proc



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

#astPrism::Node

Returns the AST for the proc.

Returns:

  • (Prism::Node)

    AST root



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.

Parameters:

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

    compilation mode (:html, :xml)

Returns:

  • (Proc)

    compiled proc



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.

Returns:

  • (self)


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.

Returns:

  • (bool)

    is the proc marked as compiled



34
35
36
# File 'lib/papercraft/proc_ext.rb', line 34

def compiled?
  @is_compiled
end

#compiled_codeString

Returns the compiled form code for the proc.

Returns:

  • (String)

    compiled proc code



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.

Parameters:

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

    compilation mode (:html, :xml)

Returns:

  • (Proc)

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

Returns:

  • (String)

    HTML string



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.

Returns:

  • (String)

    HTML string



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.

Parameters:

  • buf (String)

    buffer

Returns:

  • (String)

    HTML string



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.

Returns:

  • (String)

    XML string



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

Returns the source map for the compiled proc.

Returns:

  • (Array<String>)

    source map



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