Sha256: 9494826f0ae99905a95653c7c7128b900b422f8ee08a9db46d3c8e448cafee23

Contents?: true

Size: 939 Bytes

Versions: 6

Compression:

Stored size: 939 Bytes

Contents

# frozen_string_literal: true

module RSpecHTML
  # Reconstructs an HTML representation of an element from provided parameters.
  class ReconstitutedElement
    def initialize(tag, options)
      @tag = tag
      @options = options
    end

    def to_s
      name = @tag.to_s.downcase
      return '#document' if name == 'document'
      return name if name == 'document'
      return "<#{name}#{formatted_attributes} />" unless @options&.key?(:text)

      "<#{name}#{formatted_attributes}>#{@options[:text]}</#{name}>"
    end

    private

    def mapped_attributes
      return [] if @options.nil?

      @options.reject { |key| key.to_sym == :text }.map do |key, value|
        next %(#{key}="#{value}") unless key.to_sym == :class && value.is_a?(Array)

        %(#{key}="#{value.join(' ')}")
      end
    end

    def formatted_attributes
      mapped_attributes.empty? ? nil : " #{mapped_attributes.join(' ')}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rspec-html-0.2.8 lib/rspec_html/reconstituted_element.rb
rspec-html-0.2.7 lib/rspec_html/reconstituted_element.rb
rspec-html-0.2.6 lib/rspec_html/reconstituted_element.rb
rspec-html-0.2.5 lib/rspec_html/reconstituted_element.rb
rspec-html-0.2.4 lib/rspec_html/reconstituted_element.rb
rspec-html-0.2.3 lib/rspec_html/reconstituted_element.rb