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 = '', fields = {}, opts = {}, context = {}) ⇒ NestedHashsChecker



160
161
162
163
164
165
# File 'lib/params_checker/param_checker.rb', line 160

def initialize(key = '', fields = {}, opts = {}, context = {})
    @key = key
    @fields = fields
    @opts = opts
    @context = context
end

Instance Attribute Details

#classObject

Returns the value of attribute class.



198
199
200
# File 'lib/params_checker/param_checker.rb', line 198

def class
  @class
end

#contextObject

Returns the value of attribute context.



198
199
200
# File 'lib/params_checker/param_checker.rb', line 198

def context
  @context
end

#fieldsObject

Returns the value of attribute fields.



198
199
200
# File 'lib/params_checker/param_checker.rb', line 198

def fields
  @fields
end

#keyObject

Returns the value of attribute key.



198
199
200
# File 'lib/params_checker/param_checker.rb', line 198

def key
  @key
end

#optsObject

Returns the value of attribute opts.



198
199
200
# File 'lib/params_checker/param_checker.rb', line 198

def opts
  @opts
end

Instance Method Details

#add_error(message = '') ⇒ Object



194
195
196
# File 'lib/params_checker/param_checker.rb', line 194

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

#callObject



167
168
169
170
171
# File 'lib/params_checker/param_checker.rb', line 167

def call
    return nil if fields[key][:allow_nil] && opts[key].nil?

    check_type && formatted_nested_hashs
end

#check_typeObject



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

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

#formatted_nested_hash(nested_hash) ⇒ Object



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

def formatted_nested_hash nested_hash
    cmd = fields[key][:class].call(params: nested_hash, context: context, is_outest_hash: false)
    if cmd.success?
        cmd.result
    else
        add_error cmd.errors
    end
end

#formatted_nested_hashsObject



173
174
175
176
177
# File 'lib/params_checker/param_checker.rb', line 173

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