-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
747 additions
and
609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
source 'http://rubygems.org' | ||
gemspec | ||
|
||
if !ENV['TRAVIS'] | ||
unless ENV['TRAVIS'] | ||
gem 'byebug', require: false, platforms: :mri | ||
gem 'yard', require: false | ||
end | ||
|
||
gem 'hanami-utils', '~> 0.8', require: false, github: 'hanami/utils', branch: '0.8.x' | ||
gem 'hanami-router', '~> 0.7', require: false, github: 'hanami/router', branch: '0.7.x' | ||
gem 'hanami-validations', '~> 0.6', require: false, github: 'hanami/validations', branch: '0.6.x' | ||
gem 'minitest', '~> 5.8' | ||
gem 'hanami-utils', '~> 0.8', require: false, github: 'hanami/utils', branch: '0.8.x' | ||
gem 'hanami-router', '~> 0.7', require: false, github: 'hanami/router', branch: '0.7.x' | ||
|
||
group :validations do | ||
gem 'hanami-validations', '~> 0.6', require: false, github: 'hanami/validations' | ||
|
||
# This is required until dry-validation 0.8 will be out | ||
gem 'dry-types', require: false, github: 'dry-rb/dry-types' | ||
gem 'dry-logic', require: false, github: 'dry-rb/dry-logic' | ||
gem 'dry-validation', require: false, github: 'dry-rb/dry-validation' | ||
end | ||
|
||
gem 'coveralls', require: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
require 'rack/request' | ||
require 'hanami/utils/hash' | ||
|
||
module Hanami | ||
module Action | ||
class BaseParams | ||
# The key that returns raw input from the Rack env | ||
# | ||
# @since x.x.x | ||
RACK_INPUT = 'rack.input'.freeze | ||
|
||
# The key that returns router params from the Rack env | ||
# This is a builtin integration for Hanami::Router | ||
# | ||
# @since x.x.x | ||
ROUTER_PARAMS = 'router.params'.freeze | ||
|
||
# Separator for #get | ||
# | ||
# @since x.x.x | ||
# @api private | ||
# | ||
# @see Hanami::Action::Params#get | ||
GET_SEPARATOR = '.'.freeze | ||
|
||
# @attr_reader env [Hash] the Rack env | ||
# | ||
# @since x.x.x | ||
# @api private | ||
attr_reader :env | ||
|
||
# @attr_reader raw [Hash] the raw params from the request | ||
# | ||
# @since x.x.x | ||
# @api private | ||
attr_reader :raw | ||
|
||
# Initialize the params and freeze them. | ||
# | ||
# @param env [Hash] a Rack env or an hash of params. | ||
# | ||
# @return [Params] | ||
# | ||
# @since x.x.x | ||
def initialize(env) | ||
@env = env | ||
@raw = _extract_params | ||
@params = Utils::Hash.new(@raw).symbolize!.to_h | ||
freeze | ||
end | ||
|
||
# Returns the object associated with the given key | ||
# | ||
# @param key [Symbol] the key | ||
# | ||
# @return [Object,nil] return the associated object, if found | ||
# | ||
# @since x.x.x | ||
def [](key) | ||
@params[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 'hanami/controller' | ||
# | ||
# module Deliveries | ||
# class Create | ||
# include Hanami::Action | ||
# | ||
# 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.to_sym] | ||
|
||
Array(keys).each do |k| | ||
break if result.nil? | ||
result = result[k.to_sym] | ||
end | ||
|
||
result | ||
end | ||
|
||
# Serialize params to Hash | ||
# | ||
# @return [::Hash] | ||
# | ||
# @since x.x.x | ||
def to_h | ||
@params | ||
end | ||
alias_method :to_hash, :to_h | ||
|
||
private | ||
|
||
# @since x.x.x | ||
# @api private | ||
def _extract_params | ||
result = {} | ||
|
||
if env.key?(RACK_INPUT) | ||
result.merge! ::Rack::Request.new(env).params | ||
result.merge! _router_params | ||
else | ||
result.merge! _router_params(env) | ||
end | ||
|
||
result | ||
end | ||
|
||
# @since x.x.x | ||
# @api private | ||
def _router_params(fallback = {}) | ||
env.fetch(ROUTER_PARAMS, fallback) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
require 'hanami/action/params' | ||
|
||
module Hanami | ||
module Action | ||
module Callable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.