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, #swap, #swap_part, #swap_piece

Methods included from Predicates

#ends_with?, #include?, #junction?, #starts_with?, #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

#[], #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.



83
84
85
# File 'lib/cardname.rb', line 83

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

Instance Attribute Details

#keyObject (readonly)

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



81
82
83
# File 'lib/cardname.rb', line 81

def key
  @key
end

Class Method Details

.banned_reObject



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

def banned_re
  %r{#{ (['['] + banned_array << joint) * '\\' + ']' }}
end

.cached_name(str) ⇒ Object



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

def cached_name str
  @@cache[str]
end

.new(obj) ⇒ Object



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

def new obj
  return obj if obj.is_a? self.class
  str = stringify obj
  cached_name(str) || super(str)
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

.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)



71
72
73
74
75
76
# File 'lib/cardname.rb', line 71

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???



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

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

#==(other) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/cardname.rb', line 101

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

#sObject Also known as: to_s, to_str



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

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

#to_nameObject



93
94
95
# File 'lib/cardname.rb', line 93

def to_name
  self
end