module Util::Params
Constants
- VERSION
Public Class Methods
new()
click to toggle source
コンストラクタ
Calls superclass method
# File lib/util/params/params.rb, line 5 def initialize super # エラーフラグ @is_error = false # エラーリスト @errors = [] end
Public Instance Methods
get_array_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 63 def get_array_params key, options={} base = {key: key, type: Type::ARRAY, null: true} get_params base.merge(options) end
get_bool_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 58 def get_bool_params key, options={} base = {key: key, type: Type::BOOLEAN, null: true} get_params base.merge(options) end
get_file_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 53 def get_file_params key, options={} base = {key: key, type: Type::FILE, null: true} get_params base.merge(options) end
get_float_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 48 def get_float_params key, options={} base = {key: key, type: Type::FLOAT, null: true} get_params base.merge(options) end
get_int_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 38 def get_int_params key, options={} base = {key: key, type: Type::INTEGER, null: true} get_params base.merge(options) end
get_object_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 68 def get_object_params key, options={} base = {key: key, type: Type::OBJECT, null: true} get_params base.merge(options) end
get_params(options)
click to toggle source
# File lib/util/params/params.rb, line 23 def get_params options options = options.deep_symbolize_keys key = options[:key] val = _load_val params.permit!.to_h, key, options[:default], options[:require] unless options[:null] _push_error "*[#{key.to_s}] == nil" if val.nil? end return nil if val.nil? _validate key, options[:type], val, options end
get_params_error()
click to toggle source
エラーメッセージ入りスト
# File lib/util/params/params.rb, line 79 def get_params_error @errors.join ', ' end
get_str_params(key, options={})
click to toggle source
# File lib/util/params/params.rb, line 43 def get_str_params key, options={} base = {key: key, type: Type::STRING, null: true} get_params base.merge(options) end
has_params_error?()
click to toggle source
エラーがあるか
# File lib/util/params/params.rb, line 75 def has_params_error? @is_error end
Protected Instance Methods
_load_val(vals, key, default, is_require)
click to toggle source
# File lib/util/params/params.rb, line 85 def _load_val vals, key, default, is_require unless vals.try(:has_key?, key) _push_error "#{key.to_s} == nil" if is_require return default end vals[key] end
_push_error(message)
click to toggle source
エラー追加
# File lib/util/params/params.rb, line 274 def _push_error message @is_error |= true @errors.push message end
_validate(key_label, type, val, options)
click to toggle source
# File lib/util/params/params.rb, line 95 def _validate key_label, type, val, options options ||= {} case type when Type::INTEGER _validate_int key_label, val, options[:min], options[:max], options[:enum] when Type::STRING _validate_str key_label, val, options[:min], options[:max], options[:enum], options[:reg] when Type::FLOAT _validate_float key_label, val, options[:min], options[:max] when Type::BOOLEAN _validate_bool key_label, val when Type::FILE _validate_file key_label, val when Type::OBJECT _validate_object key_label, val, options[:elements] when Type::ARRAY vals = _validate_array key_label, val, options[:min], options[:max] return nil if vals.nil? elem_options = options[:element] || {} elem_type = elem_options[:type] || Type::STRING vals.map.with_index do |_, i| elem_val = vals[i] _validate "#{key_label}[#{i}]", elem_type, elem_val, elem_options end else # do nothing end end
_validate_array(key, val, min, max)
click to toggle source
# File lib/util/params/params.rb, line 225 def _validate_array key, val, min, max return nil if val.nil? unless val.kind_of? Array _push_error "#{key.to_s}.type != Array" return nil end v = val if min && (v.length < min) _push_error "#{key.to_s} val [#{v.to_s}.length] < #{min.to_s}" end if max && (v.length > max) _push_error "#{key.to_s} val [#{v.to_s}.length] > #{max.to_s}" end v end
_validate_bool(key, val)
click to toggle source
# File lib/util/params/params.rb, line 201 def _validate_bool key, val return false if val.kind_of? FalseClass return true if val.kind_of? TrueClass return nil if val.blank? return false if val == 'false' return true if val == 'true' return false if val == '0' true end
_validate_file(key, val)
click to toggle source
# File lib/util/params/params.rb, line 211 def _validate_file key, val return nil if val.nil? if val.size.blank? _push_error "#{key.to_s} == nil" return nil end return nil if val.nil? return nil if val.size.blank? val end
_validate_float(key, val, min, max)
click to toggle source
# File lib/util/params/params.rb, line 181 def _validate_float key, val, min, max return nil if val.blank? if /[^\d.-]/ =~ val.to_s _push_error "#{key.to_s} type [#{val.to_s}] != float" end v = val.to_f if min && (v < min) _push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}" end if max && (v > max) _push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}" end v end
_validate_int(key, val, min, max, enum)
click to toggle source
# File lib/util/params/params.rb, line 127 def _validate_int key, val, min, max, enum return nil if val.blank? if /[^\d-]/ =~ val.to_s _push_error "#{key.to_s} type [#{val.to_s}] != integer" end v = val.to_i if enum for e in enum return v if e === v end _push_error "#{key.to_s} == unknown val [#{v.to_s}]" end if min && (v < min) _push_error "#{key.to_s} val [#{v.to_s}] < #{min.to_s}" end if max && (v > max) _push_error "#{key.to_s} val [#{v.to_s}] > #{max.to_s}" end v end
_validate_object(key, val, elements)
click to toggle source
# File lib/util/params/params.rb, line 246 def _validate_object key, val, elements return nil if val.nil? unless val.kind_of? Hash _push_error "#{key.to_s}.type != Hash" return nil end r = {} if elements.nil? return val.to_h end elements.map do |options| options ||= {} elem_key = options[:key] elem_type = options[:type] elem_default = options[:default] elem_require = options[:require] elem_val = _load_val val, elem_key, elem_default, elem_require r[elem_key] = _validate("#{key}[#{elem_key}]", elem_type, elem_val, options) end r end
_validate_str(key, val, min, max, enum, reg)
click to toggle source
# File lib/util/params/params.rb, line 154 def _validate_str key, val, min, max, enum, reg return nil if val.nil? v = val.to_s if enum enum.each do |e| return v if e === v end _push_error "#{key.to_s} == unknown val [#{v.to_s}]" end if min && (v.length < min) _push_error "#{key.to_s}.length < #{min.to_s} ('#{v.to_s}')" end if max && (v.length > max) _push_error "#{key.to_s}.length > #{max.to_s} ('#{v.to_s}')" end if reg && !(/#{reg}/ =~ val) _push_error "#{key.to_s} unmatch /#{reg.to_s}/ =~ [#{v.to_s}]" end v end