Class: ActiveFunction::Functions::StrongParameters::Parameters
- Inherits:
-
Object
- Object
- ActiveFunction::Functions::StrongParameters::Parameters
- Defined in:
- lib/active_function/functions/strong_parameters.rb
Overview
The Parameters class encapsulates the parameter handling logic.
Defined Under Namespace
Classes: ParameterMissingError, UnpermittedParameterError
Instance Method Summary collapse
-
#[](attribute) ⇒ Parameters, Object
Allows access to parameters by key.
- #hash ⇒ Object
-
#permit(*attributes) ⇒ Parameters
Specifies the allowed parameters.
-
#require(attribute) ⇒ Parameters, Object
Requires the presence of a specific parameter.
-
#to_h ⇒ Hash
Converts parameters to a hash.
- #with(params:, permitted: false) ⇒ Object
Instance Method Details
#[](attribute) ⇒ Parameters, Object
Allows access to parameters by key.
59 60 61 |
# File 'lib/active_function/functions/strong_parameters.rb', line 59 def [](attribute) nested_attribute(params[attribute]) end |
#hash ⇒ Object
108 109 110 |
# File 'lib/active_function/functions/strong_parameters.rb', line 108 def hash @attributes.to_h.hash end |
#permit(*attributes) ⇒ Parameters
Specifies the allowed parameters.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/active_function/functions/strong_parameters.rb', line 80 def permit(*attributes) pparams = {} attributes.each do |attribute| if attribute.is_a? Hash attribute.each do |k, v| pparams[k] = process_nested(self[k], :permit, v) end else next unless params.key?(attribute) pparams[attribute] = self[attribute] end end with(params: pparams, permitted: true) end |
#require(attribute) ⇒ Parameters, Object
Requires the presence of a specific parameter.
68 69 70 71 72 73 74 |
# File 'lib/active_function/functions/strong_parameters.rb', line 68 def require(attribute) if (value = self[attribute]) value else raise ParameterMissingError, attribute end end |
#to_h ⇒ Hash
Converts parameters to a hash.
102 103 104 105 106 |
# File 'lib/active_function/functions/strong_parameters.rb', line 102 def to_h raise UnpermittedParameterError, params.keys unless permitted params.transform_values { process_nested(_1, :to_h) } end |
#with(params:, permitted: false) ⇒ Object
112 113 114 |
# File 'lib/active_function/functions/strong_parameters.rb', line 112 def with(params:, permitted: false) self.class.new(params, permitted) end |