ld 0.3.5 → 0.3.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d05d1f8966086a579dab4f78698c9036bbb327c
4
- data.tar.gz: 132d98e82a6884d27211af170e9596565ebdbe21
3
+ metadata.gz: d76c2f14f55e59bb505bd1f9f9214beecb31528a
4
+ data.tar.gz: 5ef39a8ec6f6a2ffb48932c2e163e92b22662c56
5
5
  SHA512:
6
- metadata.gz: bf1dbe0b4f8af249609fc80c1b242bf659a8b4f569ebb4dc067421ebfdf9463fb66eb854c2cdbdf07ba7e4042724378f77b32f6c7c96c64e8f6ce516abab3e1c
7
- data.tar.gz: c2ad5be258772c13de1921d45fc6e8743c0ee5ab379bb1183f44eb2457f577ad18197f9830bbaa329d63a06be1b8b3fb7be56f7e0cc1ab97b70356698e65119d
6
+ metadata.gz: b38e7385928f8f189a1946e6af11c31dfe1ba184f9a89110b92b258c08ed6ec8bca2a313845fcbcc896af3082174a2cf76de72307946cde2a4fa5808f9f7ec32
7
+ data.tar.gz: d1202b681920724558cf73c931c21c28bd30c76da49778c66598aec8098810afa8dbf5e8870bbd997642176ff0c2ebe0dcd9b77ec4342c8abd4ebc3b20b03214
data/README.md CHANGED
@@ -116,6 +116,8 @@ Ld::Print.p users, 'id ,name, created_at'
116
116
  ```
117
117
 
118
118
 
119
+ ## API
120
+
119
121
 
120
122
 
121
123
  ## Development
@@ -0,0 +1,65 @@
1
+ class Ld::Document
2
+
3
+ attr_accessor :doc, :class_name
4
+
5
+ #= 作用 空的实例方法,Ld::Document的大部分功能都由实例提供(个人习惯调用对象方法而不是类方法)
6
+ def initialize file
7
+ @class_name = file.split('.rb')[0].camelize
8
+ @doc = {}
9
+ lines = file.lines
10
+ lines.each_with_index do |line, i|
11
+ arr = line.split(' ')
12
+ if arr.delete_at(0) == 'def'
13
+ notes = get_notes(lines, i)
14
+ if notes.size > 0
15
+ @doc[arr.delete_at(0)] = {
16
+ params:arr.join(' '),
17
+ notes:notes
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def get_notes lines, i
25
+ notes = []
26
+ (i-1).downto(0) do |j|
27
+ arr = lines[j].split(' ')
28
+ if arr[0] == '#='
29
+ notes << {title:arr[1], note:arr[2..(arr.size)].join(' ')}
30
+ else
31
+ return notes
32
+ end
33
+ end
34
+ notes
35
+ end
36
+
37
+
38
+ def class_name
39
+
40
+ end
41
+
42
+ def self.write_readme readme_path = '/Users/liudong/ruby/my_gems/ld/README2.md'
43
+ docs = Ld::File.open('/Users/liudong/ruby/my_gems/ld/lib/ld').search_regexp(/.rb$/).map{|f| Ld::Document.new f}
44
+ arr = ["# API"]
45
+ docs.map do |doc|
46
+ arr << "## #{doc.class_name}"
47
+ arr << "```ruby"
48
+ doc.doc.each do |k, v|
49
+ v[:notes].each do |note|
50
+ arr << "#{note[:title]}:#{note[:note]}"
51
+ end
52
+ arr << "#{k} #{v[:params]}"
53
+ end
54
+ arr << "```"
55
+ end
56
+ File.open readme_path,'w' do |file|
57
+ arr.each do |line|
58
+ arr.puts line
59
+ end
60
+ file.colse
61
+ end
62
+ end
63
+
64
+ end
65
+
@@ -0,0 +1,8 @@
1
+ class Ld::Hint
2
+
3
+ def initialize hash
4
+
5
+ end
6
+
7
+
8
+ end
@@ -0,0 +1,36 @@
1
+ class Ld::ParameterError
2
+ attr_accessor :clazz, :hint
3
+
4
+ def initialize clazz, hint
5
+ @clazz = clazz
6
+ @hint = hint
7
+ end
8
+
9
+ def self.create clazz, &block
10
+ runtime_error = self.new clazz
11
+ block.call runtime_error
12
+ runtime_error
13
+ end
14
+
15
+ def add_run
16
+
17
+ end
18
+
19
+ def raise hash
20
+ lines = []
21
+ case hash[:error_type]
22
+ when :example
23
+ lines =
24
+ when :scope
25
+ lines = [
26
+ "scope参数说明:",
27
+ " 1 不能为空",
28
+ " 2 格式'单元格1:单元格2',如'A1:B2'代表读取以A1与B2为对角的矩形范围中的所有内容",
29
+ ]
30
+ end
31
+
32
+ lines.each{|line| puts line}
33
+
34
+ end
35
+
36
+ end
@@ -4,6 +4,7 @@ Spreadsheet.client_encoding = 'UTF-8'
4
4
  class Ld::Excel
