Class: Cardname

Inherits:
String show all
Includes:
Contextual, Fields, Manipulate, Parts, Pieces, Predicates, Variants
Defined in:
cardname/lib/cardname.rb,
cardname/lib/cardname/parts.rb,
cardname/lib/cardname/fields.rb,
cardname/lib/cardname/pieces.rb,
cardname/lib/cardname/variants.rb,
cardname/lib/cardname/contextual.rb,
cardname/lib/cardname/manipulate.rb,
cardname/lib/cardname/predicates.rb

Overview

The Cardname class generalizes the core naming concepts of Decko/Card. The most central of these is the idea that compound names can be formed by combining simple names.

Direct Known Subclasses

Card::Name

Defined Under Namespace

Modules: Contextual, Fields, Manipulate, Parts, Pieces, Predicates, Variants

Constant Summary collapse

OK4KEY_RE =
'\p{Word}\*'
JOINT_RE =
Regexp.escape joint

Constants included from Contextual

Contextual::RELATIVE_REGEXP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fields

#field, #field_name, #field_of?, #field_only?, #relative_field_name

Methods included from Manipulate

#[], #num_parts, #prepend_joint, #sub_in, #swap, #swap_part, #swap_piece

Methods included from Predicates

#compound?, #ends_with_parts?, #include?, #simple?, #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 Pieces

#ancestors, #piece_names, #pieces

Methods included from Parts

#left, #left_key, #left_name, #parent_keys, #parent_names, #parents, #part_keys, #part_names, #parts, #right, #right_key, #right_name, #tag, #tag_key, #tag_name, #trunk, #trunk_key, #trunk_name

Constructor Details

#initialize(str) ⇒ Cardname

Returns a new instance of Cardname.



84
85
86
87
88
89
90
91
92
# File 'cardname/lib/cardname.rb', line 84

def initialize str
  super str
  strip!
  encode! "UTF-8"
  part_names # populates @part_names and @simple
  decoded # populates @decoded
  key # populates and freezes @key
  freeze
end

Instance Attribute Details

#keyObject (readonly)

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



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

def key
  @key
end

Class Method Details

.banned_reObject



57
58
59
# File 'cardname/lib/cardname.rb', line 57

def banned_re
  @banned_re ||= /[#{Regexp.escape((banned_array + [joint])).join}]/
end

.cacheObject



65
66
67
# File 'cardname/lib/cardname.rb', line 65

def cache
  @cache ||= {}
end

.new(obj) ⇒ Object



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

def new obj
  return obj if obj.is_a? self.class

  str = stringify(obj)
  cache[str] ||= super(str)
end

.nothing_banned?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'cardname/lib/cardname.rb', line 51

def nothing_banned?
  return @nothing_banned unless @nothing_banned.nil?

  @nothing_banned = banned_array.empty?
end

.resetObject



47
48
49
# File 'cardname/lib/cardname.rb', line 47

def reset
  @cache = {}
end

.split_parts(str) ⇒ Object



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

def split_parts str
  str.split(/\s*#{JOINT_RE}\s*/, -1)
end

Instance Method Details

#<<(val) ⇒ Object



111
112
113
# File 'cardname/lib/cardname.rb', line 111

def << val
  replace self.class.new(parts << val)
end

#==(other) ⇒ Object



119
120
121
122
123
124
125
126
# File 'cardname/lib/cardname.rb', line 119

def == 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.to_name.key
    end
end

#[]=(index, val) ⇒ Object



105
106
107
108
109
# File 'cardname/lib/cardname.rb', line 105

def []= index, val
  p = parts
  p[index] = val
  replace self.class.new(p)
end

#sObject Also known as: to_s, to_str



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

def s
  String.new self
end

#to_nameObject

alias_method :dup, :clone



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

def to_name
  self
end