Class: Cardname

Inherits:
String show all
Includes:
Contextual, Manipulate, Parts, Pieces, Predicates, Variants
Defined in:
cardname/lib/cardname.rb,
cardname/lib/cardname/parts.rb,
cardname/lib/cardname/pieces.rb,
cardname/lib/cardname/variants.rb,
cardname/lib/cardname/contextual.rb,
cardname/lib/cardname/manipulate.rb,
cardname/lib/cardname/predicates.rb

Direct Known Subclasses

Card::Name

Defined Under Namespace

Modules: Contextual, Manipulate, Parts, Pieces, Predicates, Variants

Constant Summary collapse

OK4KEY_RE =
'\p{Word}\*'
JOINT_RE =
Regexp.escape joint

Constants included from Contextual

Contextual::RELATIVE_REGEXP

Instance Attribute Summary collapse

Attributes included from Parts

#part_keys, #parts, #simple

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Manipulate

#num_parts, #prepend_joint, #sub_in, #swap, #swap_part, #swap_piece

Methods included from Predicates

#compound?, #ends_with_parts?, #include?, #simple?, #starts_with_parts?, #valid?

Methods included from Contextual

#absolute, #absolute?, #absolute_name, #child_of?, #from, #name_from, #nth_left, #parts_excluding, #parts_minus, #relative?, #remove_context, #simple_relative?, #starts_with_joint?, #stripped

Methods included from Variants

#decoded, #safe_key, #simple_key, #to_sym, #url_key

Methods included from Pieces

#pieces

Methods included from Parts

#[], #ancestors, #left, #left_key, #left_name, #parent_keys, #parent_names, #parents, #part_names, #piece_names, #right, #right_key, #right_name, #tag, #tag_name, #to_a, #trunk, #trunk_name

Constructor Details

#initialize(str) ⇒ Cardname

Returns a new instance of Cardname.



99
100
101
# File 'cardname/lib/cardname.rb', line 99

def initialize str
  self.class.cache[str] = super str.strip.encode("UTF-8")
end

Instance Attribute Details

#keyObject (readonly)

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



97
98
99
# File 'cardname/lib/cardname.rb', line 97

def key
  @key
end

Class Method Details

.banned_reObject



67
68
69
# File 'cardname/lib/cardname.rb', line 67

def banned_re
  @banned_re ||= /[#{Regexp.escape((banned_array + [joint])).join}]/
end

.cacheObject



34
35
36
# File 'cardname/lib/cardname.rb', line 34

def cache
  @cache ||= {}
end

.cached_name(str) ⇒ Object



45
46
47
# File 'cardname/lib/cardname.rb', line 45

def cached_name str
  cache[str]
end

.dangerous_methodsObject



85
86
87
88
# File 'cardname/lib/cardname.rb', line 85

def dangerous_methods
  bang_methods = String.instance_methods.select { |m| m.to_s.ends_with?("!") }
  %i[replace concat clear].concat bang_methods
end

.new(obj) ⇒ Object



38
39
40
41
42
43
# File 'cardname/lib/cardname.rb', line 38

def new obj
  return obj if obj.is_a? self.class

  str = stringify(obj)
  cached_name(str) || super(str)
end

.nothing_banned?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'cardname/lib/cardname.rb', line 61

def nothing_banned?
  return @nothing_banned unless @nothing_banned.nil?

  @nothing_banned = banned_array.empty?
end

.reset_cache(str = nil) ⇒ Object



49
50
51
# File 'cardname/lib/cardname.rb', line 49

def reset_cache str=nil
  str ? cache.delete(str) : @cache = {}
end

.split_parts(str) ⇒ Object



90
91
92
# File 'cardname/lib/cardname.rb', line 90

def split_parts str
  str.split(/\s*#{JOINT_RE}\s*/, -1)
end

.stable_key(name) ⇒ Object

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)



77
78
79
80
81
82
83
# File 'cardname/lib/cardname.rb', line 77

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) ⇒ Object



53
54
55
56
57
58
59
# File 'cardname/lib/cardname.rb', line 53

def stringify obj
  if obj.is_a?(Array)
    obj.map(&:to_s) * joint
  else
    obj.to_s
  end
end

Instance Method Details

#<<(val) ⇒ Object



126
127
128
# File 'cardname/lib/cardname.rb', line 126

def << val
  replace self.class.new(parts << val)
end

#==(other) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'cardname/lib/cardname.rb', line 134

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) ⇒ Object



120
121
122
123
124
# File 'cardname/lib/cardname.rb', line 120

def []= index, val
  p = parts
  p[index] = val
  replace self.class.new(p)
end

#sObject Also known as: to_s, to_str



103
104
105
# File 'cardname/lib/cardname.rb', line 103

def s
  @s ||= String.new self
end

#to_nameObject



109
110
111
# File 'cardname/lib/cardname.rb', line 109

def to_name
  self
end