5
5
  attr_accessor :excel, :path
6
6
 
7
+ #= 参数 path(可以为空)
7
8
  def initialize path = nil
8
9
  if path
9
10
  if path.match(/.xls$/)
@@ -21,21 +22,37 @@ class Ld::Excel
21
22
  end
22
23
  end
23
24
 
25
+ #= 作用 打开一个xls文件
26
+ #= 参数 xls文件的全路径或相对路径的字符串
27
+ #= 示例 Ld::Excel.open 'project.xls'
28
+ #= 返回 Ld::Excel实例
24
29
  def self.open path
25
30
  self.new path
26
31
  end
27
-
28
- def open_sheet name
29
- Ld::Sheet.open @excel, name
32
+
33
+ #= 作用 写xls文件(会创建一个新的xls文件)
34
+ #= 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
35
+ #= 参数1 path 字符串,指定要写的文件路径(最好使用全路径)
36
+ def self.write path, &block
37
+ if path.class == Hash
38
+ path = path[:file_path]
39
+ end
40
+ excel = Ld::Excel.new
41
+ block.call excel
42
+ excel.save path
30
43
  end
31
44
 
32
- def flush
33
- @excel = Ld::Excel.open @path
45
+ #= 作用 write的同名方法,作用和使用方法完全一样
46
+ def self.create path, &block
47
+ self.write path, &block
34
48
  end
35
49
 
36
- # Example: address = "Sheet1?a1:f5"
50
+ #= 作用 读xls文件中的内容
51
+ #= 示例 Ld::Excel.read "Sheet1?A1:B2"
52
+ #= 示例 Ld::Excel.read "Sheet1?A1:B2+C,D"
53
+ #= 示例 Ld::Excel.read "Sheet1?A1:B2+C,D,1,2"
54
+ #= 返回 二维数组
37
55
  def read params, show_location = false
38
-
39
56
  case params.class.to_s
40
57
  when 'String'
41
58
  shett_name, scope = params.split('?')
@@ -50,29 +67,51 @@ class Ld::Excel
50
67
  end
51
68
  end
52
69
 
70
+ #= 作用 与read方法相同(但会多返回坐标数据)
71
+ #= 返回 与read方法相同(但会多返回坐标数据)
72
+ #= 示例 与read方法相同(但会多返回坐标数据)
73
+ def read_with_location params
74
+ read params, true
75
+ end
76
+
77
+ #= 作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了)
78
+ #= 参数 无需参数
79
+ #= 返回 Ld::Excel实例
80
+ def flush
81
+ @excel = Ld::Excel.open @path
82
+ end
53
83
 
54
- # 保存文件
84
+ #= 作用 保存数据到一个路径
85
+ #= 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
86
+ #= 参数1 path 字符串,指定要写的文件路径(最好使用全路径)
55
87
  def save path
56
88
  puts "Covers a file: #{path}" if File.exist? path
57
89
  @excel.write path
58
90
  puts "Excel save success!"
59
91
  self
92
+ rescue
93
+ puts $!
94
+ puts $@
95
+ false
60
96
  end
61
97
 
62
98
  def new_sheet name
63
99
  Ld::Sheet.new @excel, name
64
100
  end
65
101
 
102
+ def open_sheet name
103
+ Ld::Sheet.open @excel, name
104
+ end
105
+
66
106
  def write_sheet sheet_name, &block
67
107
  sheet = new_sheet sheet_name
