Class: Hanami::Action::Params

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

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.

Since:

  • 0.1.0

{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • env (Hash)

    a Rack env or an hash of params.

Since:

  • 0.1.0



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

#envObject (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.

Since:

  • 0.7.0



124
125
126
# File 'lib/hanami/action/params.rb', line 124

def env
  @env
end

#errorsHash (readonly)

Returns structured error messages

Examples:

params.errors
  # => {
         :email=>["is missing", "is in invalid format"],
         :name=>["is missing"],
         :tos=>["is missing"],
         :age=>["is missing"],
         :address=>["is missing"]
       }

Returns:

  • (Hash)

Since:

  • 0.7.0



147
148
149
# File 'lib/hanami/action/params.rb', line 147

def errors
  @errors
end

#rawObject (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.

Since:

  • 0.7.0



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.

Parameters:

  • key (Symbol)

    the key

Returns:

  • (Object, nil)

    the associated value, if found

Since:

  • 0.7.0



181
182
183
# File 'lib/hanami/action/params.rb', line 181

def [](key)
  @params[key]
end

#deconstruct_keys::Hash

Pattern-matching support

Returns:

  • (::Hash)

Since:

  • 2.0.2



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.

Yield Parameters:

  • key (Symbol)
  • value (Object)

Returns:

Since:

  • 0.7.1



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

Examples:

params.error_messages
  # => [
         "Email is missing",
         "Email is in invalid format",
         "Name is missing",
         "Tos is missing",
         "Age is missing",
         "Address is missing"
       ]

Returns:

  • (Array)

Since:

  • 0.7.0



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/hanami/action/params.rb', line 243

def error_messages(error_set = errors)
  error_set.each_with_object([]) do |(key, messages), result|
    k = Utils::String.titleize(key)

    msgs = if messages.is_a?(::Hash)
             error_messages(messages)
           else
             messages.map { |message| "#{k} #{message}" }
           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`.

Examples:

require "hanami/action"

module Deliveries
  class Create < Hanami::Action
    def handle(request, *)
      request.params.get(:customer_name)     # => "Luca"
      request.params.get(:uknown)            # => nil

      request.params.get(:address, :city)    # => "Rome"
      request.params.get(:address, :unknown) # => nil

      request.params.get(:tags, 0)           # => "foo"
      request.params.get(:tags, 1)           # => "bar"
      request.params.get(:tags, 999)         # => nil

      request.params.get(nil)                # => nil
    end
  end
end

Parameters:

  • keys (Array<Symbol,Integer>)

    the key

Returns:

  • (Object, NilClass)

    return the associated value, if found

Since:

  • 0.7.0



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

Returns:

  • (::Hash)

Since:

  • 0.3.0



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.

Examples:

params.valid? # => true

Returns:

  • (TrueClass, FalseClass)

Since:

  • 0.7.0



266
267
268
# File 'lib/hanami/action/params.rb', line 266

def valid?
  errors.empty?
end