Sha256: 431f382bcfa71ba1d67047a85fcf786e28ced219c9705805f1ad329b99549ee1

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require "formalist/element"

module Formalist
  class Elements
    class Attr < Element
      attribute :label

      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

4 entries across 4 versions & 1 rubygems

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