68
108
  block.call sheet
69
109
  sheet.save
70
- end
71
-
72
- def self.create hash, &block
73
- excel = Ld::Excel.new
74
- block.call excel
75
- excel.save hash[:file_path]
110
+ true
111
+ rescue
112
+ puts $!
113
+ puts $@
114
+ false
76
115
  end
77
116
 
78
117
  end
@@ -1,6 +1,14 @@
1
1
  class Ld::Sheet
2
2
  attr_accessor :excel, :sheet
3
3
 
4
+ # PARAMETER_ERROR ||= Ld::ParameterError.new self do |e|
5
+ # e.hint :scope => [
6
+ # "Example:",
7
+ # " read(String): read('abc?A1:B2')",
8
+ # " read(Hash): read({sheet:'abc',scope:'A1:B2',insert:'',delete:'',location:false})",
9
+ # ]
10
+ # end
11
+
4
12
  ABSCISSA = {}
5
13
  if ABSCISSA.empty?
6
14
  zm = 'A'
@@ -8,6 +16,9 @@ class Ld::Sheet
8
16
  19999.times{|i| ABSCISSA[zm.succ!] = i+1}
9
17
  end
10
18
 
19
+ #= 参数1 Ld::Excel实例
20
+ #= 参数2 String:sheet名称
21
+ #= 参数3 String:有'new'与'open'两种选择
11
22
  def initialize excel, name, type = 'new'
12
23
  raise "sheet name is nil" if !name
13
24
  @excel = excel
@@ -25,27 +36,31 @@ class Ld::Sheet
25
36
  @format = @sheet.default_format
26
37
  end
27
38
 
39
+ #= 作用 读sheet页数据
40
+ #= 参数1 String:指定要读取的范围
41
+ #= 参数2 Blooean:是否要返回坐标?
42
+ #= 返回 二维数组
28
43
  def read scope, show_location = false
29
44
  raise "scope params is nil" if !scope
30
- map = parse_scope_to_map scope
45
+ map = read_scope_to_map scope
31
46
  read_arrs map, show_location
32
47
  end
33
48
 
49
+ #= 作用 解析范围参数
34
50
  def parse_string_scope scope
51
+ PARAMETER_ERROR.hint_and_raise :scope, "'+' or '-' 只能存在1个" if scope.split('+').size > 2 or scope.split('-').size > 2
35
52
  hash = {}
36
53
  scope.upcase!
37
- raise "params error! \n'+' 只能有1个" if scope.split('+').size > 2
38
- raise "params error! \n'-' 只能有1个" if scope.split('-').size > 2
39
54
  if scope.include? '+'
40
55
  hash[:scope], other = scope.split('+')
41
56
  if other.include? '-'
42
- hash[:adds], hash[:mins] = other.split('-')
57
+ hash[:insert], hash[:delete] = other.split('-')
43
58
  else
44
- hash[:adds] = other
59
+ hash[:insert] = other
45
60
  end
46
61
  else
47
62
  if scope.include? '-'
48
- hash[:scope], hash[:mins] = scope.split('-')
63
+ hash[:scope], hash[:delete] = scope.split('-')
49
64
  else
50
65
  hash[:scope] = scope
51
66
  end
@@ -53,19 +68,18 @@ class Ld::Sheet
53
68
  hash
54
69
  end
55
70
 
56
- def parse_scope_to_map scope
71
+ #= 作用 使用范围参数构建maps(预读)
72
+ def read_scope_to_map scope
57
73
  scope = parse_string_scope scope if scope.class == String
58
- raise "params lack fields ':scope'!" if !scope[:scope]
59
- raise "params syntax error! lack ':'" if !scope[:scope].match(/:/)
60
- raise "params syntax error! ':' 只能有1个" if scope[:scope].split(':').size > 2
74
+ PARAMETER_ERROR.hint_and_raise :scope, "缺少scope参数,或':',或':'存在多个" if !scope[:scope] or !scope[:scope].match(/:/) or scope[:scope].split(':').size > 2
61
75
  a, b = scope[:scope].split(':').map{|point| parse_point point}
62
76
  cols = (a[:character]..b[:character]).to_a
63
77
  rows = (a[:number]..b[:number]).to_a
