Sha256: 7f1df61d069a065464473b56f08ddd9c06c9bb130bf70cb5b2b6cdddd93230de

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Formalist
  class Form
    class Result
      class Component
        attr_reader :definition, :input, :rules, :errors
        attr_reader :children

        def initialize(definition, input, rules, errors)
          @definition = definition
          @input = input
          @rules = rules
          @errors = errors
          @children = definition.children.map { |el| el.(input, rules, errors) }
        end

        # Converts the component into an array format for including in a
        # form's abstract syntax tree.
        #
        # The array takes the following format:
        #
        # ```
        # [:component, [params]]
        # ```
        #
        # With the following parameters:
        #
        # 1. Component configuration
        # 1. Child form elements
        #
        # @example
        #   component.to_ast # =>
        #   # [:component, [
        #   #   [
        #   #     [:some_config_name, :some_config_value]
        #   #   ],
        #   #   [
        #   #     ...child elements...
        #   #   ]
        #   # ]]
        #
        # @return [Array] the component as an array.
        def to_ast
          [:component, [
            definition.config.to_a,
            children.map(&:to_ast),
          ]]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
formalist-0.2.2 lib/formalist/form/result/component.rb
formalist-0.2.1 lib/formalist/form/result/component.rb
formalist-0.2.0 lib/formalist/form/result/component.rb