Sha256: 22ba5543e24245ef62ed2199da396e3947207361b4df2f5b8358599800cb080d

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module Formalist
  module RichText
    module Rendering
      class EmbeddedFormRenderer
        attr_reader :container
        attr_reader :namespace
        attr_reader :paths
        attr_reader :options

        def initialize(container = {}, namespace: nil, paths: [], **options)
          @container = container
          @namespace = namespace
          @paths = paths
          @options = options
        end

        def call(form_data)
          type, data = form_data.values_at(:name, :data)

          key = resolve_key(type)

          if key
            container[key].(data, **options)
          else
            ""
          end
        end

        def with(**context_options)
          self.class.new(
            container,
            namespace: namespace,
            paths: paths,
            **options.merge(context_options)
          )
        end

        private

        def resolve_key(type)
          paths.each do |path|
            path_key = path.tr("/", ".")
            key = [namespace, path_key, type].compact.join(".")
            return key if container.key?(key)
          end

          key = [namespace, type].compact.join(".")
          return key if container.key?(key)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formalist-0.9.0 lib/formalist/rich_text/rendering/embedded_form_renderer.rb