Sha256: 0b74f035c16f81e33fa96adf3e5bfef7d9fce44eb8608df2b81d6245eb53da04

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Formalist
  class Form
    class ValidityCheck
      def call(form_ast)
        form_ast.map { |node| visit(node) }.all?
      end
      alias_method :[], :call

      private

      def visit(node)
        name, nodes = node

        send(:"visit_#{name}", nodes)
      end

      def visit_attr(node)
        _name, _type, errors, _attributes, children = node

        errors.empty? && children.map { |child| visit(child) }.all?
      end

      def visit_compound_field(node)
        _type, _attributes, children = node

        children.map { |child| visit(child) }.all?
      end

      def visit_field(node)
        _name, _type, _input, errors, _attributes = node

        errors.empty?
      end

      def visit_group(node)
        _type, _attributes, children = node

        children.map { |child| visit(child) }.all?
      end

      def visit_many(node)
        _name, _type, errors, _attributes, _child_template, children = node

        # The `children parameter for `many` elements is nested since there are
        # many groups of elements, we need to flatten to traverse them all
        errors.empty? && children.flatten(1).map { |child| visit(child) }.all?
      end

      # TODO work out what to do with this.
      # I think it's only relevant to many_forms
      # nested in rich text ast
      def visit_many_forms(node)
        _name, _type, errors, _attributes, children = node

        errors.empty? && children.map { |child| visit(child[:form]) }.all?
      end

      def visit_section(node)
        _name, _type, _attributes, children = node

        children.map { |child| visit(child) }.all?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
formalist-0.9.0 lib/formalist/form/validity_check.rb
formalist-0.8.0 lib/formalist/form/validity_check.rb
formalist-0.7.0 lib/formalist/form/validity_check.rb