Class: ActiveFunction::Functions::StrongParameters::Parameters

Inherits:
Object
  • Object
show all
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

Instance Method Details

#[](attribute) ⇒ Parameters, Object

Allows access to parameters by key.

Parameters:

  • attribute (Symbol)

    The key of the parameter.

Returns:

  • (Parameters, Object)

    The value of the parameter.



59
60
61
# File 'lib/active_function/functions/strong_parameters.rb', line 59

def [](attribute)
  nested_attribute(params[attribute])
end

#hashObject



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.

Parameters:

  • attributes (Array<Symbol, Hash<Symbol, Array<Symbol>>>)

    The attributes to permit.

Returns:

  • (Parameters)

    A new instance with permitted 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.

Parameters:

  • attribute (Symbol)

    The key of the required parameter.

Returns:

  • (Parameters, Object)

    The value of the required parameter.

Raises:



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_hHash

Converts parameters to a hash.

Returns:

  • (Hash)

    The hash representation of the parameters.

Raises:



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