Class: Hanami::Action::Params
- Inherits:
-
BaseParams
- Object
- BaseParams
- Hanami::Action::Params
- Includes:
- Validations::Form
- Defined in:
- lib/hanami/action/params.rb
Overview
A set of params requested by the client
It’s able to extract the relevant params from a Rack env of from an Hash.
There are three scenarios:
* When used with Hanami::Router: it contains only the params from the request
* When used standalone: it contains all the Rack env
* Default: it returns the given hash as it is. It's useful for testing purposes.
Constant Summary
Constants inherited from BaseParams
BaseParams::RACK_INPUT, BaseParams::ROUTER_PARAMS
Instance Attribute Summary
Attributes inherited from BaseParams
Class Method Summary collapse
-
._base_rules ⇒ Object
private
This is a Hanami::Validations extension point.
- .params(&blk) ⇒ Object
Instance Method Summary collapse
-
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages.
-
#errors ⇒ Hash
Returns structured error messages.
-
#initialize(env) ⇒ Params
constructor
Initialize the params and freeze them.
-
#raw ⇒ Hash
Returns raw params from Rack env.
-
#to_h ⇒ ::Hash
(also: #to_hash)
Serialize params to Hash.
-
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
Methods inherited from BaseParams
Constructor Details
#initialize(env) ⇒ Params
Initialize the params and freeze them.
40 41 42 43 44 45 46 |
# File 'lib/hanami/action/params.rb', line 40 def initialize(env) @env = env super(_extract_params) @result = validate @params = _params freeze end |
Class Method Details
._base_rules ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is a Hanami::Validations extension point
23 24 25 26 27 |
# File 'lib/hanami/action/params.rb', line 23 def self._base_rules lambda do optional(:_csrf_token).filled(:str?) end end |
.params(&blk) ⇒ Object
29 30 31 |
# File 'lib/hanami/action/params.rb', line 29 def self.params(&blk) validations(&blk || ->() {}) end |
Instance Method Details
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/hanami/action/params.rb', line 79 def (error_set = errors) error_set.each_with_object([]) do |(key, ), result| k = Utils::String.new(key).titleize = if .is_a?(Hash) () else .map { || "#{k} #{}" } end result.concat() end end |
#errors ⇒ Hash
Returns structured error messages
66 67 68 |
# File 'lib/hanami/action/params.rb', line 66 def errors @result. end |
#raw ⇒ Hash
Returns raw params from Rack env
53 54 55 |
# File 'lib/hanami/action/params.rb', line 53 def raw @input end |
#to_h ⇒ ::Hash Also known as: to_hash
Serialize params to Hash
111 112 113 |
# File 'lib/hanami/action/params.rb', line 111 def to_h @params end |
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
102 103 104 |
# File 'lib/hanami/action/params.rb', line 102 def valid? @result.success? end |