Class: Lotus::Action::Params
- Inherits:
-
Object
- Object
- Lotus::Action::Params
- 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.
Constant Summary collapse
- RACK_INPUT =
The key that returns raw input from the Rack env
'rack.input'.freeze
- ROUTER_PARAMS =
The key that returns router params from the Rack env This is a builtin integration for Lotus::Router
'router.params'.freeze
Instance Attribute Summary collapse
- #env ⇒ Object readonly private
- #raw ⇒ Object readonly
Class Method Summary collapse
-
.build_validation_class(&block) ⇒ Object
private
Overrides the method in Lotus::Validation to build a class that inherits from Params rather than only Lotus::Validations.
-
.param(name, options = {}, &block) ⇒ Object
Whitelist and validate a parameter.
- .whitelisting? ⇒ Boolean
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns the object associated with the given key.
-
#initialize(env) ⇒ Params
constructor
Initialize the params and freeze them.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns the Ruby’s hash.
Constructor Details
#initialize(env) ⇒ Params
Initialize the params and freeze them.
123 124 125 126 127 |
# File 'lib/lotus/action/params.rb', line 123 def initialize(env) @env = env super(_compute_params) # 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.
109 110 111 |
# File 'lib/lotus/action/params.rb', line 109 def env @env end |
#raw ⇒ Object (readonly)
114 115 116 |
# File 'lib/lotus/action/params.rb', line 114 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.
99 100 101 102 103 |
# File 'lib/lotus/action/params.rb', line 99 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
83 84 85 86 |
# File 'lib/lotus/action/params.rb', line 83 def self.param(name, = {}, &block) attribute name, , &block nil end |
.whitelisting? ⇒ Boolean
90 91 92 |
# File 'lib/lotus/action/params.rb', line 90 def self.whitelisting? defined_attributes.any? end |
Instance Method Details
#[](key) ⇒ Object?
Returns the object associated with the given key
136 137 138 |
# File 'lib/lotus/action/params.rb', line 136 def [](key) @attributes.get(key) end |
#to_h ⇒ Hash Also known as: to_hash
Returns the Ruby’s hash
145 146 147 |
# File 'lib/lotus/action/params.rb', line 145 def to_h @attributes.to_h end |