Skip to content

Commit

Permalink
Merge pull request #89 from lotus/params-get
Browse files Browse the repository at this point in the history
Introduced Lotus::Action::Params#get
  • Loading branch information
jodosha committed Feb 21, 2015
2 parents 1bbd4f1 + cf2a1cb commit 89afe18
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 19 deletions.
54 changes: 54 additions & 0 deletions lib/lotus/action/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class Params
# @since 0.1.0
ROUTER_PARAMS = 'router.params'.freeze

# Separator for #get
#
# @since x.x.x
# @api private
#
# @see Lotus::Action::Params#get
GET_SEPARATOR = '.'.freeze

# Whitelist and validate a parameter
#
# @param name [#to_sym] The name of the param to whitelist
Expand Down Expand Up @@ -137,6 +145,52 @@ def [](key)
@attributes.get(key)
end

# Get an attribute value associated with the given key.
# Nested attributes are reached with a dot notation.
#
# @param key [String] the key
#
# @return [Object,NilClass] return the associated value, if found
#
# @since x.x.x
#
# @example
# require 'lotus/controller'
#
# module Deliveries
# class Create
# include Lotus::Action
#
# params do
# param :customer_name
# param :address do
# param :city
# end
# end
#
# def call(params)
# params.get('customer_name') # => "Luca"
# params.get('uknown') # => nil
#
# params.get('address.city') # => "Rome"
# params.get('address.unknown') # => nil
#
# params.get(nil) # => nil
# end
# end
# end
def get(key)
key, *keys = key.to_s.split(GET_SEPARATOR)
result = self[key]

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

result
end

# Returns the Ruby's hash
#
# @return [Hash]
Expand Down
73 changes: 54 additions & 19 deletions test/action/params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,6 @@
end

describe 'validations' do
before do
TestParams = Class.new(Lotus::Action::Params) do
param :email, presence: true, format: /\A.+@.+\z/
param :name, presence: true
param :tos, acceptance: true
param :age, type: Integer
param :address do
param :line_one, presence: true
param :deep do
param :deep_attr, type: String
end
end
end
end

after do
Object.send(:remove_const, :TestParams)
end

it "isn't valid with empty params" do
params = TestParams.new({})

Expand Down Expand Up @@ -228,6 +209,60 @@
end
end

describe '#get' do
describe 'with data' do
before do
@params = TestParams.new(name: 'John', address: { line_one: '10 High Street', deep: { deep_attr: 1 } })
end

it 'returns nil for nil argument' do
@params.get(nil).must_be_nil
end

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

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

it 'allows to read nested param' do
@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
end
end

describe 'without data' do
before do
@params = TestParams.new({})
end

it 'returns nil for nil argument' do
@params.get(nil).must_be_nil
end

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

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

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

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

describe '#to_h' do
let(:params) { Lotus::Action::Params.new(id: '23') }

Expand Down
13 changes: 13 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,19 @@ def call(params)
end
end

class TestParams < Lotus::Action::Params
param :email, presence: true, format: /\A.+@.+\z/
param :name, presence: true
param :tos, acceptance: true
param :age, type: Integer
param :address do
param :line_one, presence: true
param :deep do
param :deep_attr, type: String
end
end
end

class Root
include Lotus::Action

Expand Down

0 comments on commit 89afe18

Please sign in to comment.