Class: Hanami::Action::BaseParams

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

Overview

Since:

  • 0.1.0

Direct Known Subclasses

Params

Constant Summary collapse

RACK_INPUT =

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.

The key that returns raw input from the Rack env

Since:

  • 0.7.0

'rack.input'.freeze
ROUTER_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.

The key that returns router params from the Rack env This is a builtin integration for Hanami::Router

Since:

  • 0.7.0

'router.params'.freeze
RACK_SESSION =

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.

The key that returns Rack session params from the Rack env Please note that this is used only when an action is unit tested.

Examples:

# action unit test
action.call('rack.session' => { 'foo' => 'bar' })
action.session[:foo] # => "bar"

Since:

  • 1.0.0

'rack.session'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 0.7.0



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

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



36
37
38
# File 'lib/hanami/action/base_params.rb', line 36

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



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

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object?

Returns the object associated with the given key

Since:

  • 0.7.0



66
67
68
# File 'lib/hanami/action/base_params.rb', line 66

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

#each(&blk) ⇒ Object

Iterates through params

Since:

  • 0.7.1



137
138
139
# File 'lib/hanami/action/base_params.rb', line 137

def each(&blk)
  to_h.each(&blk)
end

#get(*keys) ⇒ Object, NilClass Also known as: dig

Get an attribute value associated with the given key. Nested attributes are reached by listing all the keys to get to the value.

Examples:

require 'hanami/controller'

module Deliveries
  class Create
    include Hanami::Action

    def call(params)
      params.get(:customer_name)     # => "Luca"
      params.get(:uknown)            # => nil

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

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

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

Since:

  • 0.7.0



101
102
103
# File 'lib/hanami/action/base_params.rb', line 101

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

#to_h::Hash Also known as: to_hash

Serialize params to Hash

Since:

  • 0.7.0



127
128
129
# File 'lib/hanami/action/base_params.rb', line 127

def to_h
  @params
end

#valid?TrueClass

Provide a common interface with Params

See Also:

Since:

  • 0.7.0



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

def valid?
  true
end