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
- .dangerous_methods ⇒ Object
- .new(obj) ⇒ Object
- .reset_cache(str = nil) ⇒ Object
- .split_parts(str) ⇒ 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_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.
92 93 94 |
# File 'lib/cardname.rb', line 92 def initialize str @@cache[str] = super str.strip.encode('UTF-8') end |
Instance Attribute Details
#key ⇒ Object (readonly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
90 91 92 |
# File 'lib/cardname.rb', line 90 def key @key end |
Class Method Details
.banned_re ⇒ Object
60 61 62 63 |
# File 'lib/cardname.rb', line 60 def banned_re banned_chars = (banned_array << joint).join /[#{Regexp.escape banned_chars}]/ end |
.cached_name(str) ⇒ Object
44 45 46 |
# File 'lib/cardname.rb', line 44 def cached_name str @@cache[str] end |
.dangerous_methods ⇒ Object
78 79 80 81 |
# File 'lib/cardname.rb', line 78 def dangerous_methods bang_methods = String.instance_methods.select { |m| m.to_s.ends_with?("!") } [:replace].concat bang_methods end |
.new(obj) ⇒ Object
38 39 40 41 42 |
# 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 |
.reset_cache(str = nil) ⇒ Object
48 49 50 |
# File 'lib/cardname.rb', line 48 def reset_cache str=nil str ? @@cache.delete(str) : @@cache = {} end |
.split_parts(str) ⇒ Object
83 84 85 |
# File 'lib/cardname.rb', line 83 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:
-
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
52 53 54 55 56 57 58 |
# 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 |
.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
117 118 119 120 121 122 123 124 125 |
# File 'lib/cardname.rb', line 117 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
96 97 98 |
# File 'lib/cardname.rb', line 96 def s @s ||= String.new self end |
#to_name ⇒ Object
102 103 104 |
# File 'lib/cardname.rb', line 102 def to_name self end |