Class: Cardname
- 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
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
Attributes included from Parts
Class Method Summary collapse
- .banned_re ⇒ Object
- .cached_name(str) ⇒ Object
- .new(obj) ⇒ Object
- .reset_cache(str = nil) ⇒ Object
-
.stable_key(name) ⇒ Object
Sometimes the core rule “the key’s key must be itself” (called “stable” below) is violated eg.
- .stringify(obj) ⇒ Object
-
.substitute!(str, hash) ⇒ Object
HACK.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(str) ⇒ Cardname
constructor
A new instance of Cardname.
- #s ⇒ Object (also: #to_s, #to_str)
- #to_name ⇒ Object
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
#key ⇒ Object (readonly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
81 82 83 |
# File 'lib/cardname.rb', line 81 def key @key end |
Class Method Details
.banned_re ⇒ Object
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:
-
don’t uninflect if the uninflected key is not stable (stabilize = false)
-
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 |
#s ⇒ Object Also known as: to_s, to_str
87 88 89 |
# File 'lib/cardname.rb', line 87 def s @s ||= String.new self end |
#to_name ⇒ Object
93 94 95 |
# File 'lib/cardname.rb', line 93 def to_name self end |