class Cardname

Constants

JOINT_RE
OK4KEY_RE

Attributes

key[R]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~

Public Class Methods

banned_re() click to toggle source
# File lib/cardname.rb, line 66
def banned_re
  @banned_re ||= /[#{Regexp.escape((banned_array + [joint])).join}]/
end
cache() click to toggle source
# File lib/cardname.rb, line 33
def cache
  @cache ||= {}
end
cached_name(str) click to toggle source
# File lib/cardname.rb, line 44
def cached_name str
  cache[str]
end
dangerous_methods() click to toggle source
# File lib/cardname.rb, line 84
def dangerous_methods
  bang_methods = String.instance_methods.select { |m| m.to_s.end_with?("!") }
  %i[replace concat clear].concat bang_methods
end
new(obj) click to toggle source
Calls superclass method
# File lib/cardname.rb, line 37
def new obj
  return obj if obj.is_a? self.class

  str = stringify(obj)
  cached_name(str) || super(str)
end
new(str) click to toggle source
Calls superclass method
# File lib/cardname.rb, line 98
def initialize str
  self.class.cache[str] = super str.strip.encode("UTF-8")
end
nothing_banned?() click to toggle source
# File lib/cardname.rb, line 60
def nothing_banned?
  return @nothing_banned unless @nothing_banned.nil?

  @nothing_banned = banned_array.empty?
end
reset_cache(str=nil) click to toggle source
# File lib/cardname.rb, line 48
def reset_cache str=nil
  str ? cache.delete(str) : @cache = {}
end
split_parts(str) click to toggle source
# File lib/cardname.rb, line 89
def split_parts str
  str.split(/\s*#{JOINT_RE}\s*/, -1)
end
stable_key(name) click to toggle source

Sometimes the core rule “the key's key must be itself” (called “stable” below) is violated eg. it fails with singularize as uninflect method for Matthias -> Matthia -> Matthium Usually that means the name is a proper noun and not a plural. You can choose between two solutions:

  1. don't uninflect if the uninflected key is not stable (stabilize = false)

  2. uninflect until the key is stable (stabilize = true)

# File lib/cardname.rb, line 76
def stable_key name
  key_one = name.send(uninflect)
  key_two = key_one.send(uninflect)
  return key_one unless key_one != key_two

  stabilize ? stable_key(key_two) : name
end
stringify(obj) click to toggle source
# File lib/cardname.rb, line 52
def stringify obj
  if obj.is_a?(Array)
    obj.map(&:to_s) * joint
  else
    obj.to_s
  end
end

Public Instance Methods

<<(val) click to toggle source
# File lib/cardname.rb, line 125
def << val
  replace self.class.new(parts << val)
end
==(other) click to toggle source
# File lib/cardname.rb, line 133
def == other
  other_key =
    case
    when other.respond_to?(:key)     then other.key
    when other.respond_to?(:to_name) then other.to_name.key
    else                                  other.to_s
    end
  other_key == key
end
[]=(index, val) click to toggle source
# File lib/cardname.rb, line 119
def []= index, val
  p = parts
  p[index] = val
  replace self.class.new(p)
end
s() click to toggle source
# File lib/cardname.rb, line 102
def s
  @s ||= String.new self
end
Also aliased as: to_s, to_str
to_name() click to toggle source
# File lib/cardname.rb, line 108
def to_name
  self
end
to_s()
Alias for: s
to_str()
Alias for: s

Private Instance Methods

ensure_simpleness(part, msg=nil) click to toggle source
# File lib/cardname/manipulate.rb, line 75
def ensure_simpleness part, msg=nil
  return if part.to_name.simple?

  raise StandardError, "'#{part}' has to be simple. #{msg}"
end
reset() click to toggle source
# File lib/cardname.rb, line 145
def reset
  self.class.reset_cache s
  instance_variables.each do |var|
    instance_variable_set var, nil
  end
end
swap_all_subsequences(oldseq, newseq) click to toggle source
# File lib/cardname/manipulate.rb, line 57
def swap_all_subsequences oldseq, newseq
  res = []
  i = 0
  while i <= num_parts - oldseq.num_parts
    # for performance reasons: check first character first then the rest
    if oldseq.part_keys.first == part_keys[i] &&
       oldseq.part_keys == part_keys[i, oldseq.num_parts]
      res += newseq.parts
      i += oldseq.num_parts
    else
      res << parts[i]
      i += 1
    end
  end
  res += parts[i..-1] if i < num_parts
  res
end