smartname 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/smart_name.rb CHANGED
@@ -2,15 +2,17 @@
2
2
 
3
3
  require 'active_support/configurable'
4
4
  require 'active_support/inflector'
5
+ require 'htmlentities'
5
6
 
6
7
  class Object
7
- def to_name() SmartName.new(self) end
8
+ def to_name
9
+ SmartName.new self
10
+ end
8
11
  end
9
12
 
10
13
  class SmartName < Object
11
- require 'htmlentities'
12
- RUBY19 = RUBY_VERSION =~ /^1\.9/
13
- WORD_RE = RUBY19 ? '\p{Word}' : '\w'
14
+ RUBY19 = RUBY_VERSION =~ /^1\.9/
15
+ OK4KEY_RE = RUBY19 ? '\p{Word}\*' : '\w\*'
14
16
 
15
17
  include ActiveSupport::Configurable
16
18
 
@@ -18,14 +20,16 @@ class SmartName < Object
18
20
  :var_re, :uninflect, :params, :codes, :lookup
19
21
 
20
22
  # Wagny defaults:
21
- JOINT = '+'
22
- SmartName.joint = JOINT
23
- SmartName.formal_joint = " <span class=\"wiki-joint\">#{JOINT}</span> "
23
+ SmartName.joint = '+'
24
+ SmartName.formal_joint = " <span class=\"wiki-joint\">#{SmartName.joint}</span> " #let's get rid of this
24
25
  SmartName.banned_array = [ '/', '~', '|' ]
25
26
  SmartName.name_attribute = :cardname
26
27
  SmartName.var_re = /\{([^\}]*})\}/
27
28
  SmartName.uninflect = :singularize
28
29
 
30
+ JOINT_RE = Regexp.escape SmartName.joint
31
+
32
+
29
33
  @@name2nameobject = {}
30
34
 
31
35
  class << self
@@ -62,8 +66,8 @@ class SmartName < Object
62
66
  @s = str.to_s.strip
63
67
  @s = @s.encode('UTF-8') if RUBY19
64
68
  @key = if @s.index(SmartName.joint)
