Class: Hanami::Action::BaseParams Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/action/base_params.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides access to params included in a Rack request.

Offers useful access to params via methods like #[], #get and #to_h.

These params are available via Request#params.

This class is used by default when Validatable is not included, or when no params validation schema is defined.

See Also:

Since:

  • 0.7.0

Direct Known Subclasses

Params

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ BaseParams

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.

Returns a new frozen params object for the Rack env.

Parameters:

  • env (Hash)

    a Rack env or an hash of params.

Since:

  • 0.7.0



40
41
42
43
44
45
# File 'lib/hanami/action/base_params.rb', line 40

def initialize(env)
  @env    = env
  @raw    = _extract_params
  @params = Utils::Hash.deep_symbolize(@raw)
  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



26
27
28
# File 'lib/hanami/action/base_params.rb', line 26

def env
  @env
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



32
33
34
# File 'lib/hanami/action/base_params.rb', line 32

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



55
56
57
# File 'lib/hanami/action/base_params.rb', line 55

def [](key)
  @params[key]
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



135
136
137
# File 'lib/hanami/action/base_params.rb', line 135

def each(&blk)
  to_h.each(&blk)
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/controller"

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

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

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

      req.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



91
92
93
# File 'lib/hanami/action/base_params.rb', line 91

def get(*keys)
  @params.dig(*keys)
end

#to_hHash Also known as: to_hash

Returns a hash of the parsed request params.

Returns:

  • (Hash)

Since:

  • 0.7.0



119
120
121
# File 'lib/hanami/action/base_params.rb', line 119

def to_h
  @params
end

#valid?TrueClass

Returns true at all times, providing a common interface with Params.

Returns:

  • (TrueClass)

    always returns true

See Also:

Since:

  • 0.7.0



109
110
111
# File 'lib/hanami/action/base_params.rb', line 109

def valid?
  true
end