Class: Card::Name::ViewName

Inherits:
Cardname
  • Object
show all
Defined in:
lib/card/name/view_name.rb

Constant Summary collapse

@@name2viewnameobject =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ViewName

Returns a new instance of ViewName.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/card/name/view_name.rb', line 23

def initialize str
  @s = str.to_s.strip
  @s = @s.encode("UTF-8") if RUBYENCODING
  @key =
    if @s.index(self.class.joint)
      @parts = @s.split(/\s*#{JOINT_RE}\s*/)
      @parts << "" if @s[-1, 1] == self.class.joint
      @simple = false
      @parts.map { |p| p.to_name.key } * self.class.joint
    else
      @parts = [str]
      @simple = true
      str.empty? ? "" : simple_key
    end
  @@name2viewnameobject[str] = self
end

Class Method Details

.new(obj) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/card/name/view_name.rb', line 12

def new obj
  return obj if self.class === obj
  str = Array === obj ? obj * joint : obj.to_s
  if (cached_name = @@name2viewnameobject[str])
    cached_name
  else
    super str.strip
  end
end

Instance Method Details

#simple_keyObject



40
41
42
43
# File 'lib/card/name/view_name.rb', line 40

def simple_key
  decoded.underscore.gsub(/[^#{OK4KEY_RE}]+/, "_")
         .split(/_+/).reject(&:empty?) * "_"
end

#to_viewnameObject



45
46
47
# File 'lib/card/name/view_name.rb', line 45

def to_viewname
  self
end