lib/active_function/functions/strong_parameters.rb in activefunction-0.3.5 vs lib/active_function/functions/strong_parameters.rb in activefunction-0.4.0

- old
+ new

@@ -1,56 +1,49 @@ # frozen_string_literal: true require "forwardable" module ActiveFunction - class ParameterMissingError < Error - MESSAGE_TEMPLATE = "Missing parameter: %s" + module Functions + module StrongParameters + ActiveFunction.register_plugin :strong_parameters, self - attr_reader :message + Error = Class.new(StandardError) - def initialize(param) - MESSAGE_TEMPLATE % param - end - end + class Parameters < Data.define(:params, :permitted) + class ParameterMissingError < Error + MESSAGE_TEMPLATE = "Missing parameter: %s" - class UnpermittedParameterError < Error - MESSAGE_TEMPLATE = "Unpermitted parameter: %s" + attr_reader :message - attr_reader :message + def initialize(param) + MESSAGE_TEMPLATE % param + end + end - def initialize(param) - MESSAGE_TEMPLATE % param - end - end + class UnpermittedParameterError < Error + MESSAGE_TEMPLATE = "Unpermitted parameter: %s" - module Functions - module StrongParameters - def params - @_params ||= Parameters.new(@request) - end + attr_reader :message - class Parameters - extend Forwardable - def_delegators :@parameters, :each, :map - include Enumerable - - def initialize(parameters, permitted: false) - @parameters = parameters - @permitted = permitted + def initialize(param) + MESSAGE_TEMPLATE % param + end end + protected :params + def [](attribute) - nested_attribute(parameters[attribute]) + nested_attribute(params[attribute]) end def require(attribute) - value = self[attribute] - - raise ParameterMissingError, attribute if value.nil? - - value + if (value = self[attribute]) + value + else + raise ParameterMissingError, attribute + end end def permit(*attributes) pparams = {} @@ -58,32 +51,41 @@ if attribute.is_a? Hash attribute.each do |k, v| pparams[k] = process_nested(self[k], :permit, v) end else - next unless parameters.key?(attribute) + next unless params.key?(attribute) pparams[attribute] = self[attribute] end end - Parameters.new(pparams, permitted: true) + with(params: pparams, permitted: true) end + # Redefines RubyNext::Core::Data instance methods def to_h - raise UnpermittedParameterError, parameters.keys unless @permitted + raise UnpermittedParameterError, params.keys unless permitted - parameters.transform_values { process_nested(_1, :to_h) } + params.transform_values { process_nested(_1, :to_h) } end + def hash + @attributes.to_h.hash + end + + def with(params:, permitted: false) + self.class.new(params, permitted) + end + private def nested_attribute(attribute) if attribute.is_a? Hash - Parameters.new(attribute) + with(params: attribute) elsif attribute.is_a?(Array) && attribute[0].is_a?(Hash) - attribute.map { Parameters.new(_1) } + attribute.map { |it| with(params: it) } else attribute end end @@ -94,11 +96,13 @@ attribute.map { _1.send(method, *options) } else attribute end end + end - attr_reader :parameters + def params + @_params ||= Parameters.new(@request, false) end end end end