64
- maps_add rows, cols, scope[:adds].upcase if scope[:adds]
65
- maps_min rows, cols, scope[:mins].upcase if scope[:mins]
78
+ insert_maps rows, cols, scope[:insert].upcase if scope[:insert]
79
+ delete_maps rows, cols, scope[:delete].upcase if scope[:delete]
66
80
 
67
- if scope[:mins]
68
- raise "mins 参数只能是 String" if scope[:mins].class != String
81
+ if scope[:delete]
82
+ raise "delete 参数只能是 String" if scope[:delete].class != String
69
83
  end
70
84
  rows = rows.uniq.sort
71
85
  cols = cols.uniq.sort
@@ -87,47 +101,49 @@ class Ld::Sheet
87
101
  maps
88
102
  end
89
103
 
90
- def maps_add rows, cols, adds
91
- raise "adds 参数只能是 String" if adds.class != String
92
- add_arr = adds.split(',').map do |add|
93
- if add.match(/:/)
94
- raise "add params syntax error! \n'#{add}'" if add.split(':').size > 2
95
- a, b = add.split(':')
104
+ #= 作用 多读一些行或列
105
+ def insert_maps rows, cols, inserts
106
+ raise "inserts 参数只能是 String" if inserts.class != String
107
+ insert_arr = inserts.split(',').map do |insert|
108
+ if insert.match(/:/)
109
+ raise "insert params syntax error! \n'#{insert}'" if insert.split(':').size > 2
110
+ a, b = insert.split(':')
96
111
  (a..b).to_a
97
112
  else
98
- add
113
+ insert
99
114
  end
100
115
  end
101
- add_arr.flatten.each do |add|
102
- if is_row? add
103
- rows << add.to_i
116
+ insert_arr.flatten.each do |insert|
117
+ if is_row? insert
118
+ rows << insert.to_i
104
119
  else
105
- cols << add.upcase
120
+ cols << insert.upcase
106
121
  end
107
122
  end
108
123
  end
109
124
 
110
- def maps_min rows, cols, mins
111
- raise "mins 参数只能是 String" if mins.class != String
112
- min_arr = mins.split(',').map do |min|
113
- if min.match(/:/)
114
- raise "min params syntax error! \n'#{min}'" if min.split(':').size > 2
115
- a, b = min.split(':')
125
+ #= 作用 少读一些行或列
126
+ def delete_maps rows, cols, deletes
127
+ raise "deletes 参数只能是 String" if deletes.class != String
128
+ del_arr = deletes.split(',').map do |del|
129
+ if del.match(/:/)
130
+ raise "del params syntax error! \n'#{del}'" if del.split(':').size > 2
131
+ a, b = del.split(':')
116
132
  (a..b).to_a
117
133
  else
118
- min
134
+ del
119
135
  end
120
136
  end
121
- min_arr.flatten.each do |min|
122
- if is_row? min
123
- rows.delete min.to_i
137
+ del_arr.flatten.each do |del|
138
+ if is_row? del
139
+ rows.delete del.to_i
124
140
  else
125
- cols.delete min.upcase
141
+ cols.delete del.upcase
126
142
  end
127
143
  end
128
144
  end
129
145
 
130
- # show_location 带不带坐标index数据
146
+ #= 作用 读二维数据(使用maps)
131
147
  def read_arrs map_arrs, show_location
132
148
  map_arrs.map do |map_arr|
133
149
  map_arr.map do |map|
@@ -141,7 +157,7 @@ class Ld::Sheet
141
157
  end
142
158
  end
143
159
 
144
- # 通过x,y坐标获取unit内容
160
+ #= 作用 通过x,y坐标获取一个单元格的内容
145
161
  def read_unit_by_xy x, y, parse
146
162
  # puts "x: #{x}\ty: #{y}"
147
163
  unit = @sheet.row(y)[x]
@@ -153,6 +169,7 @@ class Ld::Sheet
153
169
  return unit
154
170
  end
155
171
 
172
+ #= 作用 判断要添加或要移除的是一行还是一列
156
173
  def is_row? row
157
174
  if row.to_i.to_s == row.to_s
158
175
  return true
@@ -160,14 +177,17 @@ class Ld::Sheet
160
177
  false
161
178
  end
162
179
 
180
+ #= 作用 打开一个sheet
163
181
  def self.open excel, name
