Skip to content

Commit

Permalink
Merge pull request #181 from hanami/params-get-to-accept-symbols
Browse files Browse the repository at this point in the history
Let BaseParams#get to behave like Hash#dig
  • Loading branch information
jodosha committed Nov 9, 2016
2 parents 2f4165a + 036be4d commit 547f5de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
38 changes: 13 additions & 25 deletions lib/hanami/action/base_params.rb
Expand Up @@ -61,14 +61,12 @@ def [](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 key [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
Expand All @@ -79,28 +77,22 @@ def [](key)
# 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(:tags, 0) # => "foo"
# params.get(:tags, 1) # => "bar"
# params.get(:tags, 999) # => nil
#
# params.get(nil) # => 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_for_get(key)]

Array(keys).each do |k|
break if result.nil?
result = result[_key_for_get(k)]
end

result
def get(*keys)
@params.dig(*keys)
end

# Provide a common interface with Params
Expand Down Expand Up @@ -155,10 +147,6 @@ def _extract_params
def _router_params(fallback = {})
env.fetch(ROUTER_PARAMS, fallback)
end

def _key_for_get(key)
key =~ /\A\d+\z/ ? key.to_i : key.to_sym
end
end
end
end
22 changes: 11 additions & 11 deletions test/action/params_test.rb
Expand Up @@ -235,7 +235,7 @@
@params = TestParams.new(
name: 'John',
address: { line_one: '10 High Street', deep: { deep_attr: 1 } },
array: [{ name: 'Lenon' }, { name: 'Wayne' }]
array: [{ name: 'Lennon' }, { name: 'Wayne' }]
)
end

Expand All @@ -244,24 +244,24 @@
end

it 'returns nil for unknown param' do
@params.get('unknown').must_be_nil
@params.get(:unknown).must_be_nil
end

it 'allows to read top level param' do
@params.get('name').must_equal 'John'
@params.get(:name).must_equal 'John'
end

it 'allows to read nested param' do
@params.get('address.line_one').must_equal '10 High Street'
@params.get(:address, :line_one).must_equal '10 High Street'
end

it 'returns nil for uknown nested param' do
@params.get('address.unknown').must_be_nil
@params.get(:address, :unknown).must_be_nil
end

it 'allows to read datas under arrays' do
@params.get('array.0.name').must_equal 'Lenon'
@params.get('array.1.name').must_equal 'Wayne'
@params.get(:array, 0, :name).must_equal 'Lennon'
@params.get(:array, 1, :name).must_equal 'Wayne'
end
end

Expand All @@ -275,19 +275,19 @@
end

it 'returns nil for unknown param' do
@params.get('unknown').must_be_nil
@params.get(:unknown).must_be_nil
end

it 'returns nil for top level param' do
@params.get('name').must_be_nil
@params.get(:name).must_be_nil
end

it 'returns nil for nested param' do
@params.get('address.line_one').must_be_nil
@params.get(:address, :line_one).must_be_nil
end

it 'returns nil for uknown nested param' do
@params.get('address.unknown').must_be_nil
@params.get(:address, :unknown).must_be_nil
end
end
end
Expand Down

0 comments on commit 547f5de

Please sign in to comment.