Class: SmartName
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/smart_name.rb
Constant Summary collapse
- RUBYENCODING =
RUBY_VERSION !~ /^1\.8/
- OK4KEY_RE =
RUBYENCODING ? '\p{Word}\*' : '\w\*'
- JOINT_RE =
Regexp.escape joint
- @@name2nameobject =
{}
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
-
#parts ⇒ Object
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
-
#s ⇒ Object
(also: #to_s)
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
-
#simple ⇒ Object
(also: #simple?)
readonly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~.
Class Method Summary collapse
- .banned_re ⇒ 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.
-
.substitute!(str, hash) ⇒ Object
HACK.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #blank? ⇒ Boolean (also: #empty?)
- #decoded ⇒ Object
-
#initialize(str) ⇒ SmartName
constructor
A new instance of SmartName.
- #inspect ⇒ Object
- #junction? ⇒ Boolean
- #left ⇒ Object
- #left_name ⇒ Object
- #length ⇒ Object
- #nth_left(n) ⇒ Object
- #part_names ⇒ Object
- #piece_names ⇒ Object
- #pieces ⇒ Object
-
#replace_part(oldpart, newpart) ⇒ Object
~~~~~~~~~~~~~~~~~~~~ MISC ~~~~~~~~~~~~~~~~~~~~.
- #right ⇒ Object
- #right_name ⇒ Object
- #safe_key ⇒ Object
-
#simple_key ⇒ Object
~~~~~~~~~~~~~~~~~~~ VARIANTS ~~~~~~~~~~~~~~~~~~~.
- #size ⇒ Object
- #tag ⇒ Object
- #tag_name ⇒ Object
- #to_absolute(context, args = {}) ⇒ Object
- #to_absolute_name(*args) ⇒ Object
- #to_name ⇒ Object
-
#to_show(*ignore) ⇒ Object
~~~~~~~~~~~~~~~~~~~~ SHOW / ABSOLUTE ~~~~~~~~~~~~~~~~~~~~.
-
#trunk ⇒ Object
Note that all names have a trunk and tag, but only junctions have left and right.
- #trunk_name ⇒ Object
- #url_key ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(str) ⇒ SmartName
Returns a new instance of SmartName.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/smart_name.rb', line 62 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 @@name2nameobject[str] = self end |
Instance Attribute Details
#key ⇒ Object (readonly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
59 60 61 |
# File 'lib/smart_name.rb', line 59 def key @key end |
#parts ⇒ Object (readonly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
59 60 61 |
# File 'lib/smart_name.rb', line 59 def parts @parts end |
#s ⇒ Object (readonly) Also known as: to_s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
59 60 61 |
# File 'lib/smart_name.rb', line 59 def s @s end |
#simple ⇒ Object (readonly) Also known as: simple?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ INSTANCE ~~~~~~~~~~~~~~~~~~~~~~~~~
59 60 61 |
# File 'lib/smart_name.rb', line 59 def simple @simple end |
Class Method Details
.banned_re ⇒ Object
37 38 39 |
# File 'lib/smart_name.rb', line 37 def banned_re %r{#{ (['['] + banned_array << joint) * '\\' + ']' }} end |
.new(obj) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/smart_name.rb', line 27 def new obj return obj if obj.is_a? self.class str = obj.is_a?(Array) ? obj * joint : obj.to_s if (known_name = @@name2nameobject[str]) known_name else super str.strip end 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)
48 49 50 51 52 53 |
# File 'lib/smart_name.rb', line 48 def stable_uninflect name key_one = name.send(SmartName.uninflect) key_two = key_one.send(SmartName.uninflect) return key_one unless key_one != key_two SmartName.stabilize ? stable_uninflect(key_two) : name end |
.substitute!(str, hash) ⇒ Object
HACK. This doesn’t belong here. shouldn’t it use inclusions???
297 298 299 300 301 302 303 304 |
# File 'lib/smart_name.rb', line 297 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/smart_name.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 |
#blank? ⇒ Boolean Also known as: empty?
90 91 92 |
# File 'lib/smart_name.rb', line 90 def blank? s.blank? end |
#decoded ⇒ Object
138 139 140 |
# File 'lib/smart_name.rb', line 138 def decoded @decoded ||= s.index('&') ? HTMLEntities.new.decode(s) : s end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/smart_name.rb', line 101 def inspect "<#{self.class.name} key=#{key}[#{self}]>" end |
#junction? ⇒ Boolean
145 146 147 |
# File 'lib/smart_name.rb', line 145 def junction? !simple? end |
#left ⇒ Object
149 150 151 |
# File 'lib/smart_name.rb', line 149 def left @left ||= simple? ? nil : parts[0..-2] * self.class.joint end |
#left_name ⇒ Object
157 158 159 |
# File 'lib/smart_name.rb', line 157 def left_name @left_name ||= left && self.class.new(left) end |
#length ⇒ Object
82 83 84 |
# File 'lib/smart_name.rb', line 82 def length parts.length end |
#nth_left(n) ⇒ Object
262 263 264 265 |
# File 'lib/smart_name.rb', line 262 def nth_left n # 1 = left; 2= left of left; 3 = left of left of left.... (n >= length ? parts[0] : parts[0..-n - 1]).to_name end |
#part_names ⇒ Object
184 185 186 |
# File 'lib/smart_name.rb', line 184 def part_names @part_names ||= parts.map(&:to_name) end |
#piece_names ⇒ Object
188 189 190 |
# File 'lib/smart_name.rb', line 188 def piece_names @piece_names ||= pieces.map(&:to_name) end |
#pieces ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/smart_name.rb', line 192 def pieces @pieces ||= if simple? [self] else junction_pieces = [] parts[1..-1].inject parts[0] do |left, right| piece = [left, right] * self.class.joint junction_pieces << piece piece end parts + junction_pieces end end |
#replace_part(oldpart, newpart) ⇒ Object
~~~~~~~~~~~~~~~~~~~~ MISC ~~~~~~~~~~~~~~~~~~~~
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/smart_name.rb', line 269 def replace_part oldpart, newpart oldpart = oldpart.to_name newpart = newpart.to_name if oldpart.simple? if simple? self == oldpart ? newpart : self else parts.map do |p| oldpart == p ? newpart.to_s : p end.to_name end elsif simple? self else if oldpart == parts[0, oldpart.length] if length == oldpart.length newpart else (newpart.parts + parts[oldpart.length..-1]).to_name end else self end end end |
#right ⇒ Object
153 154 155 |
# File 'lib/smart_name.rb', line 153 def right @right ||= simple? ? nil : parts[-1] end |
#right_name ⇒ Object
161 162 163 |
# File 'lib/smart_name.rb', line 161 def right_name @right_name ||= right && self.class.new(right) end |
#safe_key ⇒ Object
134 135 136 |
# File 'lib/smart_name.rb', line 134 def safe_key @safe_key ||= key.tr('*', 'X').tr self.class.joint, '-' end |
#simple_key ⇒ Object
~~~~~~~~~~~~~~~~~~~ VARIANTS ~~~~~~~~~~~~~~~~~~~
117 118 119 120 121 122 123 124 125 |
# File 'lib/smart_name.rb', line 117 def simple_key decoded .underscore .gsub(/[^#{OK4KEY_RE}]+/, '_') .split(/_+/) .reject(&:empty?) .map { |key| SmartName.stable_uninflect(key) } .join('_') end |
#size ⇒ Object
86 87 88 |
# File 'lib/smart_name.rb', line 86 def size to_s.size end |
#tag ⇒ Object
172 173 174 |
# File 'lib/smart_name.rb', line 172 def tag @tag ||= simple? ? s : right end |
#tag_name ⇒ Object
180 181 182 |
# File 'lib/smart_name.rb', line 180 def tag_name @tag_name ||= simple? ? self : right_name end |
#to_absolute(context, args = {}) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/smart_name.rb', line 226 def to_absolute context, args={} context = context.to_name parts.map do |part| new_part = case part when /^_user$/i name_proc = self.class.session name_proc ? name_proc.call : part when /^_main$/i then self.class.params[:main_name] when /^(_self|_whole|_)$/i then context.s when /^_left$/i then context.trunk # note - inconsistent use of left v. trunk when /^_right$/i then context.tag when /^_(\d+)$/i pos = $~[1].to_i pos = context.length if pos > context.length context.parts[pos - 1] when /^_(L*)(R?)$/i l_s, r_s = $~[1].size, !$~[2].empty? l_part = context.nth_left l_s r_s ? l_part.tag : l_part.s # when /^_/ # custom = args[:params] ? args[:params][part] : nil # custom ? CGI.escapeHTML(custom) : part # why are we escaping HTML here? else part end.to_s.strip new_part.empty? ? context.to_s : new_part end * self.class.joint end |
#to_absolute_name(*args) ⇒ Object
258 259 260 |
# File 'lib/smart_name.rb', line 258 def to_absolute_name *args self.class.new to_absolute(*args) end |
#to_name ⇒ Object
78 79 80 |
# File 'lib/smart_name.rb', line 78 def to_name self end |
#to_show(*ignore) ⇒ Object
~~~~~~~~~~~~~~~~~~~~ SHOW / ABSOLUTE ~~~~~~~~~~~~~~~~~~~~
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/smart_name.rb', line 209 def to_show *ignore ignore.map!(&:to_name) show_parts = parts.map do |part| reject = (part.empty? || (part =~ /^_/) || ignore.member?(part.to_name)) reject ? nil : part end show_name = show_parts.compact.to_name.s case when show_parts.compact.empty? then self when show_parts[0].nil? then self.class.joint + show_name else show_name end end |
#trunk ⇒ Object
Note that all names have a trunk and tag, but only junctions have left and right
168 169 170 |
# File 'lib/smart_name.rb', line 168 def trunk @trunk ||= simple? ? s : left end |
#trunk_name ⇒ Object
176 177 178 |
# File 'lib/smart_name.rb', line 176 def trunk_name @trunk_name ||= simple? ? self : left_name end |
#url_key ⇒ Object
127 128 129 130 131 132 |
# File 'lib/smart_name.rb', line 127 def url_key @url_key ||= part_names.map do |part_name| stripped = part_name.decoded.gsub(/[^#{OK4KEY_RE}]+/, ' ').strip stripped.gsub(/[\s\_]+/, '_') end * self.class.joint end |
#valid? ⇒ Boolean
95 96 97 98 99 |
# File 'lib/smart_name.rb', line 95 def valid? !parts.find do |pt| pt.match self.class.banned_re end end |