Class: Lotus::Action::Params

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/lotus/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 Lotus::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.

Since:

  • 0.1.0

Constant Summary collapse

RACK_INPUT =

The key that returns raw input from the Rack env

Since:

  • 0.1.0

'rack.input'.freeze
ROUTER_PARAMS =

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

Since:

  • 0.1.0

'router.params'.freeze
GET_SEPARATOR =

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.

Separator for #get

See Also:

Since:

  • 0.4.0

'.'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Params

Initialize the params and freeze them.

Parameters:

  • env (Hash)

    a Rack env or an hash of params.

Since:

  • 0.1.0



131
132
133
134
135
# File 'lib/lotus/action/params.rb', line 131

def initialize(env)
  @env = env
  super(_compute_params)
  # 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.2.0



117
118
119
# File 'lib/lotus/action/params.rb', line 117

def env
  @env
end

#rawObject (readonly)

Since:

  • 0.3.2



122
123
124
# File 'lib/lotus/action/params.rb', line 122

def raw
  @raw
end

Class Method Details

.build_validation_class(&block) ⇒ Object

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.

Overrides the method in Lotus::Validation to build a class that inherits from Params rather than only Lotus::Validations.

Since:

  • 0.3.2



107
108
109
110
111
# File 'lib/lotus/action/params.rb', line 107

def self.build_validation_class(&block)
  kls = Class.new(Params)
  kls.class_eval(&block)
  kls
end

.param(name, options = {}, &block) ⇒ Object

Whitelist and validate a parameter

Examples:

Whitelisting

require 'lotus/controller'

class SignupParams < Lotus::Action::Params
  param :email
end

params = SignupParams.new({id: 23, email: '[email protected]'})

params[:email] # => '[email protected]'
params[:id]    # => nil

Validation

require 'lotus/controller'

class SignupParams < Lotus::Action::Params
  param :email, presence: true
end

params = SignupParams.new({})

params[:email] # => nil
params.valid?  # => false

Unknown validation

require 'lotus/controller'

class SignupParams < Lotus::Action::Params
  param :email, unknown: true # => raise ArgumentError
end

Wrong size validation

require 'lotus/controller'

class SignupParams < Lotus::Action::Params
  param :email, size: 'twentythree'
end

params = SignupParams.new({})
params.valid? # => raise ArgumentError

Parameters:

  • name (#to_sym)

    The name of the param to whitelist

Returns:

  • void

Raises:

  • (ArgumentError)

    if one the validations is unknown, or if the size validator is used with an object that can’t be coerced to integer.

See Also:

Since:

  • 0.3.0



91
92
93
94
# File 'lib/lotus/action/params.rb', line 91

def self.param(name, options = {}, &block)
  attribute name, options, &block
  nil
end

.whitelisting?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



98
99
100
# File 'lib/lotus/action/params.rb', line 98

def self.whitelisting?
  defined_attributes.any?
end

Instance Method Details

#[](key) ⇒ Object?

Returns the object associated with the given key

Parameters:

  • key (Symbol)

    the key

Returns:

  • (Object, nil)

    return the associated object, if found

Since:

  • 0.2.0



144
145
146
# File 'lib/lotus/action/params.rb', line 144

def [](key)
  @attributes.get(key)
end

#get(key) ⇒ Object, NilClass

Get an attribute value associated with the given key. Nested attributes are reached with a dot notation.

Examples:

require 'lotus/controller'

module Deliveries
  class Create
    include Lotus::Action

    params do
      param :customer_name
      param :address do
        param :city
      end
    end

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

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

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

Parameters:

  • key (String)

    the key

Returns:

  • (Object, NilClass)

    return the associated value, if found

Since:

  • 0.4.0



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lotus/action/params.rb', line 182

def get(key)
  key, *keys = key.to_s.split(GET_SEPARATOR)
  result     = self[key]

  Array(keys).each do |k|
    break if result.nil?
    result = result[k]
  end

  result
end

#to_h::Hash Also known as: to_hash

Serialize params to Hash

Returns:

  • (::Hash)

Since:

  • 0.3.0



199
200
201
# File 'lib/lotus/action/params.rb', line 199

def to_h
  @attributes.to_h
end