Sha256: d8e9150a59ea2f00858eda4a5d42949a0fee59501d57609625b070875c4aa60b

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

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

module Formalist
  class Elements
    class Attr < Element
      permitted_children :all

      # @api private
      attr_reader :name

      attribute :label, Types::String

      # @api private
      def initialize(*args, attributes, children, input, errors)
        super

        @name = Types::ElementName.(args.first)
        @input = input.fetch(@name, {})
        @errors = errors[@name]
        @children = build_children(children)
      end

      # Converts the attribute into an abstract syntax tree.
      #
      # It takes the following format:
      #
      # ```
      # [:attr, [params]]
      # ```
      #
      # With the following parameters:
      #
      # 1. Attribute name
      # 2. Custom element type (or `:attr` otherwise)
      # 3. Error messages
      # 4. Form element attributes
      # 5. Child form elements
      #
      # @see Formalist::Element::Attributes#to_ast "Form element attributes" structure
      #
      # @example "metadata" attr
      #   attr.to_ast
      #   # => [:attr, [
      #     :metadata,
      #     :attr,
      #     ["metadata is missing"],
      #     [:object, []],
      #     [...child elements...]
      #   ]]
      #
      # @return [Array] the attribute as an abstract syntax tree.
      def to_ast
        local_errors = errors.is_a?(Array) ? errors : []

        [:attr, [
          name,
          type,
          local_errors,
          Element::Attributes.new(attributes).to_ast,
          children.map(&:to_ast),
        ]]
      end

      private

      def build_children(definitions)
        child_errors = errors.is_a?(Hash) ? errors : {}

        definitions.map { |definition| definition.(input, child_errors) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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