Class: ParamsChecker::BaseParamsChecker
- Inherits:
-
Object
- Object
- ParamsChecker::BaseParamsChecker
- Includes:
- Fields, SimpleCommand
- Defined in:
- lib/params_checker/base_params_checker.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#formatted_params_after_custom_all_fields_check ⇒ Object
Returns the value of attribute formatted_params_after_custom_all_fields_check.
-
#formatted_params_after_custom_specify_field_checks ⇒ Object
Returns the value of attribute formatted_params_after_custom_specify_field_checks.
-
#formatted_params_after_default_check ⇒ Object
Returns the value of attribute formatted_params_after_default_check.
-
#is_outest_hash ⇒ Object
Returns the value of attribute is_outest_hash.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #add_error ⇒ Object
- #all_fields_are_valid ⇒ Object
- #all_fields_check ⇒ Object
- #call ⇒ Object
- #check(params) ⇒ Object
- #check_base_on_field_type(key) ⇒ Object
- #custom_check ⇒ Object
- #data_valid?(key) ⇒ Boolean
- #default_check ⇒ Object
- #error_exist? ⇒ Boolean
- #fields ⇒ Object
- #formatted_params ⇒ Object
- #init ⇒ Object
-
#initialize(params: {}, context: {}, is_outest_hash: true) ⇒ BaseParamsChecker
constructor
A new instance of BaseParamsChecker.
- #raise_error(message = "Invalid data") ⇒ Object
- #specify_field_check(key) ⇒ Object
- #specify_field_checks ⇒ Object
- #value_need_to_be_present?(key) ⇒ Boolean
- #value_present?(key) ⇒ Boolean
- #value_valid?(key) ⇒ Boolean
Methods included from Fields
#arr_field, #bigint_field, #bignum_field, #boolean_field, #char_field, #date_field, #datetime_field, #email_field, #int_field, #num_field, #positive_bigint_field, #positive_bignum_field, #positive_int_field, #positive_num_field, #text_field, #time_field
Constructor Details
#initialize(params: {}, context: {}, is_outest_hash: true) ⇒ BaseParamsChecker
Returns a new instance of BaseParamsChecker.
5 6 7 8 9 10 11 12 |
# File 'lib/params_checker/base_params_checker.rb', line 5 def initialize(params: {}, context: {}, is_outest_hash: true) @params = params @context = context @is_outest_hash = is_outest_hash @formatted_params_after_default_check = {} @formatted_params_after_custom_specify_field_checks = {} @formatted_params_after_custom_all_fields_check = {} end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def context @context end |
#formatted_params_after_custom_all_fields_check ⇒ Object
Returns the value of attribute formatted_params_after_custom_all_fields_check.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def formatted_params_after_custom_all_fields_check @formatted_params_after_custom_all_fields_check end |
#formatted_params_after_custom_specify_field_checks ⇒ Object
Returns the value of attribute formatted_params_after_custom_specify_field_checks.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def formatted_params_after_custom_specify_field_checks @formatted_params_after_custom_specify_field_checks end |
#formatted_params_after_default_check ⇒ Object
Returns the value of attribute formatted_params_after_default_check.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def formatted_params_after_default_check @formatted_params_after_default_check end |
#is_outest_hash ⇒ Object
Returns the value of attribute is_outest_hash.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def is_outest_hash @is_outest_hash end |
#params ⇒ Object
Returns the value of attribute params.
231 232 233 |
# File 'lib/params_checker/base_params_checker.rb', line 231 def params @params end |
Class Method Details
.init(required: true, many: false, allow_empty: false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/params_checker/base_params_checker.rb', line 14 def self.init required: true, many: false, allow_empty: false raise "This field's type must be boolean." if [required, many, allow_empty].any? { |value| !value.in? [true, false] } type = many ? 'nested_hashs' : 'nested_hash' { type: type, required: required, many: many, allow_empty: allow_empty, class: self } end |
Instance Method Details
#add_error ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/params_checker/base_params_checker.rb', line 216 def add_error() # only add errors skeleton at the outest hash return unless is_outest_hash details = {} errors.each do |key, value| details[key] = value errors.delete(key) end errors.add(:errors, { message: 'Invalid data', details: details }) end |
#all_fields_are_valid ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/params_checker/base_params_checker.rb', line 81 def all_fields_are_valid return @all_fields_are_valid if @all_fields_are_valid.present? @all_fields_are_valid = true fields.each do |key, value| @all_fields_are_valid = false unless data_valid? key end @all_fields_are_valid end |
#all_fields_check ⇒ Object
200 201 202 |
# File 'lib/params_checker/base_params_checker.rb', line 200 def all_fields_check @formatted_params_after_custom_all_fields_check = check formatted_params_after_custom_specify_field_checks end |
#call ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/params_checker/base_params_checker.rb', line 31 def call default_check && custom_check error_exist? && add_error formatted_params rescue => e # only add errors at the first hash, else raise error for the first hash to catch # example: # old: # { # "errors": { # "message": "Invalid data", # "details": { # "purchase_order_items": [ # { # "errors": { # "message": "Material not exists.", # "details": {} # } # } # ] # } # } # } # new: # { # "errors": { # "message": "Material not exists.", # "details": {} # } # } if e.class.name == 'ParamsChecker::MyError' && is_outest_hash errors.add(:errors, { message: e, details: {} }) else raise e end end |
#check(params) ⇒ Object
208 209 210 |
# File 'lib/params_checker/base_params_checker.rb', line 208 def check params params end |
#check_base_on_field_type(key) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/params_checker/base_params_checker.rb', line 147 def check_base_on_field_type key case fields[key][:type] when 'num' ParamChecker::NumParamChecker.call key, fields, params when 'int' ParamChecker::IntParamChecker.call key, fields, params when 'char' ParamChecker::CharParamChecker.call key, fields, params when 'text' ParamChecker::CharParamChecker.call key, fields, params when 'arr' ParamChecker::ArrParamChecker.call key, fields, params when 'nested_hash' ParamChecker::NestedHashChecker.call key, fields, params, context when 'nested_hashs' ParamChecker::NestedHashsChecker.call key, fields, params, context when 'date' ParamChecker::DateParamChecker.call key, fields, params when 'time' ParamChecker::TimeParamChecker.call key, fields, params when 'datetime' ParamChecker::DateTimeParamChecker.call key, fields, params when 'email' ParamChecker::EmailParamChecker.call key, fields, params when 'boolean' ParamChecker::BooleanChecker.call key, fields, params end end |
#custom_check ⇒ Object
176 177 178 |
# File 'lib/params_checker/base_params_checker.rb', line 176 def custom_check specify_field_checks && all_fields_check end |
#data_valid?(key) ⇒ Boolean
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/params_checker/base_params_checker.rb', line 91 def data_valid? key if value_need_to_be_present?(key) if value_present?(key) if value_valid?(key) true else false end else errors.add(key, 'This field is required.') false end else if value_present?(key) if value_valid?(key) true else false end else true end end end |
#default_check ⇒ Object
75 76 77 78 79 |
# File 'lib/params_checker/base_params_checker.rb', line 75 def default_check params_is_a_hash = params.is_a?(ActionController::Parameters) || params.is_a?(Hash) errors.add(:error, "ParamsChecker only receive object or ActionController::Parameters as input.") unless params_is_a_hash params_is_a_hash && all_fields_are_valid end |
#error_exist? ⇒ Boolean
71 72 73 |
# File 'lib/params_checker/base_params_checker.rb', line 71 def error_exist? !errors.empty? && errors.is_a?(Hash) end |
#fields ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/params_checker/base_params_checker.rb', line 139 def fields field_params = {} init.each do |key, field| field_params[key] = field end @fields ||= field_params end |
#formatted_params ⇒ Object
212 213 214 |
# File 'lib/params_checker/base_params_checker.rb', line 212 def formatted_params formatted_params_after_custom_all_fields_check end |
#init ⇒ Object
204 205 206 |
# File 'lib/params_checker/base_params_checker.rb', line 204 def init {} end |
#raise_error(message = "Invalid data") ⇒ Object
27 28 29 |
# File 'lib/params_checker/base_params_checker.rb', line 27 def raise_error ="Invalid data" raise MyError.new() end |
#specify_field_check(key) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/params_checker/base_params_checker.rb', line 190 def specify_field_check(key) check_method = "check_#{key}" total_parameters = method(check_method).arity # check_id(id) or check_id(id, opts) value = total_parameters == 1 ? self.send(check_method, @formatted_params_after_default_check[key]) : self.send(check_method, @formatted_params_after_default_check[key], @formatted_params_after_default_check) @formatted_params_after_custom_specify_field_checks.delete(key) @formatted_params_after_custom_specify_field_checks[key] = value end |
#specify_field_checks ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/params_checker/base_params_checker.rb', line 180 def specify_field_checks @formatted_params_after_custom_specify_field_checks = formatted_params_after_default_check fields.each do |key, value| # next unless self.methods.grep(/check_#{key}/).length > 0 next unless "check_#{key}".to_sym.in?(self.methods) specify_field_check(key) end end |
#value_need_to_be_present?(key) ⇒ Boolean
116 117 118 119 120 121 122 123 |
# File 'lib/params_checker/base_params_checker.rb', line 116 def value_need_to_be_present? key if fields[key].key?(:default) && !fields[key][:default].nil? @params[key] = fields[key][:default] true else fields[key][:required] end end |
#value_present?(key) ⇒ Boolean
125 126 127 |
# File 'lib/params_checker/base_params_checker.rb', line 125 def value_present? key params.key?(key) end |
#value_valid?(key) ⇒ Boolean
129 130 131 132 133 134 135 136 137 |
# File 'lib/params_checker/base_params_checker.rb', line 129 def value_valid? key cmd = check_base_on_field_type key if cmd.success? @formatted_params_after_default_check[key] = cmd.result else errors.add(key, cmd.errors[key]) end cmd.success? end |