Sha256: 62cfa451968f00473fb210435e5e205eeb617b8b670bd442bdf0467eeb1d9e07

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require "formalist/element"
require "formalist/types"

module Formalist
  class Elements
    class Group < Element
      permitted_children :attr, :compound_field, :field, :many

      attribute :label, Types::String

      def initialize(*args, attributes, children, input, errors)
        super
        @children = children.map { |definition| definition.(input, errors) }
      end

      # Converts the group into an abstract syntax tree.
      #
      # It takes the following format:
      #
      # ```
      # [:group, [params]]
      # ```
      #
      # With the following parameters:
      #
      # 1. Custom form element type (or `:group` otherwise)
      # 2. Form element attributes
      # 3. Child form elements
      #
      # @see Formalist::Element::Attributes#to_ast "Form element attributes" structure
      #
      # @example
      #   group.to_ast
      #   # => [:group, [
      #     :group,
      #     [:object, []],
      #     [...child elements...]
      #   ]]
      #
      # @return [Array] the group as an abstract syntax tree.
      def to_ast
        [:group, [
          type,
          Element::Attributes.new(attributes).to_ast,
          children.map(&:to_ast),
        ]]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
formalist-0.3.0 lib/formalist/elements/group.rb
formalist-0.2.3 lib/formalist/elements/group.rb