Module: IdValidator::Concern::Func
Instance Method Summary collapse
-
#check_birthday(birthday) ⇒ Object
检测出生日期, example: ‘20181015’.
-
#format_address_info(address_info) ⇒ Object
格式化地址栏,地址三元组(省,市,区).
-
#format_birthday_code(birthday_code) ⇒ Object
格式化出生日期.
-
#get_address_info(address_code) ⇒ Object
获取指定区划代码最新的地址名称.
-
#get_random_address_code(reg) ⇒ Object
随意获取一个符合要求的地址码.
-
#get_random_birthday_code ⇒ Object
随意生成一个生日.
-
#get_random_right_num(num, min, max) ⇒ Object
随机获取一个符合条件的数字.
-
#get_str_pad(str, length = 2, character = '0', right = true) ⇒ Object
字符串填充.
Instance Method Details
#check_birthday(birthday) ⇒ Object
检测出生日期, example: ‘20181015’
7 8 9 10 11 12 13 14 15 |
# File 'lib/id_validator/concern/helper.rb', line 7 def check_birthday(birthday) return false if birthday.to_s.length != 8 date = Date.parse(birthday.to_s) rescue nil return false if date.nil? return false if date.year < 1800 || date > Date.today true end |
#format_address_info(address_info) ⇒ Object
格式化地址栏,地址三元组(省,市,区)
23 24 25 |
# File 'lib/id_validator/concern/helper.rb', line 23 def format_address_info(address_info) [address_info[:province], address_info[:city], address_info[:district]] end |
#format_birthday_code(birthday_code) ⇒ Object
格式化出生日期
18 19 20 |
# File 'lib/id_validator/concern/helper.rb', line 18 def format_birthday_code(birthday_code) "#{birthday_code[0..3]}-#{birthday_code[4..5]}-#{birthday_code[6..7]}" end |
#get_address_info(address_code) ⇒ Object
获取指定区划代码最新的地址名称
56 57 58 59 60 61 62 |
# File 'lib/id_validator/concern/helper.rb', line 56 def get_address_info(address_code) address_code = address_code.to_i address_arr = IdValidator::Config.total_address_info.fetch(address_code, []) address = address_arr.sort_by{|a| a[:end_year]}.last || {} address.fetch(:address, nil) end |
#get_random_address_code(reg) ⇒ Object
随意获取一个符合要求的地址码
28 29 30 31 32 |
# File 'lib/id_validator/concern/helper.rb', line 28 def get_random_address_code(reg) keys = IdValidator::Config.latest_address_info.keys.shuffle keys.find { |key| key.to_s =~ reg }.to_s end |
#get_random_birthday_code ⇒ Object
随意生成一个生日
51 52 53 |
# File 'lib/id_validator/concern/helper.rb', line 51 def get_random_birthday_code Time.at(rand * Time.now.to_i).strftime('%Y%m%d') end |
#get_random_right_num(num, min, max) ⇒ Object
随机获取一个符合条件的数字
44 45 46 47 48 |
# File 'lib/id_validator/concern/helper.rb', line 44 def get_random_right_num(num, min, max) return num if (min..max).member?(num) rand(min..max) end |
#get_str_pad(str, length = 2, character = '0', right = true) ⇒ Object
字符串填充
35 36 37 38 39 40 41 |
# File 'lib/id_validator/concern/helper.rb', line 35 def get_str_pad(str, length = 2, character = '0', right = true) str = str.to_s return str if length <= str.length frequency = length - str.length right ? (str + character.to_s * frequency) : (character.to_s * frequency + str) end |