Sha256: eedb3e7c0c2d8943b5c8bc17916453077bce5b2618bc67b7b950d2d4746a2f74

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

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

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

      def fill(input:, errors:)
        input = input[name] || {}
        errors = errors[name] || {}

        children = self.children.map { |child|
          child.fill(input: input, errors: errors)
        }

        super(input: input, errors: errors, 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
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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