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.
Extracts the relevant params from a Rack env (query string, request body, and route params placed in ‘env` by the router), or from a plain hash passed for convenience in tests.
Defined Under Namespace
Classes: Errors
Constant Summary collapse
- EMPTY_PARAMS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{}.freeze
Instance Attribute Summary collapse
- #env ⇒ Object readonly private
-
#errors ⇒ Hash
readonly
Returns structured error messages.
- #raw ⇒ Object readonly private
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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/hanami/action/params.rb', line 157 def initialize(env:, contract: nil) @env = env @raw = _extract_params if contract validation = contract.call(raw) @params = validation.to_h @errors = Errors.new(validation.errors.to_h) else @params = raw.empty? ? EMPTY_PARAMS : Utils::Hash.deep_symbolize(raw) @errors = Errors.new end freeze 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.
124 125 126 |
# File 'lib/hanami/action/params.rb', line 124 def env @env end |
#errors ⇒ Hash (readonly)
Returns structured error messages
147 148 149 |
# File 'lib/hanami/action/params.rb', line 147 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.
130 131 132 |
# File 'lib/hanami/action/params.rb', line 130 def raw @raw end |
Instance Method Details
#[](key) ⇒ Object?
Returns the value for the given params key.
181 182 183 |
# File 'lib/hanami/action/params.rb', line 181 def [](key) @params[key] end |
#deconstruct_keys ⇒ ::Hash
Pattern-matching support
300 301 302 |
# File 'lib/hanami/action/params.rb', line 300 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.
281 282 283 |
# File 'lib/hanami/action/params.rb', line 281 def each(&blk) to_h.each(&blk) end |
#error_messages(error_set = errors) ⇒ Array
Returns flat collection of full error messages
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/hanami/action/params.rb', line 243 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`.
217 218 219 |
# File 'lib/hanami/action/params.rb', line 217 def get(*keys) @params.dig(*keys) end |
#to_h ⇒ ::Hash Also known as: to_hash
Serialize validated params to Hash
290 291 292 |
# File 'lib/hanami/action/params.rb', line 290 def to_h @params end |
#valid? ⇒ TrueClass, FalseClass
Returns true if no validation errors are found, false otherwise.
266 267 268 |
# File 'lib/hanami/action/params.rb', line 266 def valid? errors.empty? end |