lib/hanami/action/base_params.rb in hanami-controller-0.7.1 vs lib/hanami/action/base_params.rb in hanami-controller-0.8.0
- old
+ new
@@ -13,18 +13,10 @@
# This is a builtin integration for Hanami::Router
#
# @since 0.7.0
ROUTER_PARAMS = 'router.params'.freeze
- # Separator for #get
- #
- # @since 0.7.0
- # @api private
- #
- # @see Hanami::Action::Params#get
- GET_SEPARATOR = '.'.freeze
-
# @attr_reader env [Hash] the Rack env
#
# @since 0.7.0
# @api private
attr_reader :env
@@ -43,11 +35,11 @@
#
# @since 0.7.0
def initialize(env)
@env = env
@raw = _extract_params
- @params = Utils::Hash.new(@raw).symbolize!.to_h
+ @params = Utils::Hash.new(@raw).deep_dup.symbolize!.to_h
freeze
end
# Returns the object associated with the given key
#
@@ -59,50 +51,48 @@
def [](key)
@params[key]
end
# Get an attribute value associated with the given key.
- # Nested attributes are reached with a dot notation.
+ # Nested attributes are reached by listing all the keys to get to the value.
#
- # @param key [String] the key
+ # @param keys [Array<Symbol,Integer>] the key
#
# @return [Object,NilClass] return the associated value, if found
#
- # @raise [NoMethodError] if key is nil
- #
# @since 0.7.0
#
# @example
# require 'hanami/controller'
#
# module Deliveries
# class Create
# include Hanami::Action
#
# def call(params)
- # params.get('customer_name') # => "Luca"
- # params.get('uknown') # => nil
+ # params.get(:customer_name) # => "Luca"
+ # params.get(:uknown) # => nil
#
- # params.get('address.city') # => "Rome"
- # params.get('address.unknown') # => nil
+ # params.get(:address, :city) # => "Rome"
+ # params.get(:address, :unknown) # => nil
#
- # params.get(nil) # => nil
+ # params.get(:tags, 0) # => "foo"
+ # params.get(:tags, 1) # => "bar"
+ # params.get(:tags, 999) # => nil
+ #
+ # params.get(nil) # => nil
# end
# end
# end
- def get(key)
- key, *keys = key.to_s.split(GET_SEPARATOR)
- return if key.nil?
-
- result = self[key.to_sym]
-
- Array(keys).each do |k|
- break if result.nil?
- result = result[k.to_sym]
- end
-
- result
+ def get(*keys)
+ @params.dig(*keys)
end
+
+ # This is for compatibility with Hanami::Helpers::FormHelper::Values
+ #
+ # @api private
+ # @since 0.8.0
+ alias dig get
# Provide a common interface with Params
#
# @return [TrueClass] always returns true
#