65
- @parts = @s.split(/\s*#{Regexp.escape(SmartName.joint)}\s*/)
66
- @parts << '' if @s[-1] == SmartName.joint
69
+ @parts = @s.split(/\s*#{JOINT_RE}\s*/)
70
+ @parts << '' if @s[-1,1] == SmartName.joint
67
71
  @simple = false
68
72
  @parts.map { |p| p.to_name.key } * SmartName.joint
69
73
  else
@@ -74,24 +78,27 @@ class SmartName < Object
74
78
  @@name2nameobject[str] = self
75
79
  end
76
80
 
77
- def valid?()
78
- not parts.find { |pt| pt.match SmartName.banned_re }
79
- end
80
81
  def to_name() self end
81
82
  def length() parts.length end
82
83
  def size() to_s.size end
83
84
  def blank?() s.blank? end
84
85
  alias empty? blank?
85
86
 
87
+ def valid?
88
+ not parts.find do |pt|
89
+ pt.match SmartName.banned_re
90
+ end
91
+ end
92
+
86
93
  def inspect
87
94
  "<SmartName key=#{key}[#{self}]>"
88
95
  end
89
96
 
90
97
  def == obj
91
98
  object_key = case
92
- when obj.respond_to?(:key) ; obj.key
99
+ when obj.respond_to?(:key) ; obj.key
93
100
  when obj.respond_to?(:to_name) ; obj.to_name.key
94
- else ; obj.to_s
101
+ else ; obj.to_s
95
102
  end
96
103
  object_key == key
97
104
  end
@@ -100,11 +107,11 @@ class SmartName < Object
100
107
  #~~~~~~~~~~~~~~~~~~~ VARIANTS ~~~~~~~~~~~~~~~~~~~
101
108
 
102
109
  def simple_key
103
- decoded.underscore.gsub(/[^#{WORD_RE}\*]+/,'_').split(/_+/).reject(&:empty?).map(&(SmartName.uninflect))*'_'
110
+ decoded.underscore.gsub(/[^#{OK4KEY_RE}]+/,'_').split(/_+/).reject(&:empty?).map(&(SmartName.uninflect))*'_'
104
111
  end
105
112
 
106
113
  def url_key
107
- @url_key ||= decoded.gsub(/[^\*#{WORD_RE}\s\+]/,' ').strip.gsub(/[\s\_]+/,'_')
114
+ @url_key ||= decoded.gsub(/[^#{OK4KEY_RE}#{JOINT_RE}]+/,' ').strip.gsub /[\s\_]+/, '_'
108
115
  end
109
116
 
110
117
  def safe_key
@@ -115,49 +122,49 @@ class SmartName < Object
115
122
  @decoded ||= (s.index('&') ? HTMLEntities.new.decode(s) : s)
116
123
  end
117
124
 
118
- def pre_cgi
119
- #why is this necessary?? doesn't real CGI escaping handle this??
120
- # hmmm. is this to prevent absolutizing
121
- @pre_cgi ||= parts.join '~plus~'
122
- end
125
+ def pre_cgi
126
+ #why is this necessary?? doesn't real CGI escaping handle this??
127
+ # hmmm. is this to prevent absolutizing
128
+ @pre_cgi ||= parts.join '~plus~'
129
+ end
123
130
 
124
- def post_cgi
125
- #hmm. this could resolve to the key of some other card. move to class method?
126
- @post_cgi ||= s.gsub '~plus~', SmartName.joint
127
- end
131
+ def post_cgi
132
+ #hmm. this could resolve to the key of some other card. move to class method?
133
+ @post_cgi ||= s.gsub '~plus~', SmartName.joint
134
+ end
128
135
 
129
- #~~~~~~~~~~~~~~~~~~~ PARTS ~~~~~~~~~~~~~~~~~~~
136
+ #~~~~~~~~~~~~~~~~~~~ PARTS ~~~~~~~~~~~~~~~~~~~
130
137
 
131
- alias simple? simple
132
- def junction?() not simple? end
138
+ alias simple? simple
139
+ def junction?() not simple? end
133
140
 
134
- def left() @left ||= simple? ? nil : parts[0..-2]*SmartName.joint end
135
- def right() @right ||= simple? ? nil : parts[-1] end
141
+ def left() @left ||= simple? ? nil : parts[0..-2]*SmartName.joint end
142
+ def right() @right ||= simple? ? nil : parts[-1] end
136
143
 
137
- def left_name() @left_name ||= left && SmartName.new( left ) end
138
- def right_name() @right_name ||= right && SmartName.new( right ) end
144
+ def left_name() @left_name ||= left && SmartName.new( left ) end
145
+ def right_name() @right_name ||= right && SmartName.new( right ) end
139
146
 
140
- # Note that all names have a trunk and tag, but only junctions have left and right
147
+ # Note that all names have a trunk and tag, but only junctions have left and right
141
148
 
142
- def trunk() @trunk ||= simple? ? s : left end
143
- def tag() @tag ||= simple? ? s : right end
149
+ def trunk() @trunk ||= simple? ? s : left end
150
+ def tag() @tag ||= simple? ? s : right end
144
151
 
145
- def trunk_name() @trunk_name ||= simple? ? self : left_name end
146
- def tag_name() @tag_name ||= simple? ? self : right_name end
152
+ def trunk_name() @trunk_name ||= simple? ? self : left_name end
153
+ def tag_name() @tag_name ||= simple? ? self : right_name end
147
154
 
148
- def pieces
149
- @pieces ||= if simple?
150
- [ self ]
151
- else
152
- trunk_name.pieces + [ tag_name ]
153
- end
155
+ def pieces
156
+ @pieces ||= if simple?
157
+ [ self ]
158
+ else
159
+ trunk_name.pieces + [ tag_name ]
154
160
  end
161
+ end
155
162
 
156
163
 
157
164
  #~~~~~~~~~~~~~~~~~~~ TRAITS / STARS ~~~~~~~~~~~~~~~~~~~
158
165
 
159
- def star?() simple? and '*' == s[0] end
160
- def rstar?() right and '*' == right[0] end
166
+ def star?() simple? and '*' == s[0,1] end
167
+ def rstar?() right and '*' == right[0,1] end
161
168
 
162
169
  def trait_name? *traitlist
163
170
  junction? && begin
@@ -179,11 +186,9 @@ class SmartName < Object
179
186
  end
180
187
 
181
188
 
182
-
183
189
  #~~~~~~~~~~~~~~~~~~~~ SHOW / ABSOLUTE ~~~~~~~~~~~~~~~~~~~~
184
190
 
185
191
  def to_show context, args={}
186
- # ignore = [ args[:ignore], context.to_name.parts ].flatten.compact.map &:to_name
187
192
  ignore = [ args[:ignore] ].flatten.map &:to_name
188
193
  fullname = parts.to_name.to_absolute_name context, args
189
194
 
@@ -192,10 +197,13 @@ class SmartName < Object
192
197
  reject ? nil : part
193
198
  end
194
199
 
195
- initial_blank = show_parts[0].nil?
196
200
  show_name = show_parts.compact.to_name.s
197
-
198
- initial_blank ? SmartName.joint + show_name : show_name
201
+
202
+ case
203
+ when show_parts.compact.empty?; fullname
204
+ when show_parts[0].nil? ; SmartName.joint + show_name
205
+ else show_name
206
+ end
199
207
  end
200
208
 
201
209
 
@@ -264,9 +272,13 @@ class SmartName < Object
264
272
  end
265
273
  end
266
274
 
275
+ # HACK. This doesn't belong here.
276
+ # shouldn't it use inclusions???
267
277
  def self.substitute! str, hash
268
278
  hash.keys.each do |var|
269
- str.gsub!(SmartName.var_re) { |x| (v=hash[var.to_sym]).nil? ? x : v }
279
+ str.gsub! SmartName.var_re do |x|
280
+ (v=hash[var.to_sym]).nil? ? x : v
281
+ end
270
282
  end
271
283
  str
272
284
  end
@@ -243,7 +243,8 @@ describe SmartName do
243
243
  'you+awe'.to_name.to_show('A', :ignore=>'you' ).should == '+awe'
244
244
  'me+you+awe'.to_name.to_show('A', :ignore=>'you' ).should == 'me+awe' #HMMM..... what should this do?
245
245
  'me+you+awe'.to_name.to_show('A', :ignore=>['me','you'] ).should == '+awe'
246
- '_left+_right+awe'.to_name.to_show('A+B', :ignore=>'A' ).should == '+B+awe'
246
+ 'me+you'.to_name.to_show('A', :ignore=>['me','you'] ).should == 'me+you'
247
+ '_left+_right+awe'.to_name.to_show('A+B', :ignore=>'A' ).should == '+B+awe'
247
248
  '?a?+awe'.to_name.to_show('B', :ignore=>'A' ).should == '+awe'
248
249
  end
249
250
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: -1300013571457468358
146
+ hash: 2045740443953383621
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: