smartname 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/core_ext.rb +5 -0
- data/lib/smart_name.rb +27 -62
- data/spec/lib/smart_name_spec.rb +2 -35
- data/spec/spec_helper.rb +0 -7
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/core_ext.rb
ADDED
data/lib/smart_name.rb
CHANGED
@@ -4,37 +4,30 @@ require 'active_support/configurable'
|
|
4
4
|
require 'active_support/inflector'
|
5
5
|
require 'htmlentities'
|
6
6
|
|
7
|
-
class Object
|
8
|
-
def to_name
|
9
|
-
SmartName.new self
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
7
|
class SmartName < Object
|
14
|
-
RUBYENCODING = RUBY_VERSION
|
8
|
+
RUBYENCODING = RUBY_VERSION !=~ /^1\.8/
|
15
9
|
OK4KEY_RE = RUBYENCODING ? '\p{Word}\*' : '\w\*'
|
16
10
|
|
17
11
|
include ActiveSupport::Configurable
|
18
|
-
|
19
|
-
config_accessor :joint, :name_attribute, :banned_array,
|
20
|
-
|
21
|
-
|
12
|
+
|
13
|
+
config_accessor :joint, :name_attribute, :banned_array, :var_re, :uninflect, :params, :session
|
14
|
+
|
22
15
|
# Wagny defaults:
|
16
|
+
#config_accessor :joint, :default => '+'
|
23
17
|
SmartName.joint = '+'
|
24
18
|
SmartName.banned_array = [ '/', '~', '|' ]
|
25
19
|
SmartName.name_attribute = :cardname
|
26
20
|
SmartName.var_re = /\{([^\}]*\})\}/
|
27
21
|
SmartName.uninflect = :singularize
|
28
22
|
|
29
|
-
JOINT_RE
|
30
|
-
|
23
|
+
JOINT_RE = Regexp.escape joint
|
31
24
|
|
32
25
|
@@name2nameobject = {}
|
33
26
|
|
34
27
|
class << self
|
35
28
|
def new obj
|
36
|
-
return obj if
|
37
|
-
str = Array===obj ? obj*
|
29
|
+
return obj if self.class===obj
|
30
|
+
str = Array===obj ? obj*joint : obj.to_s
|
38
31
|
if known_name = @@name2nameobject[str]
|
39
32
|
known_name
|
40
33
|
else
|
@@ -43,7 +36,7 @@ class SmartName < Object
|
|
43
36
|
end
|
44
37
|
|
45
38
|
def banned_re
|
46
|
-
%r{#{ (['['] +
|
39
|
+
%r{#{ (['['] + banned_array << joint )*'\\' + ']' }}
|
47
40
|
end
|
48
41
|
end
|
49
42
|
|
@@ -57,11 +50,11 @@ class SmartName < Object
|
|
57
50
|
def initialize str
|
58
51
|
@s = str.to_s.strip
|
59
52
|
@s = @s.encode('UTF-8') if RUBYENCODING
|
60
|
-
@key = if @s.index(
|
53
|
+
@key = if @s.index(self.class.joint)
|
61
54
|
@parts = @s.split(/\s*#{JOINT_RE}\s*/)
|
62
|
-
@parts << '' if @s[-1,1] ==
|
55
|
+
@parts << '' if @s[-1,1] == self.class.joint
|
63
56
|
@simple = false
|
64
|
-
@parts.map { |p| p.to_name.key } *
|
57
|
+
@parts.map { |p| p.to_name.key } * self.class.joint
|
65
58
|
else
|
66
59
|
@parts = [str]
|
67
60
|
@simple = true
|
@@ -78,12 +71,12 @@ class SmartName < Object
|
|
78
71
|
|
79
72
|
def valid?
|
80
73
|
not parts.find do |pt|
|
81
|
-
pt.match
|
74
|
+
pt.match self.class.banned_re
|
82
75
|
end
|
83
76
|
end
|
84
77
|
|
85
78
|
def inspect
|
86
|
-
"
|
79
|
+
"<#{self.class.name} key=#{key}[#{self}]>"
|
87
80
|
end
|
88
81
|
|
89
82
|
def == obj
|
@@ -99,7 +92,7 @@ class SmartName < Object
|
|
99
92
|
#~~~~~~~~~~~~~~~~~~~ VARIANTS ~~~~~~~~~~~~~~~~~~~
|
100
93
|
|
101
94
|
def simple_key
|
102
|
-
decoded.underscore.gsub(/[^#{OK4KEY_RE}]+/,'_').split(/_+/).reject(&:empty?).map(&(
|
95
|
+
decoded.underscore.gsub(/[^#{OK4KEY_RE}]+/,'_').split(/_+/).reject(&:empty?).map(&(self.class.uninflect))*'_'
|
103
96
|
end
|
104
97
|
|
105
98
|
def url_key
|
@@ -107,7 +100,7 @@ class SmartName < Object
|
|
107
100
|
end
|
108
101
|
|
109
102
|
def safe_key
|
110
|
-
@safe_key ||= key.gsub('*','X').gsub
|
103
|
+
@safe_key ||= key.gsub('*','X').gsub self.class.joint, '-'
|
111
104
|
end
|
112
105
|
|
113
106
|
def decoded
|
@@ -122,7 +115,7 @@ class SmartName < Object
|
|
122
115
|
|
123
116
|
def post_cgi
|
124
117
|
#hmm. this could resolve to the key of some other card. move to class method?
|
125
|
-
@post_cgi ||= s.gsub '~plus~',
|
118
|
+
@post_cgi ||= s.gsub '~plus~', self.class.joint
|
126
119
|
end
|
127
120
|
|
128
121
|
#~~~~~~~~~~~~~~~~~~~ PARTS ~~~~~~~~~~~~~~~~~~~
|
@@ -130,11 +123,11 @@ class SmartName < Object
|
|
130
123
|
alias simple? simple
|
131
124
|
def junction?() not simple? end
|
132
125
|
|
133
|
-
def left() @left ||= simple? ? nil : parts[0..-2]*
|
126
|
+
def left() @left ||= simple? ? nil : parts[0..-2]*self.class.joint end
|
134
127
|
def right() @right ||= simple? ? nil : parts[-1] end
|
135
128
|
|
136
|
-
def left_name() @left_name ||= left &&
|
137
|
-
def right_name() @right_name ||= right &&
|
129
|
+
def left_name() @left_name ||= left && self.class.new( left ) end
|
130
|
+
def right_name() @right_name ||= right && self.class.new( right ) end
|
138
131
|
|
139
132
|
# Note that all n ames have a trunk and tag, but only junctions have left and right
|
140
133
|
|
@@ -153,7 +146,7 @@ class SmartName < Object
|
|
153
146
|
else
|
154
147
|
junction_pieces = []
|
155
148
|
parts[1..-1].inject parts[0] do |left, right|
|
156
|
-
piece = [left, right] *
|
149
|
+
piece = [left, right] * self.class.joint
|
157
150
|
junction_pieces << piece
|
158
151
|
piece
|
159
152
|
end
|
@@ -163,34 +156,6 @@ class SmartName < Object
|
|
163
156
|
|
164
157
|
|
165
158
|
|
166
|
-
|
167
|
-
#~~~~~~~~~~~~~~~~~~~ TRAITS / STARS ~~~~~~~~~~~~~~~~~~~
|
168
|
-
|
169
|
-
#all the below seems pretty wagn-specific
|
170
|
-
|
171
|
-
def star?() simple? and '*' == s[0,1] end
|
172
|
-
def rstar?() right and '*' == right[0,1] end
|
173
|
-
|
174
|
-
def trait_name? *traitlist
|
175
|
-
junction? && begin
|
176
|
-
right_key = right_name.key
|
177
|
-
!!traitlist.find do |codename|
|
178
|
-
card_id = SmartName.codes[ codename ] and card = SmartName.lookup[ card_id ] and
|
179
|
-
card.send(SmartName.name_attribute).key == right_key
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def trait_name tag_code
|
185
|
-
card_id = SmartName.codes[ tag_code ] and card = SmartName.lookup[ card_id ] and
|
186
|
-
[ self, card.send(SmartName.name_attribute) ].to_name
|
187
|
-
end
|
188
|
-
|
189
|
-
def trait tag_code
|
190
|
-
trait_name( tag_code ).s
|
191
|
-
end
|
192
|
-
|
193
|
-
|
194
159
|
#~~~~~~~~~~~~~~~~~~~~ SHOW / ABSOLUTE ~~~~~~~~~~~~~~~~~~~~
|
195
160
|
|
196
161
|
def to_show *ignore
|
@@ -205,7 +170,7 @@ class SmartName < Object
|
|
205
170
|
|
206
171
|
case
|
207
172
|
when show_parts.compact.empty?; self
|
208
|
-
when show_parts[0].nil? ;
|
173
|
+
when show_parts[0].nil? ; self.class.joint + show_name
|
209
174
|
else show_name
|
210
175
|
end
|
211
176
|
end
|
@@ -215,8 +180,8 @@ class SmartName < Object
|
|
215
180
|
context = context.to_name
|
216
181
|
parts.map do |part|
|
217
182
|
new_part = case part
|
218
|
-
when /^_user$/i; name_proc =
|
219
|
-
when /^_main$/i;
|
183
|
+
when /^_user$/i; name_proc = self.class.session and name_proc.call or part
|
184
|
+
when /^_main$/i; self.class.params[:main_name]
|
220
185
|
when /^(_self|_whole|_)$/i; context.s
|
221
186
|
when /^_left$/i; context.trunk #note - inconsistent use of left v. trunk
|
222
187
|
when /^_right$/i; context.tag
|
@@ -235,11 +200,11 @@ class SmartName < Object
|
|
235
200
|
part
|
236
201
|
end.to_s.strip
|
237
202
|
new_part.empty? ? context.to_s : new_part
|
238
|
-
end *
|
203
|
+
end * self.class.joint
|
239
204
|
end
|
240
205
|
|
241
206
|
def to_absolute_name *args
|
242
|
-
|
207
|
+
self.class.new to_absolute(*args)
|
243
208
|
end
|
244
209
|
|
245
210
|
def nth_left n
|
@@ -280,7 +245,7 @@ class SmartName < Object
|
|
280
245
|
# shouldn't it use inclusions???
|
281
246
|
def self.substitute! str, hash
|
282
247
|
hash.keys.each do |var|
|
283
|
-
str.gsub!
|
248
|
+
str.gsub! self.class.var_re do |x|
|
284
249
|
hash[var.to_sym]
|
285
250
|
end
|
286
251
|
end
|
data/spec/lib/smart_name_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
3
|
+
require 'core_ext'
|
4
|
+
|
3
5
|
|
4
6
|
describe SmartName do
|
5
7
|
|
@@ -151,41 +153,6 @@ describe SmartName do
|
|
151
153
|
end
|
152
154
|
end
|
153
155
|
|
154
|
-
describe "Cardnames star handling" do
|
155
|
-
it "recognizes star cards" do
|
156
|
-
'*a'.to_name.star?.should be_true
|
157
|
-
end
|
158
|
-
|
159
|
-
it "doesn't recognize star cards with plusses" do
|
160
|
-
'*a+*b'.to_name.star?.should be_false
|
161
|
-
end
|
162
|
-
|
163
|
-
it "recognizes rstar cards" do
|
164
|
-
'a+*a'.to_name.rstar?.should be_true
|
165
|
-
end
|
166
|
-
|
167
|
-
it "doesn't recognize star cards as rstar" do
|
168
|
-
'*a'.to_name.rstar?.should be_false
|
169
|
-
end
|
170
|
-
|
171
|
-
it "doesn't recognize non-star or star left" do
|
172
|
-
'*a+a'.to_name.rstar?.should be_false
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
describe "trait_name?" do
|
177
|
-
it "returns true for content codename" do
|
178
|
-
"bazoinga+*right+*content".to_name.trait_name?(:content).should be_true
|
179
|
-
end
|
180
|
-
|
181
|
-
it "handles arrays" do
|
182
|
-
"bazoinga+*right+*content".to_name.trait_name?(:content, :default).should be_true
|
183
|
-
end
|
184
|
-
|
185
|
-
it "returns false for non-template" do
|
186
|
-
"bazoinga+*right+nontent".to_name.trait_name?(:content).should be_false
|
187
|
-
end
|
188
|
-
end
|
189
156
|
|
190
157
|
describe "#to_absolute" do
|
191
158
|
it "handles _self, _whole, _" do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,13 +2,6 @@
|
|
2
2
|
require 'smart_name'
|
3
3
|
require File.expand_path('./inflection_helper', File.dirname(__FILE__))
|
4
4
|
|
5
|
-
class CardMock < String
|
6
|
-
def name() to_name end
|
7
|
-
end
|
8
|
-
|
9
|
-
SmartName.name_attribute= :name
|
10
|
-
SmartName.codes= { :content => 1 }
|
11
|
-
SmartName.lookup= { 1 => CardMock.new('*content'), }
|
12
5
|
|
13
6
|
RSpec.configure do |config|
|
14
7
|
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-05
|
13
|
+
date: 2013-06-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- README.rdoc
|
121
121
|
- Rakefile
|
122
122
|
- VERSION
|
123
|
+
- lib/core_ext.rb
|
123
124
|
- lib/smart_name.rb
|
124
125
|
- spec/inflection_helper.rb
|
125
126
|
- spec/lib/smart_name_spec.rb
|
@@ -143,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
144
|
version: '0'
|
144
145
|
segments:
|
145
146
|
- 0
|
146
|
-
hash: -
|
147
|
+
hash: -3553502979741413020
|
147
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
149
|
none: false
|
149
150
|
requirements:
|