Class: ParamsChecker::BaseParamsChecker

Inherits:
Object
  • Object
show all
Includes:
Fields, SimpleCommand
Defined in:
lib/params_checker/base_params_checker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fields

#arr_field, #bigint_field, #bignum_field, #boolean_field, #char_field, #date_field, #datetime_field, #hash_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.



9
10
11
12
13
14
15
16
17
# File 'lib/params_checker/base_params_checker.rb', line 9

def initialize(params: {}, context: {}, is_outest_hash: true)
  @params = params
  @context = context
  @is_outest_hash = is_outest_hash
  @formatted_params_after_default_fields_check = {}
  @formatted_params_after_custom_fields_check = {}
  @formatted_params_after_custom_overall_check = {}
  @custom_check_errors = {}
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def context
  @context
end

#custom_check_errorsObject

Returns the value of attribute custom_check_errors.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def custom_check_errors
  @custom_check_errors
end

#formatted_params_after_custom_fields_checkObject

Returns the value of attribute formatted_params_after_custom_fields_check.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def formatted_params_after_custom_fields_check
  @formatted_params_after_custom_fields_check
end

#formatted_params_after_custom_overall_checkObject

Returns the value of attribute formatted_params_after_custom_overall_check.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def formatted_params_after_custom_overall_check
  @formatted_params_after_custom_overall_check
end

#formatted_params_after_default_fields_checkObject

Returns the value of attribute formatted_params_after_default_fields_check.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def formatted_params_after_default_fields_check
  @formatted_params_after_default_fields_check
end

#is_outest_hashObject

Returns the value of attribute is_outest_hash.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def is_outest_hash
  @is_outest_hash
end

#paramsObject

Returns the value of attribute params.



256
257
258
# File 'lib/params_checker/base_params_checker.rb', line 256

def params
  @params
end

Class Method Details

