Sha256: 8a9511b7fd85bc2a1551bb41c0f8f10fcf6e3ee790d45591d027c5c4fb20bb84

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "hanami/view"
require "hanami/view/context"
require_relative "../../errors"
require_relative "../../slice_configurable"
require_relative "slice_configured_context"

module Hanami
  class Application
    class View < Hanami::View
      # View context for views in Hanami applications.
      #
      # @api public
      # @since 2.0.0
      class Context < Hanami::View::Context
        extend Hanami::SliceConfigurable

        # @api private
        def self.configure_for_slice(slice)
          extend SliceConfiguredContext.new(slice)
        end

        # @see SliceConfiguredContext#define_new
        def initialize(**kwargs)
          defaults = {content: {}}

          super(**kwargs, **defaults)
        end

        def inflector
          _options.fetch(:inflector)
        end

        def routes
          _options.fetch(:routes)
        end

        def settings
          _options.fetch(:settings)
        end

        def assets
          unless _options[:assets]
            raise Hanami::ComponentLoadError, "hanami-assets gem is required to access assets"
          end

          _options[:assets]
        end

        def content_for(key, value = nil, &block)
          content = _options[:content]
          output = nil

          if block
            content[key] = yield
          elsif value
            content[key] = value
          else
            output = content[key]
          end

          output
        end

        def current_path
          request.fullpath
        end

        def csrf_token
          request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN]
        end

        def request
          _options.fetch(:request)
        end

        def session
          request.session
        end

        def flash
          response.flash
        end

        private

        # TODO: create `Request#flash` so we no longer need the `response`
        def response
          _options.fetch(:response)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-2.0.0.alpha8 lib/hanami/application/view/context.rb