Class: ParamsChecker::ParamChecker::NestedHashsChecker

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 = {}) ⇒ NestedHashsChecker

Returns a new instance of NestedHashsChecker.



181
182
183
184
185
186
# File 'lib/params_checker/param_checker.rb', line 181

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.



239
240
241
# File 'lib/params_checker/param_checker.rb', line 239

def class
  @class
end

#contextObject

Returns the value of attribute context.



239
240
241
# File 'lib/params_checker/param_checker.rb', line 239

def context
  @context
end

#keyObject

Returns the value of attribute key.



239
240
241
# File 'lib/params_checker/param_checker.rb', line 239

def key
  @key
end

#paramsObject

Returns the value of attribute params.



239
240
241
# File 'lib/params_checker/param_checker.rb', line 239

def params
  @params
end

#schemaObject

Returns the value of attribute schema.



239
240
241
# File 'lib/params_checker/param_checker.rb', line 239

def schema
  @schema
end

Instance Method Details

#add_errorsObject



194
195
196
197
198
199
200
201
# File 'lib/params_checker/param_checker.rb', line 194

def add_errors
  all_errors = params[key].map do |nested_hash|
    get_error(nested_hash)
  end
  return true if all_errors.all?(&:nil?)

  errors.add(key, all_errors)
end

#add_field_error(message = '') ⇒ Object



235
236
237
# File 'lib/params_checker/param_checker.rb', line 235

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

#callObject



188
189
190
191
192
# File 'lib/params_checker/param_checker.rb', line 188

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

  check_type && add_errors && formatted_nested_hashs
end

#check_typeObject



229
230
231
232
233
# File 'lib/params_checker/param_checker.rb', line 229

def check_type
  valid = params[key].is_a?(Array)
  add_field_error("This field's type must be array.") unless valid
  valid
end

#formatted_nested_hash(nested_hash) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/params_checker/param_checker.rb', line 220

def formatted_nested_hash(nested_hash)
  cmd = schema[key][:class].call(
    params: nested_hash,
    context: context,
    is_outest_hash: false
  )
  cmd.result
end

#formatted_nested_hashsObject



214
215
216
217
218
# File 'lib/params_checker/param_checker.rb', line 214

def formatted_nested_hashs
  params[key].map do |nested_hash|
    formatted_nested_hash(nested_hash)
  end
end

#get_error(nested_hash) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/params_checker/param_checker.rb', line 203

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

  cmd.errors[:errors][0][:field_errors]
end