Sha256: 12548ade6e0d3a8e717c38521cc46ce718b8c493ecd667b773e8bfb1b690c265

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require "dry/core/equalizer"

module Hanami
  class View
    # The output of a view rendering.
    #
    # @api public
    # @since 2.1.0
    class Rendered
      include Dry::Equalizer(:output, :locals)

      # Returns the rendered view output.
      #
      # @return [String]
      #
      # @see to_s
      # @see to_str
      #
      # @api public
      # @since 2.1.0
      attr_reader :output

      # Returns the hash of locals used to render the view output.
      #
      # @return [Hash[<Symbol, Hanami::View::Part>] locals hash
      #
      # @api public
      # @since 2.1.0
      attr_reader :locals

      # @api private
      # @since 2.1.0
      def initialize(output:, locals:)
        @output = output
        @locals = locals
      end

      # Returns the local corresponding to the key.
      #
      # @param name [Symbol] local key
      #
      # @return [Hanami::View::Part]
      #
      # @api public
      # @since 2.1.0
      def [](name)
        locals[name]
      end

      # Returns the rendered view output.
      #
      # @return [String]
      #
      # @api public
      # @since 2.1.0
      def to_s
        output
      end

      # @api public
      # @since 2.1.0
      alias_method :to_str, :to_s

      # Returns true if the given input matches the rendered view output.
      #
      # @param matcher [String, Regexp] matcher
      #
      # @return [TrueClass,FalseClass]
      #
      # @api public
      # @since 2.1.0
      def match?(matcher)
        output.match?(matcher)
      end
      alias_method :match, :match?

      # Returns true if given string is included in the rendered view output.
      #
      # @param string [String] string
      #
      # @return [TrueClass,FalseClass]
      #
      # @api public
      # @since 2.1.0
      def include?(string)
        output.include?(string)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-view-2.2.0 lib/hanami/view/rendered.rb
hanami-view-2.2.0.rc1 lib/hanami/view/rendered.rb
hanami-view-2.2.0.beta2 lib/hanami/view/rendered.rb
hanami-view-2.2.0.beta1 lib/hanami/view/rendered.rb
hanami-view-2.1.0 lib/hanami/view/rendered.rb
hanami-view-2.1.0.rc3 lib/hanami/view/rendered.rb
hanami-view-2.1.0.rc2 lib/hanami/view/rendered.rb