Class: Formalist::RichText::EmbeddedFormCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/formalist/rich_text/embedded_form_compiler.rb

Overview

Our input data looks like this example, which consists of 3 elements:

  1. A text line
  2. embedded form data
  3. Another text line

[ ["block",["unstyled","b14hd",[["inline",[[],"Before!"]]]]], ["block",["atomic","48b4f",[["entity",["formalist","1","IMMUTABLE",with caption","data":{"image_id":"5678","caption":"Large panda"},[["inline",[[],"ΒΆ"]]]]]]]], ["block",["unstyled","aivqi",[["inline",[[],"After!"]]]]] ]

We want to intercept the embededed form data and transform them into full form ASTs, complete with validation messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(embedded_form_collection) ⇒ EmbeddedFormCompiler

Returns a new instance of EmbeddedFormCompiler.



24
25
26
# File 'lib/formalist/rich_text/embedded_form_compiler.rb', line 24

def initialize(embedded_form_collection)
  @embedded_forms = embedded_form_collection
end

Instance Attribute Details

#embedded_formsObject (readonly)

Returns the value of attribute embedded_forms.



22
23
24
# File 'lib/formalist/rich_text/embedded_form_compiler.rb', line 22

def embedded_forms
  @embedded_forms
end

Instance Method Details

#call(ast) ⇒ Object Also known as: []



28
29
30
31
32
33
34
# File 'lib/formalist/rich_text/embedded_form_compiler.rb', line 28

def call(ast)
  return ast if ast.nil?

  ast = ast.is_a?(String) ? JSON.parse(ast) : ast

  ast.map { |node| visit(node) }
end