Class: Hanami::Action::Params
- Inherits:
-
Object
- Object
- Hanami::Action::Params
- 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.
Defined Under Namespace
Classes: DefaultContract, Errors
Class Attribute Summary collapse
- ._contract ⇒ Object readonly private
Instance Attribute Summary collapse
- #env ⇒ Object readonly private
-
#errors ⇒ Hash
readonly
Returns structured error messages.
- #raw ⇒ Object readonly private
Class Method Summary collapse
-
.params(&block) ⇒ Object
Defines validations for the params, using the ‘params` schema of a dry-validation contract.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns the value for the given params key.
-
#deconstruct_keys ⇒ ::Hash
Pattern-matching support.
-
#each {|key, value| ... } ⇒ to_h
Iterates over the params.
-
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages.
-
#get(*keys) ⇒ Object, NilClass
(also: #dig)
Returns an value associated with the given params key.
-
#initialize(env:, contract: nil) ⇒ Params
constructor
private
Initialize the params and freeze them.
-
#to_h ⇒ ::Hash
(also: #to_hash)
Serialize validated params to Hash.
-
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
Constructor Details
#initialize(env:, contract: nil) ⇒ 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.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/hanami/action/params.rb', line 197 def initialize(env:, contract: nil) @env = env @raw = _extract_params # Fall back to the default contract here, rather than in the `._contract` method itself. # This allows `._contract` to return nil when there is no user-defined contract, which is # important for the backwards compatibility behavior in `Validatable::ClassMethods#params`. contract ||= self.class._contract || DefaultContract validation = contract.call(raw) @params = validation.to_h @errors = Errors.new(validation.errors.to_h) freeze end |
Class Attribute Details
._contract ⇒ Object (readonly)
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.
157 158 159 |
# File 'lib/hanami/action/params.rb', line 157 def _contract @_contract end |
Instance Attribute Details
#env ⇒ Object (readonly)
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.
164 165 166 |
# File 'lib/hanami/action/params.rb', line 164 def env @env end |
#errors ⇒ Hash (readonly)
Returns structured error messages
187 188 189 |
# File 'lib/hanami/action/params.rb', line 187 def errors @errors end |
#raw ⇒ Object (readonly)
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.
170 171 172 |
# File 'lib/hanami/action/params.rb', line 170 def raw @raw end |
Class Method Details
.params(&block) ⇒ Object
Defines validations for the params, using the ‘params` schema of a dry-validation contract.
145 146 147 148 149 150 151 152 |
# File 'lib/hanami/action/params.rb', line 145 def self.params(&block) unless defined?(Dry::Validation::Contract) = %(To use `.params`, please add the "hanami-validations" gem to your Gemfile) raise NoMethodError, end @_contract = Class.new(Dry::Validation::Contract) { params(&block || -> {}) }.new end |
Instance Method Details
#[](key) ⇒ Object?
Returns the value for the given params key.
221 222 223 |
# File 'lib/hanami/action/params.rb', line 221 def [](key) @params[key] end |
#deconstruct_keys ⇒ ::Hash
Pattern-matching support
340 341 342 |
# File 'lib/hanami/action/params.rb', line 340 def deconstruct_keys(*) to_hash end |
#each {|key, value| ... } ⇒ to_h
Iterates over the params.
Calls the given block with each param key-value pair; returns the full hash of params.
321 322 323 |
# File 'lib/hanami/action/params.rb', line 321 def each(&blk) to_h.each(&blk) end |
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages
283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/hanami/action/params.rb', line 283 def (error_set = errors) error_set.each_with_object([]) do |(key, ), result| k = Utils::String.titleize(key) msgs = if .is_a?(::Hash) () else .map { || "#{k} #{}" } end result.concat(msgs) end end |
#get(*keys) ⇒ Object, NilClass Also known as: dig
Returns an value associated with the given params key.
You can access nested attributes by listing all the keys in the path. This uses the same key path semantics as ‘Hash#dig`.
257 258 259 |
# File 'lib/hanami/action/params.rb', line 257 def get(*keys) @params.dig(*keys) end |
#to_h ⇒ ::Hash Also known as: to_hash
Serialize validated params to Hash
330 331 332 |
# File 'lib/hanami/action/params.rb', line 330 def to_h @params end |
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
306 307 308 |
# File 'lib/hanami/action/params.rb', line 306 def valid? errors.empty? end |