164
182
  self.new excel, name, 'open'
165
183
  end
166
184
 
185
+ #= 作用 创建一个sheet
167
186
  def self.create excel, name
168
187
  self.new excel, name, 'new'
169
188
  end
170
189
 
190
+ #= 作用 将数据写入sheet
171
191
  def save
172
192
  point = parse_point @point
173
193
  raise '保存sheet必须要有内容,请 set_rows' if !@rows
@@ -184,7 +204,7 @@ class Ld::Sheet
184
204
  self
185
205
  end
186
206
 
187
- # 解析一个 content_url
207
+ #= 作用 解析一个字符串坐标(如'A1')返回x,y坐标('A1'返回[0,0])
188
208
  def parse_point point
189
209
  raise "无法解析excel坐标,坐标需要是String,不能是#{point.class.to_s}" if point.class != String
190
210
  point.upcase!
@@ -195,11 +215,13 @@ class Ld::Sheet
195
215
  {:character => characters[0], :number => numbers[0].to_i}
196
216
  end
197
217
 
218
+ #= 作用 在当前sheet中添加主体数据(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
198
219
  def set_rows rows
199
220
  raise '必须是一个数组且是一个二维数组' if rows.class != Array && rows.first.class != Array
200
221
  @rows = rows
201
222
  end
202
223
 
224
+ #= 作用 在当前sheet的主体内容顶上方添加一个表头(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
203
225
  def set_headings headings
204
226
  if headings
205
227
  raise 'headings 必须是一个数组' if headings.class != Array
@@ -209,12 +231,13 @@ class Ld::Sheet
209
231
  end
210
232
  end
211
233
 
212
- def set_row row
213
- raise 'add_row 传入的必须是一个数组' if row.class != Array
234
+ #= 作用 在当前sheet的主体内容末尾添加一行数据(传入一维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
235
+ def insert_row row
236
+ raise 'insert_row 传入的必须是一个数组' if row.class != Array
214
237
  @rows << row
215
238
  end
216
239
 
217
- # 通过xy坐标往unit写内容
240
+ #= 作用 通过x,y坐标往一个单元格中写入数据,但不写入(只有调用Ld::Excel的实例方法save才会写入io)
218
241
  def write_unit_by_xy x, y, unit
219
242
  if unit.class == Array
220
243
  unit = unit.to_s
@@ -223,23 +246,28 @@ class Ld::Sheet
223
246
  @sheet.row(x)[y] = unit
224
247
  end
225
248
 
249
+ #= 作用 设置当前sheet页的字体颜色
226
250
  def set_color color
227
251
  @format.font.color = color
228
252
  end
229
253
 
254
+ #= 作用 设置当前sheet页的字体大小
230
255
  def set_font_size size
231
256
  raise 'size 必须是一个整数' if size.class != Fixnum
232
257
  @format.font.size = size
233
258
  end
234
259
 
260
+ #= 作用 设置当前sheet页的字体
235
261
  def set_font font
236
262
  @format.font.name = font
237
263
  end
238
264
 
265
+ #= 作用 设置当前sheet页的单元格宽度(暂时无效)
239
266
  def set_weight weight
240
267
  @format
241
268
  end
242
269
 
270
+ #= 作用 设置当前sheet页的字体颜色
243
271
  def set_point point
244
272
  @point = point
245
273
  end
data/lib/ld/file/file.rb CHANGED
@@ -1,118 +1,198 @@
1
1
  class Ld::File
2
2
 
3
- attr_accessor :path, :base_name, :name, :type
3
+ attr_accessor :path, :name, :exist, :size, :mode, :stat
4
+ @@current_path = Dir.pwd
5
+ @@exclude = ['.', '..']
4
6
 
7
+ #= 作用 创建一个Ld::File实例(不初始化参数)
5
8
  def initialize path
6
- # raise "file is not found!\n#{path}" if !File.exist? path
7
- @path = path
9
+ @path = path[0] == '/' ? path : "#{Dir.pwd}/#{path}"
8
10
  @name = File.basename @path
9
- @base_name = name.split('.')[0]
10
- @type = File.directory?(@path) ? 1 : 0
11
+ if File.exist? @path
12
+ @exist = true
13
+ @type = File.ftype path
14
+ @stat = File.stat path
15
+ @size = @stat.size
16
+ @mode = @stat.mode
17
+ else
18
+ @exist = false
19
+ @type = 'not found'
20
+ end
11
21
  end
