lib/hanami/action/base_params.rb in hanami-controller-2.0.0.alpha8 vs lib/hanami/action/base_params.rb in hanami-controller-2.0.0.beta1
- old
+ new
@@ -1,48 +1,13 @@
-require 'rack/request'
-require 'hanami/utils/hash'
+# frozen_string_literal: true
+require "rack/request"
+require "hanami/utils/hash"
+
module Hanami
class Action
class BaseParams
- # The key that returns raw input from the Rack env
- #
- # @since 0.7.0
- # @api private
- RACK_INPUT = 'rack.input'.freeze
-
- # The key that returns router params from the Rack env
- # This is a builtin integration for Hanami::Router
- #
- # @since 0.7.0
- # @api private
- ROUTER_PARAMS = 'router.params'.freeze
-
- # The key that returns Rack session params from the Rack env
- # Please note that this is used only when an action is unit tested.
- #
- # @since 1.0.0
- # @api private
- #
- # @example
- # # action unit test
- # action.call('rack.session' => { 'foo' => 'bar' })
- # action.session[:foo] # => "bar"
- RACK_SESSION = 'rack.session'.freeze
-
- # HTTP request method for Rack env
- #
- # @since 1.1.1
- # @api private
- REQUEST_METHOD = 'REQUEST_METHOD'.freeze
-
- # Default HTTP request method for Rack env
- #
- # @since 1.1.1
- # @api private
- DEFAULT_REQUEST_METHOD = 'GET'.freeze
-
# @attr_reader env [Hash] the Rack env
#
# @since 0.7.0
# @api private
attr_reader :env
@@ -87,28 +52,26 @@
# @return [Object,NilClass] return the associated value, if found
#
# @since 0.7.0
#
# @example
- # require 'hanami/controller'
+ # require "hanami/controller"
#
# module Deliveries
- # class Create
- # include Hanami::Action
+ # class Create < Hanami::Action
+ # def handle(req, *)
+ # req.params.get(:customer_name) # => "Luca"
+ # req.params.get(:uknown) # => nil
#
- # def call(params)
- # params.get(:customer_name) # => "Luca"
- # params.get(:uknown) # => nil
+ # req.params.get(:address, :city) # => "Rome"
+ # req.params.get(:address, :unknown) # => nil
#
- # params.get(:address, :city) # => "Rome"
- # params.get(:address, :unknown) # => nil
+ # req.params.get(:tags, 0) # => "foo"
+ # req.params.get(:tags, 1) # => "bar"
+ # req.params.get(:tags, 999) # => nil
#
- # params.get(:tags, 0) # => "foo"
- # params.get(:tags, 1) # => "bar"
- # params.get(:tags, 999) # => nil
- #
- # params.get(nil) # => nil
+ # req.params.get(nil) # => nil
# end
# end
# end
def get(*keys)
@params.dig(*keys)
@@ -116,11 +79,11 @@
# This is for compatibility with Hanami::Helpers::FormHelper::Values
#
# @api private
# @since 0.8.0
- alias dig get
+ alias_method :dig, :get
# Provide a common interface with Params
#
# @return [TrueClass] always returns true
#
@@ -155,26 +118,26 @@
# @since 0.7.0
# @api private
def _extract_params
result = {}
- if env.key?(RACK_INPUT)
+ if env.key?(Action::RACK_INPUT)
result.merge! ::Rack::Request.new(env).params
result.merge! _router_params
else
result.merge! _router_params(env)
- env[REQUEST_METHOD] ||= DEFAULT_REQUEST_METHOD
+ env[Action::REQUEST_METHOD] ||= Action::DEFAULT_REQUEST_METHOD
end
result
end
# @since 0.7.0
# @api private
def _router_params(fallback = {})
env.fetch(ROUTER_PARAMS) do
- if session = fallback.delete(RACK_SESSION) # rubocop:disable Lint/AssignmentInCondition
- fallback[RACK_SESSION] = Utils::Hash.deep_symbolize(session)
+ if session = fallback.delete(Action::RACK_SESSION)
+ fallback[Action::RACK_SESSION] = Utils::Hash.deep_symbolize(session)
end
fallback
end
end