Sha256: 176ed2f6fe8c9afe7817fbfbbba4eed23ee048b53f450cb801f5ae2411219e43

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

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

module Formalist
  class Elements
    class Section < Element
      permitted_children :all

      # @api private
      attr_reader :name

      attribute :label, Types::String

      def initialize(*args, attributes, children, input, errors)
        super

        @name = Types::ElementName.(args.first)
        @children = children.map { |definition| definition.(input, 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

2 entries across 2 versions & 1 rubygems

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