Class: Cardname

Inherits:
Object 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

RUBYENCODING =
RUBY_VERSION !~ /^1\.8/
OK4KEY_RE =
RUBYENCODING ? '\p{Word}\*' : '\w\*'
JOINT_RE =
Regexp.escape joint
@@name2nameobject =

name 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

#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

#keyObject (readonly)

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



78
79
80
# File 'lib/cardname.rb', line 78

def key
  @key
end

#sObject (readonly) Also known as: to_s

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



78
79
80
# File 'lib/cardname.rb', line 78

def s
  @s
end

Class Method Details

.banned_reObject



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:

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

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

#inspectObject



101
102
103
# File 'lib/cardname.rb', line 101

def inspect
  "<#{self.class.name} key=#{key}[#{self}]>"
end

#lengthObject



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

def length
  parts.length
end

#sizeObject



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

def size
  to_s.size
end

#to_nameObject



89
90
91
# File 'lib/cardname.rb', line 89

def to_name
  self
end