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
- CSRF_TOKEN =
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.
CSRF params key
This key is shared with
lotusrb
andlotus-helpers
'_csrf_token'.freeze
- DEFAULT_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.
Set of params that are never filtered
Hash[CSRF_TOKEN => true].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
'.'.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.
-
#_csrf_token=(value) ⇒ Object
private
Assign CSRF Token.
-
#get(key) ⇒ Object, NilClass
Get an attribute value associated with the given key.
-
#initialize(env) ⇒ Params
constructor
Initialize the params and freeze them.
-
#to_h ⇒ ::Hash
(also: #to_hash)
Serialize params to Hash.
Constructor Details
#initialize(env) ⇒ Params
Initialize the params and freeze them.
149 150 151 152 153 |
# File 'lib/lotus/action/params.rb', line 149 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.
135 136 137 |
# File 'lib/lotus/action/params.rb', line 135 def env @env end |
#raw ⇒ Object (readonly)
140 141 142 |
# File 'lib/lotus/action/params.rb', line 140 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.
121 122 123 124 125 126 127 128 129 |
# File 'lib/lotus/action/params.rb', line 121 def self.build_validation_class(&block) kls = Class.new(Params) do def lotus_nested_attributes? true end end kls.class_eval(&block) kls end |
.param(name, options = {}, &block) ⇒ Object
Whitelist and validate a parameter
105 106 107 108 |
# File 'lib/lotus/action/params.rb', line 105 def self.param(name, = {}, &block) attribute name, , &block nil end |
.whitelisting? ⇒ Boolean
112 113 114 |
# File 'lib/lotus/action/params.rb', line 112 def self.whitelisting? defined_attributes.any? end |
Instance Method Details
#[](key) ⇒ Object?
Returns the object associated with the given key
162 163 164 |
# File 'lib/lotus/action/params.rb', line 162 def [](key) @attributes.get(key) end |
#_csrf_token=(value) ⇒ 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.
Assign CSRF Token. This method is here for compatibility with Lotus::Validations
.
NOTE: When we will not support indifferent access anymore, we can probably remove this method.
230 231 232 |
# File 'lib/lotus/action/params.rb', line 230 def _csrf_token=(value) @attributes.set(CSRF_TOKEN, value) end |
#get(key) ⇒ Object, NilClass
Get an attribute value associated with the given key. Nested attributes are reached with a dot notation.
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/lotus/action/params.rb', line 200 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
217 218 219 |
# File 'lib/lotus/action/params.rb', line 217 def to_h @attributes.to_h end |