Class: ParamsChecker::ParamChecker::NestedHashsChecker
- Inherits:
-
Object
- Object
- ParamsChecker::ParamChecker::NestedHashsChecker
- Includes:
- SimpleCommand
- Defined in:
- lib/params_checker/param_checker.rb
Instance Attribute Summary collapse
-
#class ⇒ Object
Returns the value of attribute class.
-
#context ⇒ Object
Returns the value of attribute context.
-
#key ⇒ Object
Returns the value of attribute key.
-
#params ⇒ Object
Returns the value of attribute params.
-
#schema ⇒ Object
Returns the value of attribute schema.
Instance Method Summary collapse
- #add_errors ⇒ Object
- #add_field_error(message = '') ⇒ Object
- #call ⇒ Object
- #check_type ⇒ Object
- #formatted_nested_hash(nested_hash) ⇒ Object
- #formatted_nested_hashs ⇒ Object
- #get_error(nested_hash) ⇒ Object
-
#initialize(key = '', schema = {}, params = {}, context = {}) ⇒ NestedHashsChecker
constructor
A new instance of NestedHashsChecker.
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
#class ⇒ Object
Returns the value of attribute class.
239 240 241 |
# File 'lib/params_checker/param_checker.rb', line 239 def class @class end |
#context ⇒ Object
Returns the value of attribute context.
239 240 241 |
# File 'lib/params_checker/param_checker.rb', line 239 def context @context end |
#key ⇒ Object
Returns the value of attribute key.
239 240 241 |
# File 'lib/params_checker/param_checker.rb', line 239 def key @key end |
#params ⇒ Object
Returns the value of attribute params.
239 240 241 |
# File 'lib/params_checker/param_checker.rb', line 239 def params @params end |
#schema ⇒ Object
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_errors ⇒ Object
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( = '') errors.add(key, ) end |
#call ⇒ Object
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_type ⇒ Object
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_hashs ⇒ Object
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 |