12
22
 
13
- def self.open_dir path
14
- Ld::File.new path
23
+ #= 作用 创建一个Ld::File实例(始化参数)
24
+ def self.open path
25
+ self.new path
15
26
  end
16
27
 
17
- def self.open path
18
- if File.exist? path
19
- self.new path
20
- else
21
- return nil
22
- end
28
+ # 作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
29
+ def children
30
+ dir!
31
+ Dir.foreach(@path).map{|n| Ld::File.new("#{@path}/#{n}") if !is_exclude? n}.compact.sort{|a,b| a.type <=> b.type}
23
32
  end
24
33
 
25
- def brothers
26
- father.children
34
+ def is_exclude? name
35
+ @@exclude.include? name
27
36
  end
28
37
 
29
- def children(remove = nil)
30
- arr = []
31
- Dir.foreach(@path)do |p|
32
- removes = ['.','..','.DS_Store']
33
- removes << remove if remove
34
- if !removes.include?(p)
35
- arr << Ld::File.new("#{@path}/#{p}")
36
- end
37
- end
38
- arr.sort!{|a,b| b.type-a.type}
39
- arr
38
+ #= 作用 返回当前所在目录(Dir.pwd)
39
+ def self.current
40
+ Ld::File.new @@current_path
41
+ end
42
+
43
+ def exist!
44
+ raise "不存在的 #{path}" if !@exist
40
45
  end
41
46
 
42
- def search_files regexp
43
- arr = []
44
- iter_search_files regexp, arr
45
- arr
47
+ def dir?
48
+ type == 'directory'
46
49
  end
47
50
 
48
- def search_dirs
49
- arr = []
50
- iter_search_dir arr
51
- arr
51
+ def file?
52
+ type == 'file'
52
53
  end
53
54
 
54
- def iter_search_dir arr
55
- children.each do |f|
56
- if f.type == 1
57
- arr << f
58
- f.iter_search_dir arr
55
+ def dir!
56
+ raise "这不是一个目录,而是一个#{type}:#{path}" if type != 'directory'
57
+ end
58
+
59
+ def file!
60
+ raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file'
61
+ end
62
+
63
+ #= 作用 返回一个一级目录或文件,如果不存在则返回nil
64
+ def find name
65
+ dir!
66
+ Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}"
67
+ end
68
+
69
+ #= 作用 返回所有精确匹配的(查当前目录下的所有节点)目录和文件
70
+ def search name, type = :all
71
+ dir!
72
+ results = []
73
+ iter_search name, results
74
+ case type.to_s
75
+ when 'all'
76
+ results
77
+ when 'file'
78
+ results.map{|f| f.type == 'file'}
79
+ when 'dir'
80
+ results.map{|f| f.type == 'directory'}
81
+ end
82
+ end
83
+
84
+ #= 作用 返回所有模糊匹配的(查当前目录下的所有节点)目录和文件
85
+ def search_regexp regexp, type = :all
86
+ dir!
87
+ results = []
88
+ iter_search_regexp regexp, results
89
+ case type.to_s
90
+ when 'all'
91
+ results
92
+ when 'file'
93
+ results.map{|f| f.type == 'file'}
94
+ when 'dir'
95
+ results.map{|f| f.type == 'directory'}
96
+ end
97
+ end
98
+
99
+ def iter_search name, results
100
+ children.each do |file|
101
+ if file.name == name
102
+ results << file
103
+ end
104
+ if file.dir?
105
+ file.iter_search name, results
59
106
  end
60
107
  end
61
- self
62
108
  end
63
109
 
64
- def iter_search_files regexp, arr
65
- children.each do |f|
66
- if f.type == 1
67
- f.iter_search_files regexp, arr
110
+ def iter_search_regexp regexp, results
111
+ children.each do |file|
112
+ if file.name.match(regexp)
113
+ results << file
68
114
  end
69
- if f.name.match(regexp)
70
- arr << f
115
+ if file.dir?
116
+ file.iter_search_regexp regexp, results
71
117
  end
72
118
  end
73
- self
74
119
  end
75
120
 
