Sha256: a94c73dd62f3fba432ecebcde862ea92d0e38d28234d56e04bea7a0744a80dcd

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

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

module Formalist
  class Elements
    class Section < Element
      attribute :label, Types::String

      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

8 entries across 8 versions & 1 rubygems

Version Path
formalist-0.5.4 lib/formalist/elements/section.rb
formalist-0.5.3 lib/formalist/elements/section.rb
formalist-0.5.2 lib/formalist/elements/section.rb
formalist-0.5.1 lib/formalist/elements/section.rb
formalist-0.5.0 lib/formalist/elements/section.rb
formalist-0.4.2 lib/formalist/elements/section.rb
formalist-0.4.1 lib/formalist/elements/section.rb
formalist-0.4.0 lib/formalist/elements/section.rb