Class: ParamsChecker::ParamChecker::NestedHashChecker
- Inherits:
-
Object
- Object
- ParamsChecker::ParamChecker::NestedHashChecker
- 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_field_error(message = '') ⇒ Object
- #add_nested_hash_error(message = '') ⇒ Object
- #call ⇒ Object
- #check_type ⇒ Object
- #formatted_nested_hash ⇒ Object
-
#initialize(key = '', schema = {}, params = {}, context = {}) ⇒ NestedHashChecker
constructor
A new instance of NestedHashChecker.
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
#class ⇒ Object
Returns the value of attribute class.
176 177 178 |
# File 'lib/params_checker/param_checker.rb', line 176 def class @class end |
#context ⇒ Object
Returns the value of attribute context.
176 177 178 |
# File 'lib/params_checker/param_checker.rb', line 176 def context @context end |
#key ⇒ Object
Returns the value of attribute key.
176 177 178 |
# File 'lib/params_checker/param_checker.rb', line 176 def key @key end |
#params ⇒ Object
Returns the value of attribute params.
176 177 178 |
# File 'lib/params_checker/param_checker.rb', line 176 def params @params end |
#schema ⇒ Object
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( = '') errors.add(key, ) end |
#add_nested_hash_error(message = '') ⇒ Object
168 169 170 |
# File 'lib/params_checker/param_checker.rb', line 168 def add_nested_hash_error( = '') errors.add(key, [:errors][0][:field_errors]) end |
#call ⇒ Object
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_type ⇒ Object
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_hash ⇒ Object
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 |