Class: Cardname

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

Defined Under Namespace

Modules: Contextual, Manipulate, Parts, Predicates, Variants

Constant Summary collapse

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

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

#ends_with_parts?, #include?, #junction?, #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 Parts

#[], #ancestors, #left, #left_key, #left_name, #parent_keys, #parent_names, #parents, #part_names, #piece_names, #pieces, #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 'lib/cardname.rb', line 99

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

Instance Attribute Details

#keyObject (readonly)

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



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

def key
  @key
end

Class Method Details

.banned_reObject



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

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

.cached_name(str) ⇒ Object



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

def cached_name str
  @@cache[str]
end

.dangerous_methodsObject



85
86
87
88
# File '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 '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 '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 'lib/cardname.rb', line 49

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

.split_parts(str) ⇒ Object



90
91
92
# File '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 '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 'lib/cardname.rb', line 53

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

.substitute!(str, hash) ⇒ Object

HACK. This doesn’t belong here. shouldn’t it use inclusions???



83
84
85
86
87
88
89
90
# File 'lib/cardname/manipulate.rb', line 83

def self.substitute! str, hash
  hash.keys.each do |var|
    str.gsub! var_re do |x|
      hash[var.to_sym]
    end
  end
  str
end

Instance Method Details

#<<(val) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

dangerous, too



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

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 'lib/cardname.rb', line 103

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

#to_nameObject



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

def to_name
  self
end