rmtools 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,113 @@
1
+ # String#to_proc
2
+ #
3
+ # See http://weblog.raganwald.com/2007/10/stringtoproc.html ( Subscribe in a reader)
4
+ #
5
+ # Ported from the String Lambdas in Oliver Steele's Functional Javascript
6
+ # http://osteele.com/sources/javascript/functional/
7
+ #
8
+ # This work is licensed under the MIT License:
9
+ #
10
+ # (c) 2007 Reginald Braithwaite
11
+ # Portions Copyright (c) 2006 Oliver Steele
12
+ #
13
+ # Permission is hereby granted, free of charge, to any person obtaining
14
+ # a copy of this software and associated documentation files (the
15
+ # "Software"), to deal in the Software without restriction, including
16
+ # without limitation the rights to use, copy, modify, merge, publish,
17
+ # distribute, sublicense, and/or sell copies of the Software, and to
18
+ # permit persons to whom the Software is furnished to do so, subject to
19
+ # the following conditions:
20
+ #
21
+ # The above copyright notice and this permission notice shall be
22
+ # included in all copies or substantial portions of the Software.
23
+ #
24
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+
33
+ class String
34
+ unless ''.respond_to?(:to_proc)
35
+ =begin original definition:
36
+ def to_proc &block
37
+ params = []
38
+ expr = self
39
+ sections = expr.split(/\s*->\s*/m)
40
+ if sections.length > 1 then
41
+ eval sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block && block.binding
42
+ elsif expr.match(/\b_\b/)
43
+ eval "Proc.new { |_| #{expr} }", block && block.binding
44
+ else
45
+ leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
46
+ rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
47
+ if leftSection || rightSection then
48
+ if (leftSection) then
49
+ params.push('$left')
50
+ expr = '$left' + expr
51
+ end
52
+ if (rightSection) then
53
+ params.push('$right')
54
+ expr = expr + '$right'
55
+ end
56
+ else
57
+ self.gsub(
58
+ /(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
59
+ ).scan(
60
+ /([a-z_$][a-z_$\d]*)/i
61
+ ) do |v|
62
+ params.push(v) unless params.include?(v)
63
+ end
64
+ end
65
+ eval "Proc.new { |#{params.join(', ')}| #{expr} }", block && block.binding
66
+ end
67
+ end
68
+ =end
69
+
70
+ RMTools::String_to_proc_cache = {}
71
+ def to_proc &block
72
+ # improving performance
73
+ if !block and proc = RMTools::String_to_proc_cache[self]
74
+ return proc
75
+ end
76
+ params = []
77
+ expr = self
78
+ sections = expr.split(/\s*->\s*/m)
79
+ proc =
80
+ if sections.length > 1
81
+ str = sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }
82
+ (proc = eval str, block && block.binding).string = str
83
+ proc
84
+ elsif expr.match(/\b_\b/)
85
+ Proc.eval "|_| #{expr}", block && block.binding
86
+ else
87
+ leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
88
+ rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
89
+ if leftSection || rightSection
90
+ if leftSection
91
+ params.push('__left')
92
+ expr = '__left' + expr
93
+ end
94
+ if rightSection
95
+ params.push('__right')
96
+ expr = expr + '__right'
97
+ end
98
+ else
99
+ params = gsub(/(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
100
+ ).scan(
101
+ /([a-z_$][a-z_$\d]*)/i
102
+ ).uniq
103
+ end
104
+ Proc.eval "|#{params.join(', ')}| #{expr}", block && block.binding
105
+ end
106
+ RMTools::String_to_proc_cache[self] = proc if !block
107
+ proc
108
+ end
109
+
110
+ end
111
+ end
112
+
113
+
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ class StringScanner
4
+ attr_reader :last
5
+
6
+ def each(re, cbs=nil, &cb)
7
+ @last = 0
8
+ res = scan_until re
9
+ if cbs
10
+ if cbs[0].kinda Integer
11
+ while res
12
+ if cb = cbs[matched.ord]
13
+ cb[self]
14
+ @last = pos
15
+ res = !eos? && scan_until(re)
16
+ else break
17
+ end
18
+ end
19
+ else
20
+ while res
21
+ if cb = cbs.find {|pattern, proc| pattern.in matched}
22
+ # patterns must be as explicit as possible
23
+ cb[1][self]
24
+ @last = pos
25
+ res = !eos? && scan_until(re)
26
+ else break
27
+ end
28
+ end
29
+ end
30
+ else
31
+ while res
32
+ cb[self]
33
+ @last = pos
34
+ res = !eos? && scan_until(re)
35
+ end
36
+ end
37
+ if (cb = cbs[nil]) and !eos?
38
+ cb[tail]
39
+ end
40
+ end
41
+
42
+ def head
43
+ string[@last...pos-matched.size]
44
+ end
45
+
46
+ def tail
47
+ string[pos..-1]
48
+ end
49
+
50
+ def hl_next(re)
51
+ (res = scan_until re) && RMTools.hl(string[pos-1000..pos+1000], res)
52
+ end
53
+
54
+ def self.each string, *args, &b
55
+ new string, *args, &b
56
+ end
57
+
58
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module RMTools
3
+
4
+ def timer(ts=1, output=true)
5
+ timez = ts - 1
6
+ panic, verbose = $panic, $verbose
7
+ $panic = $verbose = false
8
+ t1 = Time.now
9
+ timez.times {yield}
10
+ res = yield
11
+ t2 = (Time.now.to_f*1000).round
12
+ t1 = (t1.to_f*1000).round
13
+ $panic, $verbose = panic, verbose
14
+ res = res.inspect
15
+ puts "#{output ? "res: #{res.size > 1000 ? res[0...1000]+"…" : res}\n" : "size of res string: #{res.to_s.size}, "}one: #{(t2-t1).to_f/ts}ms, total: #{(t2-t1).to_f}ms"
16
+ end
17
+
18
+ def puttime(ms=nil)
19
+ t = Time.now
20
+ if ms
21
+ t.strftime("%H:%M:%S")+sprintf(".%03d ", t.usec/1000)
22
+ else
23
+ t.strftime("%d.%m.%y %H:%M:%S ")
24
+ end
25
+ end
26
+
27
+ def putdate
28
+ Time.now.strftime("%d.%m.%y")
29
+ end
30
+
31
+ module_function :puttime, :putdate
32
+ end
@@ -0,0 +1,106 @@
1
+ module RMTools
2
+
3
+ def format_trace a
4
+ bt, calls, i = [], [], 0
5
+ # $log.info 'a.size', binding
6
+ m = a[0].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
7
+ while i < a.size
8
+ # $log.info i
9
+ m2 = a[i+1] && a[i+1].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
10
+ # $log.info 'm', binding
11
+ # $log.info 'm2', binding
12
+ # $log.info 'm[3] m[1..2]==m2[1..2]', binding if m and m2
13
+ # $log.info 'm[1] m[2]', binding if m
14
+ # $log.info highlighted_line(*m[1..2]) if m
15
+ if m and m[3] and m2 and m[1..2] == m2[1..2]
16
+ calls.unshift " <- `#{m[3]}'"
17
+ elsif m and m[1] !~ /\.gemspec$/ and line = highlighted_line(*m[1..2])
18
+ bt << "#{a[i]}#{calls.join}\n#{line}"
19
+ calls = []
20
+ else bt << a[i]
21
+ end
22
+ i += 1
23
+ m = m2
24
+ end
25
+ # $log << Painter.r("FORMAT DONE! #{bt.size} lines formatted")
26
+ bt
27
+ end
28
+
29
+ def highlighted_line_html file, line
30
+ if File.file?(file)
31
+ " >> <a style=\"color:#0A0; text-decoration: none;\"#{
32
+ " href=\"http://#{
33
+ defined?(DEBUG_SERVER) ? DEBUG_SERVER : 'localhost:8888'
34
+ }/code/#{CGI.escape CGI.escape(file).gsub('.', '%2E')}/#{line}\""
35
+ }>#{read_lines(file, line.to_i).chop}</a>"
36
+ end
37
+ end
38
+
39
+ def format_trace_html a
40
+ bt, calls, i = [], [], 0
41
+ m = a[0].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
42
+ while i < a.size
43
+ m2 = a[i+1] && a[i+1].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
44
+ if m and m[3] and m2 and m[1..2] == m2[1..2]
45
+ calls.unshift " <- `#{m[3]}'"
46
+ elsif m and m[1] !~ /\.gemspec$/ and line = highlighted_line_html(*m[1..2])
47
+ bt << "#{a[i]}#{calls.join}\n#{line}"
48
+ calls = []
49
+ else bt << a[i]
50
+ end
51
+ i += 1
52
+ m = m2
53
+ end
54
+ bt
55
+ end
56
+
57
+ module_function :format_trace, :format_trace_html, :highlighted_line_html
58
+ end
59
+
60
+ # 1.9 may hung up processing IO while generating traceback
61
+ if RUBY_VERSION < '1.9'
62
+
63
+ class Class
64
+
65
+ def trace_format method
66
+ if new.kinda Exception
67
+ if method; class_eval(%{
68
+ def set_backtrace src
69
+ src = #{method} src
70
+ set_bt src
71
+ end
72
+ })
73
+ else class_eval(%{
74
+ def set_backtrace src
75
+ set_bt src
76
+ end
77
+ })
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ class Exception
85
+ alias :set_bt :set_backtrace
86
+
87
+ # If you set (e.g. in irbrc file) IRB logging and Readline::TEMPLOG:
88
+ # module Readline
89
+ # TEMPLOG = "path/to/logs/#{Time.now.to_i}.rb"
90
+ # alias :orig_readline :readline
91
+ # def readline(*args)
92
+ # ln = orig_readline(*args)
93
+ # RMTools::write TEMPLOG, "#{ln}\n"
94
+ # return ln
95
+ # end
96
+ # end
97
+ # it will be possible to get the lines entered in IRB
98
+ # else it reads only ordinal require'd files
99
+
100
+ trace_format :format_trace
101
+
102
+ end
103
+
104
+ SystemStackError.trace_format false
105
+
106
+ end
@@ -0,0 +1,71 @@
1
+ class Array
2
+
3
+ def to_tree(set_keys=false)
4
+ ary = Tree.new dup
5
+ ary.set_keys! if set_keys
6
+ ary
7
+ end
8
+
9
+ protected
10
+ def set_keys!(i=-1)
11
+ each {|e| Array === e && e.unshift(i+=1).set_keys!}
12
+ end
13
+
14
+ end
15
+
16
+ module RMTools
17
+
18
+ class Tree < Array
19
+
20
+ def self.from(*obj) new(*obj) end
21
+
22
+ def initialize(obj, to_ary_method=nil)
23
+ super(to_ary_method ? recurse_build(obj, to_ary_method) : obj.to_a)
24
+ end
25
+
26
+ def recurse_build(obj, to_ary_method)
27
+ to_ary_method = [to_ary_method] unless Array === to_ary_method
28
+ if to_ary_method = to_ary_method.find {|m| obj.respond_to?(m) && Array === (ary = obj.send m) && ary.size > 0}
29
+ obj.send(to_ary_method).map {|branch|
30
+ [branch, recurse_build(branch, to_ary_method)]
31
+ }
32
+ else
33
+ [self]
34
+ end
35
+ end
36
+
37
+ def trace_leaf(a, c, cont=[])
38
+ return unless Array === a
39
+ return (a[0] == c && cont) if a.size == 1
40
+ y = nil
41
+ a[1..-1].each_with_index.find {|_, i|
42
+ y = trace_leaf(_, c, cont+[[a[0], i]])
43
+ }; y
44
+ end
45
+
46
+ def trace_branch(a, c, cont=[])
47
+ return unless Array === a
48
+ return cont if a[0] == c
49
+ y = nil
50
+ a[1..-1].each_with_index.find {|_, i|
51
+ y = trace_branch(_, c, cont+[[a[0], i]])
52
+ }; y
53
+ end
54
+
55
+ def leaves
56
+ y=[]
57
+ each {|_| Array === node && (node.size>1 ? y.concat(node.leaves) : y.concat(node))}
58
+ y
59
+ end
60
+
61
+ def get_branch_by_key(a, key, value)
62
+ return unless Array === a
63
+ return a if a[0][key] == value
64
+ y = nil
65
+ a[1..-1].find {|record| y = get_branch_by_key(record, key, value)}
66
+ y
67
+ end
68
+
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rmtools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Shinku Templar
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-10 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 49
30
+ segments:
31
+ - 0
32
+ - 8
33
+ - 7
34
+ version: 0.8.7
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 9
46
+ segments:
47
+ - 2
48
+ - 3
49
+ - 5
50
+ version: 2.3.5
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rubyforge
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 2
64
+ - 0
65
+ - 4
66
+ version: 2.0.4
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: gemcutter
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 11
78
+ segments:
79
+ - 0
80
+ - 5
81
+ - 0
82
+ version: 0.5.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: hoe
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 27
94
+ segments:
95
+ - 2
96
+ - 5
97
+ - 0
98
+ version: 2.5.0
99
+ type: :development
100
+ version_requirements: *id005
101
+ description: "== RMTools\n\
102
+ Methods for basic classes addon collection.\n\n\
103
+ == CHANGES\n\
104
+ :include: History.txt\n"
105
+ email:
106
+ - tinbka@gmail.com
107
+ executables: []
108
+
109
+ extensions:
110
+ - ext/extconf.rb
111
+ extra_rdoc_files:
112
+ - ./Manifest.txt
113
+ - ./License.txt
114
+ - ./README.txt
115
+ - ./History.txt
116
+ files:
117
+ - ext/extconf.rb
118
+ - ext/rmtools.h
119
+ - ext/rmtools.cpp
120
+ - lib/rmtools.rb
121
+ - lib/rmtools/arguments.rb
122
+ - lib/rmtools/numeric.rb
123
+ - lib/rmtools/string.rb
124
+ - lib/rmtools/io.rb
125
+ - lib/rmtools/js.rb
126
+ - lib/rmtools/cyrilic.rb
127
+ - lib/rmtools/coloring.rb
128
+ - lib/rmtools/stringscanner.rb
129
+ - lib/rmtools/enum.rb
130
+ - lib/rmtools/string_to_proc.rb
131
+ - lib/rmtools/traceback.rb
132
+ - lib/rmtools/time.rb
133
+ - lib/rmtools/random.rb
134
+ - lib/rmtools/binding.rb
135
+ - lib/rmtools/printing.rb
136
+ - lib/rmtools/range.rb
137
+ - lib/rmtools/tree.rb
138
+ - lib/rmtools/boolean.rb
139
+ - lib/rmtools/hash.rb
140
+ - lib/rmtools/logging.rb
141
+ - lib/rmtools/limited_string.rb
142
+ - lib/rmtools/module.rb
143
+ - lib/rmtools/object.rb
144
+ - lib/rmtools/array.rb
145
+ - lib/rmtools/setup.rb
146
+ - lib/rmtools/cyr-time.rb
147
+ - lib/rmtools/dumps.rb
148
+ - lib/rmtools/proc.rb
149
+ - ./Rakefile
150
+ - ./Manifest.txt
151
+ - ./License.txt
152
+ - ./README.txt
153
+ - ./History.txt
154
+ has_rdoc: true
155
+ homepage: http://github.com/tinbka
156
+ licenses: []
157
+
158
+ post_install_message:
159
+ rdoc_options:
160
+ - --main
161
+ - README.txt
162
+ require_paths:
163
+ - lib
164
+ - ext
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: 3
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ hash: 3
180
+ segments:
181
+ - 0
182
+ version: "0"
183
+ requirements: []
184
+
185
+ rubyforge_project: rmtools
186
+ rubygems_version: 1.3.7
187
+ signing_key:
188
+ specification_version: 3
189
+ summary: Yet another Ruby applied framework
190
+ test_files: []
191
+