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
- RUBYENCODING =
RUBY_VERSION !~ /^1\.8/
- OK4KEY_RE =
RUBYENCODING ? '\p{Word}\*' : '\w\*'
- JOINT_RE =
Regexp.escape joint
- @@name2nameobject =
name cache
{}
Constants included from Contextual
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
-
#s ⇒ Object
(also: #to_s)
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
Attributes included from Parts
Class Method Summary collapse
- .banned_re ⇒ Object
- .known_name(str) ⇒ Object
- .new(obj) ⇒ Object
-
.stable_uninflect(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.
- #inspect ⇒ Object
- #length ⇒ Object
- #size ⇒ Object
- #to_name ⇒ Object
Methods included from Manipulate
#replace, #replace_part, #replace_piece
Methods included from Predicates
#blank?, #ends_with?, #include?, #junction?, #starts_with?, #valid?
Methods included from Contextual
#absolute?, #absolute_name, #child_of?, #nth_left, #relative?, #relative_name, #simple_relative?, #starts_with_joint?, #stripped, #to_absolute, #to_absolute_name, #to_show
Methods included from Variants
#decoded, #safe_key, #simple_key, #to_sym, #url_key
Methods included from Parts
#+, #[], #initialize_parts, #left, #left_key, #left_name, #parent_keys, #parent_names, #parents, #part_names, #piece_names, #pieces, #right, #right_key, #right_name, #tag, #tag_name, #trunk, #trunk_name
Constructor Details
#initialize(str) ⇒ Cardname
Returns a new instance of Cardname.
81 82 83 84 85 86 87 |
# File 'lib/cardname.rb', line 81 def initialize str @s = str.to_s.strip @s = @s.encode('UTF-8') if RUBYENCODING initialize_parts @key = @part_keys.join(self.class.joint) @@name2nameobject[str] = self end |
Instance Attribute Details
#key ⇒ Object (readonly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
78 79 80 |
# File 'lib/cardname.rb', line 78 def key @key end |
#s ⇒ Object (readonly) Also known as: to_s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
78 79 80 |
# File 'lib/cardname.rb', line 78 def s @s end |
Class Method Details
.banned_re ⇒ Object
57 58 59 |
# File 'lib/cardname.rb', line 57 def banned_re %r{#{ (['['] + banned_array << joint) * '\\' + ']' }} end |
.known_name(str) ⇒ Object
45 46 47 |
# File 'lib/cardname.rb', line 45 def known_name str @@name2nameobject[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 known_name(str) || super(str.strip) end |
.stable_uninflect(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) (probably the best choice because you want Matthias not to be the same as Matthium)
-
uninflect until the key is stable (stabilize = true)
68 69 70 71 72 73 |
# File 'lib/cardname.rb', line 68 def stable_uninflect name key_one = name.send(Cardname.uninflect) key_two = key_one.send(Cardname.uninflect) return key_one unless key_one != key_two Cardname.stabilize ? stable_uninflect(key_two) : name end |
.stringify(obj) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/cardname.rb', line 49 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???
65 66 67 68 69 70 71 72 |
# File 'lib/cardname/manipulate.rb', line 65 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/cardname.rb', line 105 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 |
#inspect ⇒ Object
101 102 103 |
# File 'lib/cardname.rb', line 101 def inspect "<#{self.class.name} key=#{key}[#{self}]>" end |
#length ⇒ Object
93 94 95 |
# File 'lib/cardname.rb', line 93 def length parts.length end |
#size ⇒ Object
97 98 99 |
# File 'lib/cardname.rb', line 97 def size to_s.size end |
#to_name ⇒ Object
89 90 91 |
# File 'lib/cardname.rb', line 89 def to_name self end |