Class: ParamsChecker::ParamChecker::NestedHashChecker

Inherits:
Object
  • Object
show all
Includes:
SimpleCommand
Defined in:
lib/params_checker/param_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = '', schema = {}, params = {}, context = {}) ⇒ NestedHashChecker



141
142
143
144
145
146
# File 'lib/params_checker/param_checker.rb', line 141

def initialize(key = '', schema = {}, params = {}, context = {})
  @key = key
  @schema = schema
  @params = params
  @context = context
end

Instance Attribute Details

#classObject

Returns the value of attribute class.



176
177
178
# File 'lib/params_checker/param_checker.rb', line 176

def class
  @class
end

#contextObject

Returns the value of attribute context.



176
177
178
# File 'lib/params_checker/param_checker.rb', line 176

def context
  @context
end

#keyObject

Returns the value of attribute key.



176
177
178
# File 'lib/params_checker/param_checker.rb', line 176

def key
  @key
end

#paramsObject

Returns the value of attribute params.



176
177
178
# File 'lib/params_checker/param_checker.rb', line 176

def params
  @params
end

#schemaObject

Returns the value of attribute schema.



176
177
178
# File 'lib/params_checker/param_checker.rb', line 176

def schema
  @schema
end

Instance Method Details

#add_field_error(message = '') ⇒ Object



172
173
174
# File 'lib/params_checker/param_checker.rb', line 172

def add_field_error(message = '')
  errors.add(key, message)
end

#add_nested_hash_error(message = '') ⇒ Object



168
169
170
# File 'lib/params_checker/param_checker.rb', line 168

def add_nested_hash_error(message = '')
  errors.add(key, message[:errors][0][:field_errors])
end

#callObject



148
149
150
151
152
# File 'lib/params_checker/param_checker.rb', line 148

def call
  return nil if schema[key][:allow_nil] && params[key].nil?

  check_type && formatted_nested_hash
end

#check_typeObject



161
162
163
164
165
166
# File 'lib/params_checker/param_checker.rb', line 161

def check_type
  valid = params[key].is_a?(ActionController::Parameters) || params[key].is_a?(Hash)
  add_field_error("This field's type must be object or ActionController::Parameters.") unless valid

  valid
end

#formatted_nested_hashObject



154
155
156
157
158
159
# File 'lib/params_checker/param_checker.rb', line 154

def formatted_nested_hash
  cmd = schema[key][:class].call(params: params[key], context: context, is_outest_hash: false)
  return cmd.result if cmd.success?

  add_nested_hash_error(cmd.errors)
end