.init(required: true, many: false, default: nil, allow_nil: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/params_checker/base_params_checker.rb', line 19

def self.init(required: true, many: false, default: nil, allow_nil: false)
  raise "This field's type must be boolean." if [required, many, allow_nil].any? { |value| !value.in? [true, false] }

  type = many ? 'nested_hashs' : 'nested_hash'

  {
    type: type,
    required: required,
    default: default,
    allow_nil: allow_nil,
    many: many,
    class: self
  }
end

Instance Method Details

#add_error(message, key = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/params_checker/base_params_checker.rb', line 38

def add_error(message, key = nil)
  raise ParamsChecker::FieldError.new({
    message: message,
    key: key,
  })
end

#add_errorsObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/params_checker/base_params_checker.rb', line 230

def add_errors
  # only add errors at the outest hash
  # return unless is_outest_hash

  field_errors = errors.each_with_object({}) do |error, hash|
    key, value = error
    value = value.is_a?(Array) ? value[0] : value
    hash[key] = value

    errors.delete(key)
  end

  @custom_check_errors.each do |key, value|
    field_errors[key] = value
  end

  errors.add(
    :errors,
    {
      message: DEFAULT_MESSAGE_ERROR,
      error_type: 'fields_errors',
      field_errors: field_errors
    }
  )
end

#all_fields_of_params_are_valid?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/params_checker/base_params_checker.rb', line 84

def all_fields_of_params_are_valid?
  @all_fields_of_params_are_valid ||= field_valids.all?(true)
end

#callObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/params_checker/base_params_checker.rb', line 45

def call
  default_fields_check && custom_check
  error_exist? && add_errors
  formatted_params
rescue ParamsChecker::GeneralError => e
  # if is the outest hash, add error
  # if is not, keep raising error, bubble up to the outest hash,
  # then the outest hash will add error
  unless is_outest_hash
    raise e
  end

  errors.add(
    :errors,
    {
      message: e.message,
      error_type: 'general_error'
    }
  )
end

#check(params) ⇒ Object



222
223
224
# File 'lib/params_checker/base_params_checker.rb', line 222

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
175
176
# File 'lib/params_checker/base_params_checker.rb', line 147

def check_base_on_field_type(key)
  case schema[key][:type]
  when 'num'
    ParamChecker::NumParamChecker.call(key, schema, params)
  when 'int'
    ParamChecker::IntParamChecker.call(key, schema, params)
  when 'char'
    ParamChecker::CharParamChecker.call(key, schema, params)
  when 'text'
    ParamChecker::CharParamChecker.call(key, schema, params)
  when 'arr'
    ParamChecker::ArrParamChecker.call(key, schema, params)
  when 'hash'
    ParamChecker::HashParamChecker.call(key, schema, params)
  when 'nested_hash'
    ParamChecker::NestedHashChecker.call(key, schema, params, context)
  when 'nested_hashs'
    ParamChecker::NestedHashsChecker.call(key, schema, params, context)
  when 'date'
    ParamChecker::DateParamChecker.call(key, schema, params)
  when 'time'
    ParamChecker::TimeParamChecker.call(key, schema, params)
  when 'datetime'
    ParamChecker::DateTimeParamChecker.call(key, schema, params)
  when 'boolean'
    ParamChecker::BooleanChecker.call(key, schema, params)
  when 'file'
    ParamChecker::FileChecker.call(key, schema, params)
  end
end

#custom_checkObject



80
81
82
# File 'lib/params_checker/base_params_checker.rb', line 80

def custom_check
  fields_check && overall_check
end

#default_fields_checkObject



70
71
72
73
74
75
76
77
78
# File 'lib/params_checker/base_params_checker.rb', line 70

def default_fields_check
  params_is_not_a_hash = !params.is_a?(ActionController::Parameters) && !params.is_a?(Hash)

  if params_is_not_a_hash
    errors.add(:error, 'ParamsChecker only receive object or ActionController::Parameters as input.')
  end

  all_fields_of_params_are_valid?
end

#error_exist?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/params_checker/base_params_checker.rb', line 66

def error_exist?
  errors.present? && errors.is_a?(Hash) || @custom_check_errors.present?
end

#field_check(key) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/params_checker/base_params_checker.rb', line 191

def field_check(key)
  check_method = "check_#{key}"
  total_parameters = method(check_method).arity
  # pp "========>total_parameters : ", total_parameters

  value = if total_parameters == 1
            # like check_name(name)
            send(check_method, @formatted_params_after_default_fields_check[key])
          elsif total_parameters == 2
            # like check_name(name, opts)
            send(check_method, @formatted_params_after_default_fields_check[key], @formatted_params_after_default_fields_check)
          end

  @formatted_params_after_custom_fields_check[key] = value
rescue ParamsChecker::FieldError => e
  # binding.pry
  key = e.data[:key].presence || key
  @custom_check_errors[key] = e.data[:message]
end

#field_is_valid?(key) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'lib/params_checker/base_params_checker.rb', line 97

def field_is_valid?(key)
  return value_valid?(key) if value_present?(key)

  return true unless value_need_to_be_present?(key)

  errors.add(key, 'This field is required.')
  false
end

#field_validsObject



88
89
90
91
92
93
94
95
# File 'lib/params_checker/base_params_checker.rb', line 88

def field_valids
  @field_valids ||=
    schema.map do |key, _|
      set_default_value(key) if need_to_set_default_value?(key)

      field_is_valid?(key)
    end
end

#fields_checkObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/params_checker/base_params_checker.rb', line 178

def fields_check
  @formatted_params_after_custom_fields_check = formatted_params_after_default_fields_check
  schema.each do |key, _|
    # next unless self.methods.grep(/check_#{key}/).length > 0
    need_to_check = "check_#{key}".to_sym.in?(methods)
    passed_default_check = errors[key].nil?

    next unless need_to_check && passed_default_check

    field_check(key)
  end
end

#formatted_paramsObject



226
227
228
# File 'lib/params_checker/base_params_checker.rb', line 226

def formatted_params
  formatted_params_after_custom_overall_check
end

#initObject



218
219
220
# File 'lib/params_checker/base_params_checker.rb', line 218

def init
  {}
end

#need_to_set_default_value?(key) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
# File 'lib/params_checker/base_params_checker.rb', line 106

def need_to_set_default_value?(key)
  value_is_nil = @params[key].nil?
  schema_field_has_default_value_key = schema[key].key?(:default)
  default_value_is_set = !schema[key][:default].nil?

  value_is_nil &&
    schema_field_has_default_value_key &&
    default_value_is_set
end

#overall_checkObject



211
212
213
214
215
216
# File 'lib/params_checker/base_params_checker.rb', line 211

def overall_check
  @formatted_params_after_custom_overall_check = check(formatted_params_after_custom_fields_check)
rescue ParamsChecker::FieldError => e
  key = e.data[:key]
  @custom_check_errors[key] = e.data[:message]
end

#raise_error(message = DEFAULT_MESSAGE_ERROR) ⇒ Object

Raises:



34
35
36
# File 'lib/params_checker/base_params_checker.rb', line 34

def raise_error(message = DEFAULT_MESSAGE_ERROR)
  raise GeneralError.new(message)
end

#schemaObject



140
141
142
143
144
145
# File 'lib/params_checker/base_params_checker.rb', line 140

def schema
  @schema ||= init.each_with_object({}) do |error, hash|
    key, value = error
    hash[key] = value
  end
end

#set_default_value(key) ⇒ Object



116
117
118
# File 'lib/params_checker/base_params_checker.rb', line 116

def set_default_value(key)
  @params[key] = schema[key][:default]
end

#value_need_to_be_present?(key) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
# File 'lib/params_checker/base_params_checker.rb', line 120

def value_need_to_be_present?(key)
  return true if schema[key].key?(:default) && !schema[key][:default].nil?

  schema[key][:required]
end

#value_present?(key) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/params_checker/base_params_checker.rb', line 126

def value_present?(key)
  params.key?(key)
end

#value_valid?(key) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
# File 'lib/params_checker/base_params_checker.rb', line 130

def value_valid?(key)
  cmd = check_base_on_field_type(key)
  if cmd.success?
    @formatted_params_after_default_fields_check[key] = cmd.result
  else
    errors.add(key, cmd.errors[key])
  end
  cmd.success?
end