Sha256: 1f2a66052484f23371300b14c4ae6d09ea3cc647de8492e667d6293a160207aa

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require "react_on_rails/utils"

module ReactOnRails
  module ReactComponent
    class RenderOptions
      include Utils::Required

      attr_accessor :request_digest

      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 random_dom_id
        retrieve_key(:random_dom_id)
      end

      def dom_id
        @dom_id ||= options.fetch(:id) do
          if random_dom_id
            generate_unique_dom_id
          else
            base_dom_id
          end
        end
      end

      def random_dom_id?
        return false if options[:id]

        return false unless random_dom_id

        true
      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}, request_digest = #{request_digest}"
      end

      def internal_option(key)
        options[key]
      end

      private

      attr_reader :options

      def base_dom_id
        "#{react_component_name}-react-component"
      end

      def generate_unique_dom_id
        "#{base_dom_id}-#{SecureRandom.uuid}"
      end

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
react_on_rails-12.1.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.5.beta.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.4 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.3.1 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.3.1.beta.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.3 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.3.beta.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.2 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.1 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0.pre.beta.4 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0.pre.beta.3 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0.pre.beta.2 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0.pre.beta.1 lib/react_on_rails/react_component/render_options.rb
react_on_rails-12.0.0.pre.beta.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.3.0 lib/react_on_rails/react_component/render_options.rb