module NickFallbacks
Constants
- DEFAULT_SUFFIX
Public Instance Methods
Source
# File lib/nick_fallbacks.rb, line 32 def expand(preferred:, bases:, static:, nicklen:, suffix: DEFAULT_SUFFIX) candidates = [preferred, *Array(bases).flat_map { |base| expand_base(base, nicklen: nicklen, suffix: suffix) }, *static] candidates.map { |nick| nick.to_s.strip } .reject(&:empty?) .select { |nick| nicklen.nil? || nick.length <= nicklen } .uniq end
Source
# File lib/nick_fallbacks.rb, line 16 def expand_base(base, nicklen:, suffix: DEFAULT_SUFFIX) base = base.to_s.strip suffix = suffix.to_s return [] if base.empty? || suffix.empty? return [] if nicklen && base.length > nicklen limit = nicklen || base.length variants = [base] current = base while current.length + suffix.length <= limit current += suffix variants << current end variants end
Source
# File lib/nick_fallbacks.rb, line 8 def numeric_nicklen(value, fallback: nil) return fallback if value.nil? || value == Float::INFINITY Integer(value) rescue ArgumentError, TypeError fallback end