Sha256: fa4653f271c89b6787aecb1d47a72f305ccef7a56e3e76e33ed8a17fe9031490

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

require "formalist/element"

module Formalist
  class Elements
    class Section < Element
      attribute :label

      def fill(input: {}, errors: {})
        super(
          input: input,
          errors: errors,
          children: children.map { |child| child.fill(input: input, errors: errors) },
        )
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
formalist-0.9.0 lib/formalist/elements/section.rb
formalist-0.8.0 lib/formalist/elements/section.rb
formalist-0.7.0 lib/formalist/elements/section.rb
formalist-0.6.0 lib/formalist/elements/section.rb