76
- def father
77
- arr = @path.split('/')
78
- arr.pop
79
- Ld::File.new(arr.join('/'))
121
+ #= 作用 返回所有lines
122
+ def lines
123
+ File.open(@path).readlines
80
124
  end
81
125
 
82
- def find name
83
- name = name.to_s
84
- children.each do |f|
85
- if f.name == name
86
- return f
126
+ # def method_missing name
127
+ # find name
128
+ # end
129
+
130
+ #= 作用 改名称
131
+ def rename new_name
132
+ new_path = "#{dir.path}/#{new_name}"
133
+ if File.rename @path, new_path
134
+ @path = new_path
135
+ end
136
+ end
137
+
138
+ #= 作用 删除当前文件
139
+ def delete
140
+ puts "删除!:#{path}\n,确认请输入 delete_file,"
141
+ if gets.chomp == 'delete_file'
142
+ if File.delete path == 1
143
+ @exist = false
144
+ puts "删除成功 #{path}"
87
145
  end
88
146
  end
89
- return nil
90
147
  end
91
148
 
92
- def read
93
- File.open(@path).read
149
+ #= 作用 返回所有文件(下一级)
150
+ def files
151
+ children.select{|f| f.type == 'file'}
94
152
  end
95
153
 
96
- def readlines
97
- File.open(@path).readlines
154
+ #= 作用 返回父目录
155
+ def parent
156
+ Ld::File.new(File.dirname @path)
98
157
  end
99
158
 
100
- def size
101
- File.size path
159
+ #= 作用 返回所有兄弟
160
+ def siblings
161
+ parent.children
102
162
  end
103
163
 
104
- def lines
105
- arr = []
106
- File.new(path).each_line{|l| arr << l }
107
- arr
164
+ #= 作用 返回所有目录
165
+ def dirs
166
+ children.select{|f| f.type == 'directory'}
167
+ end
168
+
169
+ #= 作用 输出目录中所有条目
170
+ def ls
171
+ if type == 'directory'
172
+ Ld::Print.ls self
173
+ elsif type == 'file'
174
+ Ld::Print.ls self.parent
175
+ end
176
+ end
177
+
178
+ def save
179
+
180
+ end
181
+
182
+ def self.write path, arr
183
+ File.open(path)
108
184
  end
109
185
 
