Sha256: 0b5767f8a67f585400d38e5b342a84b27c36cf4609ae239b5fcd4a74fdfcfebb

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require "react_on_rails/utils"

module ReactOnRails
  module ReactComponent
    class RenderOptions
      include Utils::Required

      NO_PROPS = {}.freeze

      def initialize(react_component_name: required("react_component_name"), options: required("options"))
        @react_component_name = react_component_name.camelize
        @options = options
      end

      attr_reader :react_component_name

      def props
        options.fetch(:props) { NO_PROPS }
      end

      def dom_id
        @dom_id ||= options.fetch(:id) { generate_unique_dom_id }
      end

      def html_options
        options[:html_options].to_h
      end

      def prerender
        retrieve_key(:prerender)
      end

      def trace
        retrieve_key(:trace)
      end

      def replay_console
        retrieve_key(:replay_console)
      end

      def raise_on_prerender_error
        retrieve_key(:raise_on_prerender_error)
      end

      def logging_on_server
        retrieve_key(:logging_on_server)
      end

      def to_s
        "{ react_component_name = #{react_component_name}, options = #{options}"
      end

      private

      attr_reader :options

      def generate_unique_dom_id
        "#{react_component_name}-react-component-#{SecureRandom.uuid}"
      end

      def retrieve_key(key)
        options.fetch(key) do
          ReactOnRails.configuration.public_send(key)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
react_on_rails-11.0.5 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.0.4 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.0.3 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.0.2 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.0.1 lib/react_on_rails/react_component/render_options.rb