Class: ParamsChecker::ParamChecker::NestedHashChecker

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 = {}) ⇒ NestedHashChecker

Returns a new instance of NestedHashChecker.



123
124
125
126
127
128
# File 'lib/params_checker/param_checker.rb', line 123

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.



155
156
157
# File 'lib/params_checker/param_checker.rb', line 155

def class
  @class
end

#contextObject

Returns the value of attribute context.



155
156
157
# File 'lib/params_checker/param_checker.rb', line 155

def context
  @context
end

#fieldsObject

Returns the value of attribute fields.



155
156
157
# File 'lib/params_checker/param_checker.rb', line 155

def fields
  @fields
end

#keyObject

Returns the value of attribute key.



155
156
157
# File 'lib/params_checker/param_checker.rb', line 155

def key
  @key
end

#optsObject

Returns the value of attribute opts.



155
156
157
# File 'lib/params_checker/param_checker.rb', line 155

def opts
  @opts
end

Instance Method Details

#add_error(message = '') ⇒ Object



151
152
153
# File 'lib/params_checker/param_checker.rb', line 151

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

#callObject



130
131
132
133
134
# File 'lib/params_checker/param_checker.rb', line 130

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

    check_type && formatted_nested_hash
end

#check_typeObject



145
146
147
148
149
# File 'lib/params_checker/param_checker.rb', line 145

def check_type
    valid = opts[key].is_a?(ActionController::Parameters) || opts[key].is_a?(Hash)
    add_error("This field's type must be object or ActionController::Parameters.") unless valid
    valid
end

#formatted_nested_hashObject



136
137
138
139
140
141
142
143
# File 'lib/params_checker/param_checker.rb', line 136

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