Sha256: fa689f91675e16f0def8e8e736b7d8bcb65ed1217549fa5ec9a424425066f2ce

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "dry/core/equalizer"

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

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

      # Returns the hash of locals used to render the view
      #
      # @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
      #
      # @return [String]
      #
      # @api public
      # @since 2.1.0
      def to_s
        output
      end
      alias_method :to_str, :to_s

      # Matches given input with the rendered view
      #
      # @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?

      # Checks if given string is included in the rendered view
      #
      # @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

2 entries across 2 versions & 1 rubygems

Version Path
hanami-view-2.1.0.rc1 lib/hanami/view/rendered.rb
hanami-view-2.1.0.beta2 lib/hanami/view/rendered.rb