epitools 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,78 +0,0 @@
1
- class String
2
-
3
- unless public_method_defined? :to_proc
4
-
5
- #
6
- # String#to_proc
7
- #
8
- # See http://weblog.raganwald.com/2007/10/stringtoproc.html
9
- #
10
- # Ported from the String Lambdas in Oliver Steele's Functional Javascript
11
- # http://osteele.com/sources/javascript/functional/
12
- #
13
- # This work is licensed under the MIT License:
14
- #
15
- # (c) 2007 Reginald Braithwaite
16
- # Portions Copyright (c) 2006 Oliver Steele
17
- #
18
- # Permission is hereby granted, free of charge, to any person obtaining
19
- # a copy of this software and associated documentation files (the
20
- # "Software"), to deal in the Software without restriction, including
21
- # without limitation the rights to use, copy, modify, merge, publish,
22
- # distribute, sublicense, and/or sell copies of the Software, and to
23
- # permit persons to whom the Software is furnished to do so, subject to
24
- # the following conditions:
25
- #
26
- # The above copyright notice and this permission notice shall be
27
- # included in all copies or substantial portions of the Software.
28
- #
29
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
- #
37
- def to_proc &block
38
- params = []
39
- expr = self
40
- sections = expr.split(/\s*->\s*/m)
41
- if sections.length > 1 then
42
- eval_block(sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block)
43
- elsif expr.match(/\b_\b/)
44
- eval_block("Proc.new { |_| #{expr} }", block)
45
- else
46
- leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
47
- rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
48
- if leftSection || rightSection then
49
- if (leftSection) then
50
- params.push('$left')
51
- expr = '$left' + expr
52
- end
53
- if (rightSection) then
54
- params.push('$right')
55
- expr = expr + '$right'
56
- end
57
- else
58
- self.gsub(
59
- /(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
60
- ).scan(
61
- /([a-z_$][a-z_$\d]*)/i
62
- ) do |v|
63
- params.push(v) unless params.include?(v)
64
- end
65
- end
66
- eval_block("Proc.new { |#{params.join(', ')}| #{expr} }", block)
67
- end
68
- end
69
-
70
- private
71
-
72
- def eval_block(code, block)
73
- eval code, block && block.binding
74
- end
75
-
76
- end # unless public_method_defined? :to_proc
77
-
78
- end