lib/hanami/view/application_context.rb in hanami-view-2.0.0.alpha3 vs lib/hanami/view/application_context.rb in hanami-view-2.0.0.alpha5

- old
+ new

@@ -1,35 +1,57 @@ # frozen_string_literal: true module Hanami class View - module ApplicationContext - def initialize(inflector: Hanami.application.inflector, **options) - @inflector = inflector - super - end + class ApplicationContext < Module + attr_reader :provider + attr_reader :application - def inflector - @inflector + def initialize(provider) + @provider = provider + @application = provider.respond_to?(:application) ? provider.application : Hanami.application end - def request - _options.fetch(:request) + def included(context_class) + define_initialize + context_class.include(InstanceMethods) end - def session - request.session - end + private - def flash - response.flash + def define_initialize + inflector = application.inflector + routes = application[:routes_helper] if application.key?(:routes_helper) + + define_method :initialize do |**options| + @inflector = options[:inflector] || inflector + @routes = options[:routes] || routes + super(**options) + end end - private + module InstanceMethods + attr_reader :inflector + attr_reader :routes - # TODO: create `Request#flash` so we no longer need the `response` - def response - _options.fetch(:response) + 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