-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from K-S-A/master
Add support for updated ActionController::Parameters
- Loading branch information
Showing
2 changed files
with
91 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,112 @@ | ||
require "params_converter/version" | ||
# frozen_string_literal: true | ||
|
||
module ParamsConverter | ||
require 'params_converter/version' | ||
|
||
class ParamsConverterError < ::StandardError | ||
module ParamsConverter # :nodoc: | ||
class ParamsConverterError < ::StandardError # :nodoc: | ||
def initialize(msg = nil) | ||
msg = msg.is_a?(Array) ? msg.join(',') : msg.to_s | ||
super("#{message_prefix} #{msg}") | ||
end | ||
end | ||
|
||
class NotAllowedError < ParamsConverterError | ||
class NotAllowedError < ParamsConverterError # :nodoc: | ||
def message_prefix | ||
"Not allowed keys" | ||
'Not allowed keys' | ||
end | ||
end | ||
|
||
class MissingRequiredError < ParamsConverterError | ||
class MissingRequiredError < ParamsConverterError # :nodoc: | ||
def message_prefix | ||
"Missing keys" | ||
'Missing keys' | ||
end | ||
end | ||
|
||
def convert!(params, re=nil, pe=nil) | ||
raise ArgumentError unless re.nil? or re.is_a?(Array) | ||
raise ArgumentError unless pe.nil? or pe.is_a?(Array) | ||
raise ArgumentError unless params.is_a?(Hash) | ||
class Adjustor # :nodoc: | ||
attr_reader :params, | ||
:required_keys, | ||
:allowed_keys | ||
|
||
params = Hash[params.map { |k,v| [k.to_s.to_sym, v] }] | ||
def initialize(params, required_keys, allowed_keys) | ||
@params = check_and_symbolize_keys!(params, Hash) | ||
@required_keys = check_and_symbolize!(required_keys, nil, Array) | ||
@allowed_keys = check_and_symbolize!(allowed_keys, nil, Array) | ||
end | ||
|
||
def perform | ||
required_keys_filter unless required_keys.nil? | ||
allowed_keys_filter unless allowed_keys.nil? | ||
params | ||
end | ||
|
||
private | ||
|
||
def required_keys_filter | ||
raise_if_any(MissingRequiredError, missing_keys) | ||
end | ||
|
||
def allowed_keys_filter | ||
raise_if_any(NotAllowedError, not_allowed_keys) | ||
end | ||
|
||
unless re.nil? | ||
re = re.map(&:to_s).map(&:to_sym).uniq | ||
missing_keys = re.reject { |r| !!params.fetch(r, false) } | ||
raise MissingRequiredError.new(missing_keys) if missing_keys.count > 0 | ||
def missing_keys | ||
@missing_keys ||= required_keys - param_keys | ||
end | ||
|
||
pe = params.keys if pe.nil? | ||
pe = ((re || []) | pe).map(&:to_s).map(&:to_sym).uniq | ||
not_allowed_keys = params.keys - pe | ||
raise NotAllowedError.new(not_allowed_keys) if not_allowed_keys.count > 0 | ||
def not_allowed_keys | ||
@not_allowed_keys ||= param_keys - [*allowed_keys, *required_keys] | ||
end | ||
|
||
def param_keys | ||
@param_keys ||= params.keys | ||
end | ||
|
||
def raise_if_any(error, keys) | ||
raise(error, keys) if keys.any? | ||
end | ||
|
||
def check_and_symbolize_keys!(value, *allowed_values) | ||
value = prepare(value) | ||
check_params!(value, allowed_values) | ||
hash_with_symbolized_keys(value) | ||
end | ||
|
||
params | ||
def prepare(value) | ||
value = value.to_unsafe_h if value.respond_to?(:to_unsafe_h) | ||
value = value.to_h if value.respond_to?(:to_h) | ||
value | ||
end | ||
|
||
def check_params!(value, allowed_values) | ||
case value | ||
when *allowed_values | ||
value | ||
else | ||
raise ArgumentError | ||
end | ||
end | ||
|
||
def check_and_symbolize!(value, *allowed_values) | ||
check_params!(value, allowed_values) | ||
return if value.nil? | ||
symbolized_uniq_array(value) | ||
end | ||
|
||
def symbolized_uniq_array(value) | ||
value.map { |v| symbolize(v) }.uniq | ||
end | ||
|
||
def symbolize(value) | ||
value.to_s.to_sym | ||
end | ||
|
||
def hash_with_symbolized_keys(value) | ||
Hash[value.map { |k, v| [symbolize(k), v] }] | ||
end | ||
end | ||
module_function :convert! | ||
|
||
def convert!(params, required_keys = nil, allowed_keys = nil) | ||
Adjustor.new(params, required_keys, allowed_keys).perform | ||
end | ||
module_function :convert! | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module ParamsConverter | ||
VERSION = "0.1.0" | ||
VERSION = '0.1.0' | ||
end |