cc 1.2.1 → 1.4.0

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.
Files changed (9) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -3
  3. data/cc.rb +1 -0
  4. data/monkey-patch.rb +375 -1
  5. data/text-client.rb +297 -0
  6. data/text-server.rb +403 -0
  7. data/text-tools.rb +418 -0
  8. data/tree.rb +184 -184
  9. metadata +5 -2
data/tree.rb CHANGED
@@ -1,184 +1,184 @@
1
- #coding:utf-8
2
-
3
- ############################################################################################################
4
- # The Class Tree is a recursive hash-generator based on Hash. #
5
- # #
6
- # HOW TO USE #
7
- ############################################################################################################
8
-
9
- =begin
10
- require 'cc'
11
- CC.use 'tree'
12
-
13
- # 奇怪的数据结构增加了!!!
14
- a = Tree.init
15
-
16
- # 逐个赋值
17
- a[1][2][3] = :'4'
18
- a['1']['2']['3']=4
19
- p a
20
- puts Array.new(32,'-').join
21
-
22
- # 直接挂载
23
- a.mount '1/2/3/4/5/6', '7'
24
- p a
25
- a['1/2/3'] = 4
26
- p a
27
- puts Array.new(32,'-').join
28
-
29
- # 不能给根目录赋值
30
- begin
31
- a['/'] = :root
32
- rescue Tree::PathError => exception
33
- puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
34
- end
35
- begin
36
- a.mount '/', :home
37
- rescue Tree::PathError => exception
38
- puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
39
- end
40
- p a.route('/')==a['/']
41
- puts Array.new(32,'-').join
42
-
43
- # 合并到某分支,连接到路径
44
- b = Tree.init
45
- b['a/b/c'] = :d
46
- a.emerge '1/2/3', b['a']
47
- p a # a['1/2/b'].parent==a['1/2/3'].parent # a['1/2/3'] is 4 and not respond to parent
48
- begin
49
- a.contact '1/2/3', b['a']
50
- rescue Tree::PathError => exception
51
- puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
52
- end
53
- a.contact '1/2', b
54
- p a
55
- puts Array.new(32,'-').join
56
-
57
- # 回溯父节点
58
- t = Tree.init
59
- p t['a/b/c'] = :d
60
- p t
61
- puts Array.new(32,'-').join
62
- p '1:',t['1'],t['1/'].parent
63
- puts Array.new(32,'-').join
64
- p '1/2:',t['/1/2'],t['1/2'].parent
65
- puts Array.new(32,'-').join
66
- p '1/2/3:',t['1/2/3/'],t['1/2/3'].parent
67
- puts Array.new(32,'-').join
68
- p '1/2/3/4:',t['1/2/3/4'],t['1/2/3/4'].parent
69
- puts Array.new(32,'-').join
70
- p 'a:',t['a'],t['a'].parent
71
- puts Array.new(32,'-').join
72
- p 'a/b:',t['a/b'],t['a/b'].parent
73
- puts Array.new(32,'-').join
74
- require './exception'
75
- p 'a/b/c:',t['a/b/c'],t['/a/b/c'].try(:parent)
76
- puts Array.new(32,'-').join
77
- p 'a/b/c/d:',(begin;t['a/b/c/d'];rescue(Exception);'cant pass path="a/b/c/d" with leaf["a/b/c"]';end)
78
- puts Array.new(32,'-').join
79
- =end
80
-
81
- class Tree < Hash
82
- VERSION = '0.2.0'
83
-
84
- def self.init
85
- Tree.new{|tree, path|tree[path] = Tree.new(tree,&tree.default_proc) }
86
- end
87
-
88
- def self.clear
89
- self.init
90
- end
91
-
92
- attr_accessor :parent
93
-
94
- def initialize parent=nil
95
- @parent=parent
96
- super()
97
- end
98
-
99
- def [] key
100
- unless key.instance_of?(String) && key.include?('/')
101
- child = super(key)
102
- child.parent = self if child.respond_to? :parent
103
- return child
104
- end
105
- key.include?('/') ? send(:route,key) : super(key)
106
- end
107
-
108
- def []= key, value
109
- unless key.instance_of?(String) && key.include?('/')
110
- super(key,value)
111
- child = self[key]
112
- child.parent = self if child.respond_to? :parent
113
- return child
114
- end
115
- key.include?('/') ? (key=='/' ? (raise PathError, "Class Tree can't use '/' as a key except rootself.") : send(:mount,key,value)) : super(key,value)
116
- end
117
-
118
- def route path
119
- unless path.instance_of?(String) && path.include?('/')
120
- post = self[path]
121
- post.parent = self if post.respond_to? :parent
122
- return post
123
- end
124
- return self if path=='/'
125
- hops = path.split('/')
126
- hops.delete ''
127
- curr = hops.shift
128
- if hops.empty?
129
- self[curr]
130
- else
131
- # if a leaf exists, any path walkthrough leaf would not pass;
132
- # if no leaf exists, any path walkthrough could be pass.
133
- raise PathError, "The current expected path`#{path}` not exist." unless self[curr].respond_to? :route
134
- self[curr].send "route", hops.join('/')
135
- end
136
- end
137
-
138
- # Caution: #mount will arrive the deepest leaf and change the passthrough
139
- def mount path, store
140
- unless path.instance_of?(String) && path.include?('/')
141
- self[path]=store
142
- post = self[path]
143
- post.parent = self if post.respond_to? :parent
144
- return post
145
- end
146
- raise PathError, "Class Tree can't mount an value on the root." if path=='/'
147
- hops = path.split('/')
148
- hops.delete ''
149
- curr = hops.shift
150
- if hops.empty?
151
- self[curr]=store
152
- self.delete nil # except ['/']=store to {nil=>store}
153
- else
154
- self[curr]=Tree.init
155
- # self[curr].send "route", hops.join('/')
156
- self[curr].send "mount", hops.join('/'), store
157
- end
158
- end
159
-
160
- # Caution: #emerge can adapt on HashObject but loose the tree-ability
161
- def emerge path, tree
162
- if tree.is_a?(Hash)
163
- hops = path.split("/")
164
- popz = hops.pop
165
- curr = hops.join('/')
166
- target = hops.empty? ? self[popz] : self.route(curr)
167
- raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
168
- target.merge!(tree)
169
- return target
170
- end
171
- end
172
-
173
- def contact path, tree
174
- if tree.is_a?(Tree)
175
- target = self.route path
176
- raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
177
- target.merge!(tree)
178
- tree.parent = target
179
- end
180
- end
181
-
182
- class PathError < Exception
183
- end
184
- end
1
+ #coding:utf-8
2
+
3
+ ############################################################################################################
4
+ # The Class Tree is a recursive hash-generator based on Hash. #
5
+ # #
6
+ # HOW TO USE #
7
+ ############################################################################################################
8
+
9
+ =begin
10
+ require 'cc'
11
+ CC.use 'tree'
12
+
13
+ # 奇怪的数据结构增加了!!!
14
+ a = Tree.init
15
+
16
+ # 逐个赋值
17
+ a[1][2][3] = :'4'
18
+ a['1']['2']['3']=4
19
+ p a
20
+ puts Array.new(32,'-').join
21
+
22
+ # 直接挂载
23
+ a.mount '1/2/3/4/5/6', '7'
24
+ p a
25
+ a['1/2/3'] = 4
26
+ p a
27
+ puts Array.new(32,'-').join
28
+
29
+ # 不能给根目录赋值
30
+ begin
31
+ a['/'] = :root
32
+ rescue Tree::PathError => exception
33
+ puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
34
+ end
35
+ begin
36
+ a.mount '/', :home
37
+ rescue Tree::PathError => exception
38
+ puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
39
+ end
40
+ p a.route('/')==a['/']
41
+ puts Array.new(32,'-').join
42
+
43
+ # 合并到某分支,连接到路径
44
+ b = Tree.init
45
+ b['a/b/c'] = :d
46
+ a.emerge '1/2/3', b['a']
47
+ p a # a['1/2/b'].parent==a['1/2/3'].parent # a['1/2/3'] is 4 and not respond to parent
48
+ begin
49
+ a.contact '1/2/3', b['a']
50
+ rescue Tree::PathError => exception
51
+ puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
52
+ end
53
+ a.contact '1/2', b
54
+ p a
55
+ puts Array.new(32,'-').join
56
+
57
+ # 回溯父节点
58
+ t = Tree.init
59
+ p t['a/b/c'] = :d
60
+ p t
61
+ puts Array.new(32,'-').join
62
+ p '1:',t['1'],t['1/'].parent
63
+ puts Array.new(32,'-').join
64
+ p '1/2:',t['/1/2'],t['1/2'].parent
65
+ puts Array.new(32,'-').join
66
+ p '1/2/3:',t['1/2/3/'],t['1/2/3'].parent
67
+ puts Array.new(32,'-').join
68
+ p '1/2/3/4:',t['1/2/3/4'],t['1/2/3/4'].parent
69
+ puts Array.new(32,'-').join
70
+ p 'a:',t['a'],t['a'].parent
71
+ puts Array.new(32,'-').join
72
+ p 'a/b:',t['a/b'],t['a/b'].parent
73
+ puts Array.new(32,'-').join
74
+ require './exception'
75
+ p 'a/b/c:',t['a/b/c'],t['/a/b/c'].try(:parent)
76
+ puts Array.new(32,'-').join
77
+ p 'a/b/c/d:',(begin;t['a/b/c/d'];rescue(Exception);'cant pass path="a/b/c/d" with leaf["a/b/c"]';end)
78
+ puts Array.new(32,'-').join
79
+ =end
80
+
81
+ class Tree < Hash
82
+ VERSION = '0.2.0'
83
+
84
+ def self.init
85
+ Tree.new{|tree, path|tree[path] = Tree.new(tree,&tree.default_proc) }
86
+ end
87
+
88
+ def self.clear
89
+ self.init
90
+ end
91
+
92
+ attr_accessor :parent
93
+
94
+ def initialize parent=nil
95
+ @parent=parent
96
+ super()
97
+ end
98
+
99
+ def [] key
100
+ unless key.instance_of?(String) && key.include?('/')
101
+ child = super(key)
102
+ child.parent = self if child.respond_to? :parent
103
+ return child
104
+ end
105
+ key.include?('/') ? send(:route,key) : super(key)
106
+ end
107
+
108
+ def []= key, value
109
+ unless key.instance_of?(String) && key.include?('/')
110
+ super(key,value)
111
+ child = self[key]
112
+ child.parent = self if child.respond_to? :parent
113
+ return child
114
+ end
115
+ key.include?('/') ? (key=='/' ? (raise PathError, "Class Tree can't use '/' as a key except rootself.") : send(:mount,key,value)) : super(key,value)
116
+ end
117
+
118
+ def route path
119
+ unless path.instance_of?(String) && path.include?('/')
120
+ post = self[path]
121
+ post.parent = self if post.respond_to? :parent
122
+ return post
123
+ end
124
+ return self if path=='/'
125
+ hops = path.split('/')
126
+ hops.delete ''
127
+ curr = hops.shift
128
+ if hops.empty?
129
+ self[curr]
130
+ else
131
+ # if a leaf exists, any path walkthrough leaf would not pass;
132
+ # if no leaf exists, any path walkthrough could be pass.
133
+ raise PathError, "The current expected path`#{path}` not exist." unless self[curr].respond_to? :route
134
+ self[curr].send "route", hops.join('/')
135
+ end
136
+ end
137
+
138
+ # Caution: #mount will arrive the deepest leaf and change the passthrough
139
+ def mount path, store
140
+ unless path.instance_of?(String) && path.include?('/')
141
+ self[path]=store
142
+ post = self[path]
143
+ post.parent = self if post.respond_to? :parent
144
+ return post
145
+ end
146
+ raise PathError, "Class Tree can't mount an value on the root." if path=='/'
147
+ hops = path.split('/')
148
+ hops.delete ''
149
+ curr = hops.shift
150
+ if hops.empty?
151
+ self[curr]=store
152
+ self.delete nil # except ['/']=store to {nil=>store}
153
+ else
154
+ self[curr]=Tree.init
155
+ # self[curr].send "route", hops.join('/')
156
+ self[curr].send "mount", hops.join('/'), store
157
+ end
158
+ end
159
+
160
+ # Caution: #emerge can adapt on HashObject but loose the tree-ability
161
+ def emerge path, tree
162
+ if tree.is_a?(Hash)
163
+ hops = path.split("/")
164
+ popz = hops.pop
165
+ curr = hops.join('/')
166
+ target = hops.empty? ? self[popz] : self.route(curr)
167
+ raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
168
+ target.merge!(tree)
169
+ return target
170
+ end
171
+ end
172
+
173
+ def contact path, tree
174
+ if tree.is_a?(Tree)
175
+ target = self.route path
176
+ raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
177
+ target.merge!(tree)
178
+ tree.parent = target
179
+ end
180
+ end
181
+
182
+ class PathError < Exception
183
+ end
184
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt
@@ -34,6 +34,9 @@ files:
34
34
  - regexp.rb
35
35
  - shell-tools.rb
36
36
  - string.rb
37
+ - text-client.rb
38
+ - text-server.rb
39
+ - text-tools.rb
37
40
  - trace.rb
38
41
  - tree.rb
39
42
  homepage: https://github.com/ChenMeng1365/custom-core
@@ -54,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
57
  - !ruby/object:Gem::Version
55
58
  version: '0'
56
59
  requirements: []
57
- rubygems_version: 3.6.9
60
+ rubygems_version: 4.0.19
58
61
  specification_version: 4
59
62
  summary: custom core
60
63
  test_files: []