110
- def exist?
111
- File.exist? path
186
+ # test:
187
+ def self.test
188
+ sdf{100.times{Ld::File.new('app').search_regexp //}}
112
189
  end
113
190
 
114
- def method_missing name
115
- find name
191
+ def sdf &block
192
+ t1 = Time.new
193
+ block.call
194
+ t2 = Time.new
195
+ puts t2 - t1
116
196
  end
117
197
 
118
198
  end
@@ -2,24 +2,33 @@ require 'terminal-table'
2
2
 
3
3
  class Ld::Print
4
4
 
5
- def initialize models
6
- @models = models
5
+ def self.p models, fields
6
+ self.print "#{models.first.class.to_s}" do |table|
7
+ table.set_headings (fields.class == Array ? fields : fields.split(',')).map{|f| f.rstrip.lstrip}
8
+ table.set_rows models.map { |model|
9
+ fields.map { |field|
10
+ value = model.send field
11
+ value = value.strftime("%Y/%m/%d %H:%M:%S") if [Date, Time, DateTime, ActiveSupport::TimeWithZone].include? value.class
12
+ value
13
+ }
14
+ }
15
+ end
7
16
  end
8
17
 
9
- def self.p models,fields
18
+ def self.print hash
10
19
  t = Terminal::Table.new
11
- t.title = models.first.class.to_s
12
- fields = (fields.class == Array ? fields : fields.split(',')).map{|f| f.rstrip.lstrip}
13
- t.headings = fields
14
- models.map { |model|
15
- fields.map { |field|
16
- value = model.send field
17
- value = value.strftime("%Y/%m/%d %H:%M:%S") if [Date, Time, DateTime, ActiveSupport::TimeWithZone].include? value.class
18
- value
19
- }
20
- }#.sort{|a,b| a[2] <=> b[2]}
21
- .each{|row| t.add_row row}
20
+ t.title = hash[:title]
21
+ t.headings = hash[:headings]
22
+ t.rows = hash[:rows]
22
23
  puts t
23
24
  end
24
25
 
25
- end
26
+ def self.ls dir
27
+ t = Terminal::Table.new
28
+ t.title = "目录列表:#{dir.path}"
29
+ t.headings = ["name","type","size","permission"]
30
+ t.rows = dir.children.map{|f| [f.name, f.type, f.size, f.mode] if f.name[0] != '.'}.compact.sort{|a,b| a[1] <=> b[1]}
31
+ puts t
32
+ end
33
+
34
+ end
@@ -34,7 +34,8 @@ class Ld::Project
34
34
  fs = '字段,字段类型,描述,空约束,默认值,精度位数,limit'.split(',')
35
35
  indexs = fs.map{|f| @tables.headings.index(f)}.compact
36
36
  rows = []
37
- @tables.rows.select{|a| a[0]==model_name}.each{|arr| rows << indexs.map{|i| arr[i]} }
37
+ @tables.rows.select{|a| a[0]==model_name}
38
+ .each{|arr| rows << indexs.map{|i| arr[i]} }
38
39
  puts Terminal::Table.new(
39
40
  :title => "#{title_str}:字段解释",
40
41
  :headings => fs,
@@ -44,9 +45,8 @@ class Ld::Project
44
45
  fs = 'has_many,belongs_to,has_one'.split(',')
45
46
  indexs = fs.map{|f| @models.headings.index(f)}.compact
46
47
  rows = []
47
- @models.rows.select{|a| a[0]==model_name}.each{|arr|
48
- rows << indexs.map{|i| arr[i]}
49
- }
48
+ @models.rows.select{|a| a[0]==model_name}.
49
+ each{|arr| rows << indexs.map{|i| arr[i]} }
50
50
  puts Terminal::Table.new(
51
51
  :title => "#{title_str}:关联关系",
52
52
  :headings => fs,
@@ -56,7 +56,8 @@ class Ld::Project
56
56
  fs = '控制器,action,请求类型,URI,帮助方法'.split(',')
57
57
  indexs = fs.map{|f| @routes.headings.index(f)}.compact
58
58
  rows = []
59
- @routes.rows.select{|a| a[0]==model_name}.each{|arr| rows << indexs.map{|i| arr[i]} }
59
+ @routes.rows.select{|a| a[0]==model_name}.
60
+ each{|arr| rows << indexs.map{|i| arr[i]} }
60
61
  puts Terminal::Table.new(
61
62
  :title => "#{title_str}:路由",
62
63
  :headings => fs,
@@ -66,7 +67,8 @@ class Ld::Project
66
67
  fs = '文件夹名,行数,文件名,path'.split(',')
67
68
  indexs = fs.map{|f| @views.headings.index(f)}.compact
68
69
  rows = []
69
- @views.rows.select{|a| a[0]==model_name}.each{|arr| rows << indexs.map{|i| arr[i]} }
70
+ @views.rows.select{|a| a[0]==model_name}.
71
+ each{|arr| rows << indexs.map{|i| arr[i]} }
70
72
  puts Terminal::Table.new(
71
73
  :title => "#{title_str}:视图",
72
74
  :headings => fs,
@@ -76,7 +78,8 @@ class Ld::Project
76
78
  fs = 'action个数,文件行数,所有action'.split(',')
77
79
  indexs = fs.map{|f| @controllers.headings.index(f)}.compact
78
80
  rows = []
79
- @controllers.rows.select{|a| a[0]==model_name}.each{|arr| rows << indexs.map{|i| arr[i]} }
81
+ @controllers.rows.select{|a| a[0]==model_name}.
82
+ each{|arr| rows << indexs.map{|i| arr[i]} }
80
83
  puts Terminal::Table.new(
81
84
  :title => "#{title_str}:控制器",
82
85
  :headings => fs,
data/lib/ld/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ld
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Dong
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-08 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: terminal-table
@@ -92,6 +92,9 @@ files:
92
92
  - bin/setup
93
93
  - ld.gemspec
94
94
  - lib/ld.rb
95
+ - lib/ld/document/document.rb
96
+ - lib/ld/error/hint.rb
97
+ - lib/ld/error/parameter_error.rb
95
98
  - lib/ld/excel/excel.rb
96
99
  - lib/ld/excel/sheet.rb
97
100
  - lib/ld/file/dir.rb