Class: Hanami::V1::Action::Params
- Inherits:
-
BaseParams
- Object
- BaseParams
- Hanami::V1::Action::Params
- Includes:
- Validations::Form
- Defined in:
- lib/hanami/v1/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.
Defined Under Namespace
Classes: Errors
Constant Summary
Constants inherited from BaseParams
BaseParams::DEFAULT_REQUEST_METHOD, BaseParams::RACK_INPUT, BaseParams::RACK_SESSION, BaseParams::REQUEST_METHOD, BaseParams::ROUTER_PARAMS
Instance Attribute Summary collapse
-
#errors ⇒ Hash
readonly
Returns structured error messages.
Attributes inherited from BaseParams
Class Method Summary collapse
-
._base_rules ⇒ Object
private
This is a Hanami::Validations extension point.
-
.params(&blk) ⇒ Object
Define params validations.
Instance Method Summary collapse
-
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages.
-
#initialize(env) ⇒ Params
constructor
private
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 included from Validations::Form
Methods inherited from BaseParams
Constructor Details
#initialize(env) ⇒ Params
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.
Initialize the params and freeze them.
164 165 166 167 168 169 170 171 |
# File 'lib/hanami/v1/action/params.rb', line 164 def initialize(env) @env = env super(_extract_params) @result = validate @params = _params @errors = Errors.new(@result.) freeze end |
Instance Attribute Details
#errors ⇒ Hash (readonly)
Returns structured error messages
191 192 193 |
# File 'lib/hanami/v1/action/params.rb', line 191 def errors @errors 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
118 119 120 121 122 |
# File 'lib/hanami/v1/action/params.rb', line 118 def self._base_rules lambda do optional(:_csrf_token).filled(:str?) end end |
.params(&blk) ⇒ Object
Define params validations
152 153 154 |
# File 'lib/hanami/v1/action/params.rb', line 152 def self.params(&blk) validations(&blk || ->() {}) end |
Instance Method Details
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/hanami/v1/action/params.rb', line 202 def (error_set = errors) error_set.each_with_object([]) do |(key, ), result| k = Utils::String.titleize(key) = if .is_a?(Hash) () else .map { || "#{k} #{message}" } end result.concat() end end |
#raw ⇒ Hash
Returns raw params from Rack env
178 179 180 |
# File 'lib/hanami/v1/action/params.rb', line 178 def raw @input end |
#to_h ⇒ ::Hash Also known as: to_hash
Serialize params to Hash
234 235 236 |
# File 'lib/hanami/v1/action/params.rb', line 234 def to_h @params end |
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
225 226 227 |
# File 'lib/hanami/v1/action/params.rb', line 225 def valid? errors.empty? end |