zucker 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +4 -0
  3. data/Rakefile +23 -0
  4. data/lib/zucker.rb +10 -0
  5. data/lib/zucker/1/D.rb +19 -0
  6. data/lib/zucker/1/aliases.rb +28 -0
  7. data/lib/zucker/1/all.rb +6 -0
  8. data/lib/zucker/1/array.rb +17 -0
  9. data/lib/zucker/1/array2proc.rb +10 -0
  10. data/lib/zucker/1/binding.rb +25 -0
  11. data/lib/zucker/1/blank.rb +26 -0
  12. data/lib/zucker/1/class2proc.rb +10 -0
  13. data/lib/zucker/1/debug.rb +4 -0
  14. data/lib/zucker/1/default.rb +25 -0
  15. data/lib/zucker/1/descriptions/D.yaml +25 -0
  16. data/lib/zucker/1/descriptions/aliases.yaml +18 -0
  17. data/lib/zucker/1/descriptions/array.yaml +16 -0
  18. data/lib/zucker/1/descriptions/array2proc.yaml +15 -0
  19. data/lib/zucker/1/descriptions/binding.yaml +12 -0
  20. data/lib/zucker/1/descriptions/blank.yaml +14 -0
  21. data/lib/zucker/1/descriptions/class2proc.yaml +15 -0
  22. data/lib/zucker/1/descriptions/egonil.yaml +20 -0
  23. data/lib/zucker/1/descriptions/enumerable.yaml +13 -0
  24. data/lib/zucker/1/descriptions/hash.yaml +14 -0
  25. data/lib/zucker/1/descriptions/hash2proc.yaml +13 -0
  26. data/lib/zucker/1/descriptions/iterate.yaml +19 -0
  27. data/lib/zucker/1/descriptions/ivars.yaml +22 -0
  28. data/lib/zucker/1/descriptions/kernel.yaml +13 -0
  29. data/lib/zucker/1/descriptions/mcopy.yaml +14 -0
  30. data/lib/zucker/1/descriptions/mm.yaml +14 -0
  31. data/lib/zucker/1/descriptions/regexp2proc.yaml +15 -0
  32. data/lib/zucker/1/descriptions/sandbox.yaml +16 -0
  33. data/lib/zucker/1/descriptions/square_brackets_for.yaml +25 -0
  34. data/lib/zucker/1/descriptions/string.yaml +15 -0
  35. data/lib/zucker/1/descriptions/unary.yaml +15 -0
  36. data/lib/zucker/1/descriptions/union.yaml +11 -0
  37. data/lib/zucker/1/documentation/meta.yaml +4 -0
  38. data/lib/zucker/1/documentation/zucker_doc.html +1158 -0
  39. data/lib/zucker/1/documentation/zucker_doc.rb +369 -0
  40. data/lib/zucker/1/egonil.rb +14 -0
  41. data/lib/zucker/1/enumerable.rb +12 -0
  42. data/lib/zucker/1/hash.rb +19 -0
  43. data/lib/zucker/1/hash2proc.rb +12 -0
  44. data/lib/zucker/1/iterate.rb +23 -0
  45. data/lib/zucker/1/ivars.rb +26 -0
  46. data/lib/zucker/1/kernel.rb +20 -0
  47. data/lib/zucker/1/mcopy.rb +9 -0
  48. data/lib/zucker/1/mm.rb +31 -0
  49. data/lib/zucker/1/regexp2proc.rb +10 -0
  50. data/lib/zucker/1/sandbox.rb +15 -0
  51. data/lib/zucker/1/specification/D_spec.rb +27 -0
  52. data/lib/zucker/1/specification/aliases_spec.rb +34 -0
  53. data/lib/zucker/1/specification/array2proc_spec.rb +8 -0
  54. data/lib/zucker/1/specification/array_spec.rb +23 -0
  55. data/lib/zucker/1/specification/binding_spec.rb +6 -0
  56. data/lib/zucker/1/specification/blank_spec.rb +20 -0
  57. data/lib/zucker/1/specification/class2proc_spec.rb +9 -0
  58. data/lib/zucker/1/specification/egonil_spec.rb +30 -0
  59. data/lib/zucker/1/specification/enumerable_spec.rb +8 -0
  60. data/lib/zucker/1/specification/hash2proc_spec.rb +12 -0
  61. data/lib/zucker/1/specification/hash_spec.rb +10 -0
  62. data/lib/zucker/1/specification/iterate_spec.rb +54 -0
  63. data/lib/zucker/1/specification/ivars_spec.rb +20 -0
  64. data/lib/zucker/1/specification/kernel_spec.rb +34 -0
  65. data/lib/zucker/1/specification/mcopy_spec.rb +13 -0
  66. data/lib/zucker/1/specification/mm_spec.rb +6 -0
  67. data/lib/zucker/1/specification/regexp2proc_spec.rb +9 -0
  68. data/lib/zucker/1/specification/sandbox_spec.rb +7 -0
  69. data/lib/zucker/1/specification/square_brackets_for_spec.rb +59 -0
  70. data/lib/zucker/1/specification/string_spec.rb +29 -0
  71. data/lib/zucker/1/specification/unary_spec.rb +26 -0
  72. data/lib/zucker/1/specification/union_spec.rb +13 -0
  73. data/lib/zucker/1/square_brackets_for.rb +20 -0
  74. data/lib/zucker/1/string.rb +25 -0
  75. data/lib/zucker/1/unary.rb +22 -0
  76. data/lib/zucker/1/union.rb +14 -0
  77. data/lib/zucker/all.rb +2 -0
  78. data/lib/zucker/debug.rb +2 -0
  79. data/lib/zucker/default.rb +2 -0
  80. data/lib/zucker/edge/ideas/args.rb +22 -0
  81. data/lib/zucker/edge/ideas/args.yaml +19 -0
  82. data/lib/zucker/edge/ideas/args_spec.rb +19 -0
  83. data/lib/zucker/edge/ideas/clone_deep.rb +21 -0
  84. data/lib/zucker/edge/ideas/clone_deep_spec.rb +12 -0
  85. data/lib/zucker/edge/ideas/dclone.yaml +12 -0
  86. data/lib/zucker/edge/ideas/hash.rb +18 -0
  87. data/lib/zucker/edge/ideas/require_directory.rb +6 -0
  88. metadata +153 -0
@@ -0,0 +1,369 @@
1
+ # Ruby 1.9, encoding: utf-8
2
+ require 'yaml'
3
+ require 'coderay'
4
+
5
+ class ZuckerDoc
6
+ DESCRIPTIONS = {
7
+ 'summary' => 'Summary',
8
+ 'why' => 'Why?',
9
+ 'methods' => 'Methods/Usage',
10
+ 'info' => 'Further information',
11
+ 'specification' => 'Specification',
12
+ 'source' => 'Source',
13
+ 'versions' => 'Compatibility',
14
+ 'authors' => 'Authors',
15
+ }
16
+ ORDER = %w|summary why methods info specification source versions|
17
+
18
+ # template vars
19
+ @version = 1
20
+
21
+ class << self
22
+ def generate
23
+
24
+
25
+ cubes = Dir['../descriptions/*'].inject({}) do |res, cube_file; a|
26
+ a = YAML.load_file cube_file
27
+ if a.instance_of? Hash
28
+ res.merge a
29
+ else
30
+ res
31
+ end
32
+ end
33
+
34
+ @meta = YAML.load_file 'meta.yaml'
35
+
36
+ output = 'zucker_doc.html'
37
+ result = DATA.read
38
+
39
+ cubes_html = cubes.map{ |name, hash|
40
+ cube name, hash
41
+ }.join
42
+
43
+ # insert cubes
44
+ result.sub! '....', cubes_html
45
+ # substitute vars
46
+ result.gsub! /\.\.([a-z]+)\.\./i do eval "@#$1" end
47
+ # code needs to be codish ;)
48
+ result.gsub! /~~([\w]+?)~~/i, '<code>\1</code>'
49
+ # highlight links
50
+ #...
51
+
52
+ File.open output, 'w' do |file|
53
+ file.puts result
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ def cube(name, hash)
60
+ @cube_name = name
61
+ %{ <h3 title="require 'zucker/..version../#{name}'">#{ name }</h3>
62
+ <table class="cube_table"
63
+ id="#{ name }_cube"
64
+ title="require 'zucker/..version../#{name}'"> } +
65
+
66
+ ORDER.map{ |th|
67
+ if th == 'specification' ||
68
+ th == 'source' ||
69
+ td = hash[th]
70
+
71
+ "<tr><th>#{ DESCRIPTIONS[th] }</th>" +
72
+ " <td>#{ send th, td }</td></tr>"
73
+ end
74
+ }.join +
75
+ '</table>'
76
+ end
77
+
78
+ def methods(m)
79
+ m.map{ |name, usage|
80
+ "<h5>#{name}</h5>" +
81
+ "<pre class=\"usage source\" style=\"display:block\">#{ syntax_highlight usage }</pre>"
82
+ }.join
83
+ end
84
+
85
+ def info(i)
86
+ convert_html_chars i.join '<br/>'
87
+ end
88
+
89
+ def authors(a)
90
+ a.map{ |author|
91
+ author +
92
+ ( (tmp = @meta[author]) ? " | #{ tmp }" : '' )
93
+ }.join '<br/>'
94
+ end
95
+
96
+ def why(w)
97
+ if w.is_a? Array
98
+ w = w.map{|e| "<p>#{e}</p>"}.join
99
+ end
100
+ convert_html_chars w
101
+ end
102
+
103
+ def summary(s)
104
+ convert_html_chars s
105
+ end
106
+
107
+ def versions(v)
108
+ v.join ', '
109
+ end
110
+
111
+ def specification(s)
112
+ source_helper(:specification, '../specification/', '_spec')
113
+ end
114
+
115
+ def source(s)
116
+ source_helper(:source, '../')
117
+ end
118
+
119
+ def source_helper(kind, file_prefix, suffix='')
120
+ %{ <span id="show_#{@cube_name}_#{kind}">(<a href="javascript:show('#{@cube_name}_#{kind}')">show</a>)</span>
121
+ <pre class="source" id="#{@cube_name}_#{kind}">#{
122
+ get_source_file( file_prefix + @cube_name + suffix + '.rb' )
123
+ }</pre> }
124
+ end
125
+
126
+ def get_source_file(filename)
127
+ if File.file? filename
128
+ syntax_highlight( File.read(filename).strip )
129
+ else
130
+ '<em>FIXME: missing</em>'
131
+ end
132
+ end
133
+
134
+ def syntax_highlight(string)
135
+ #convert_html_chars
136
+ CodeRay.scan(string, :ruby).html
137
+ end
138
+
139
+ def convert_html_chars(string, protect_spaces = false)
140
+ string = string.to_s.
141
+ gsub( "\n", '<br/>' ).
142
+ gsub( /⇧(.+?)⇧/, '<code>\1</code>' ).
143
+ gsub( /●(.+?)●/, '<strong>\1</strong>' )
144
+
145
+ if protect_spaces
146
+ string.gsub(' ',' ')
147
+ else
148
+ string
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ ZuckerDoc.generate
155
+
156
+ __END__
157
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
158
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
159
+
160
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
161
+ <head>
162
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
163
+
164
+ <title>Ruby Zucker ..version..</title>
165
+ <script type="text/javascript">
166
+ var show = function(snippet){
167
+ document.getElementById( snippet ).style.display = 'block'
168
+ if( document.getElementById( 'show_'+snippet ) ){
169
+ document.getElementById( 'show_'+snippet ).style.display = 'none'
170
+ }
171
+ }
172
+ </script>
173
+ <style type="text/css">
174
+ body{
175
+ background-color:#aaa;
176
+ color:#111;
177
+ font-family:sans-serif;
178
+ }
179
+
180
+ #world{
181
+ background:#fff;
182
+ }
183
+
184
+ h1, h2, h3, h4, h5, h6{
185
+ margin:0px;
186
+ padding:0px;
187
+ }
188
+
189
+ h1{
190
+ color:#222;
191
+ text-align:center;
192
+ padding:0.5em;
193
+ }
194
+ h2{
195
+ margin-left:0.5em;
196
+ margin-top:0.8em;
197
+ margin-bottom:0.5em;
198
+ }
199
+
200
+ th{
201
+ width:200px;
202
+ color:#444;
203
+ }
204
+
205
+ p{
206
+ margin:0px;
207
+ margin-bottom:0.5em;
208
+ }
209
+
210
+ p.text{
211
+ margin-left:1.5em;
212
+ margin-right:1em;
213
+ }
214
+
215
+ code{
216
+ }
217
+
218
+ .scode{
219
+ display:block;
220
+ margin:0.8em;
221
+ # margin-right:1.8em;
222
+ padding:0.5em;
223
+ border:1px solid black;
224
+ background:#eee;
225
+ }
226
+
227
+ #.cube_table{
228
+ # display:none;
229
+ #}
230
+ .cubes{
231
+ margin:0px;
232
+ margin-left:1.5em;
233
+ margin-right:1em;
234
+ }
235
+
236
+ a{
237
+ color:#111;
238
+ }
239
+
240
+ table{
241
+ margin:0.8em;
242
+ margin-top:0.2em;
243
+ padding:0.2em;
244
+ border:1px solid #111;
245
+ background:#eee;
246
+ overflow:auto;
247
+ display:block;
248
+ }
249
+
250
+ th{
251
+ text-align:left;
252
+ vertical-align:top;
253
+ padding-right:3em;
254
+ }
255
+
256
+ td{
257
+ width:100%;
258
+ }
259
+
260
+ li{
261
+ list-style:none;
262
+ }
263
+
264
+
265
+ #foot{
266
+ text-align:left;
267
+ padding:0.3em;
268
+ font-size:70%
269
+ }
270
+ #foot, #foot a{
271
+ color:#444;
272
+ }
273
+ #smile{
274
+ font-size:150%;
275
+ float:right;
276
+ }
277
+ #smile a{
278
+ text-decoration:none;
279
+ }
280
+
281
+ .small{
282
+ font-size:70%;
283
+ }
284
+
285
+ code, pre{
286
+ font-face:mono;
287
+ margin:0px;
288
+ padding:0px;
289
+ }
290
+
291
+ .source{
292
+ display:none;
293
+ border:1px solid #005;
294
+ background:#111;
295
+ padding:5px;
296
+ width:98%;
297
+
298
+ background-color: #232323;
299
+ # border: 1px solid black;
300
+ font-family: 'Courier New', 'Terminal', monospace;
301
+ color: #E6E0DB;
302
+ padding: 3px 5px;
303
+ # margin-right:1em;
304
+ overflow: auto;
305
+ font-size: 12px;
306
+ }
307
+
308
+ /*railscasts*/
309
+ .source .an { color:#E7BE69 } /* html attribute */
310
+ .source .c { color:#BC9358; font-style: italic; } /* comment */
311
+ .source .ch { color:#509E4F } /* escaped character */
312
+ .source .cl { color:#FFF } /* class */
313
+ .source .co { color:#FFF } /* constant */
314
+ .source .fl { color:#A4C260 } /* float */
315
+ .source .fu { color:#FFC56D } /* function */
316
+ .source .gv { color:#D0CFFE } /* global variable */
317
+ .source .i { color:#A4C260 } /* integer */
318
+ .source .il { background:#151515 } /* inline code */
319
+ .source .iv { color:#D0CFFE } /* instance variable */
320
+ .source .pp { color:#E7BE69 } /* doctype */
321
+ .source .r { color:#CB7832 } /* keyword */
322
+ .source .rx { color:#A4C260 } /* regex */
323
+ .source .s { color:#A4C260 } /* string */
324
+ .source .sy { color:#6C9CBD } /* symbol */
325
+ .source .ta { color:#E7BE69 } /* html tag */
326
+ .source .pc { color:#6C9CBD } /* boolean */
327
+
328
+ </style>
329
+ </head>
330
+ <body>
331
+ <div id="world">
332
+
333
+ <h1>Ruby Zucker ..version..</h1>
334
+ <h2>What is it?</h2>
335
+ <p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>). This gem adds syntactical sugar in the form of independent, lightweight scripts that make Ruby even more beautiful. Read <a href="http://rbjl.net/32-">this blog post</a> for a little introduction!</p>
336
+
337
+ <h2>Install</h2>
338
+ <p class="text">
339
+ <code class="scode">gem install zucker # might need sudo</code>
340
+ </p>
341
+
342
+ <h2>Usage / Organisation</h2>
343
+ <p class="text">The gem consists of many small snippets, called <em>cubes</em>, which are bundled in <em>packages</em>. Currently, there are two packages available: <strong>default</strong> and <strong>debug</strong>. You can use a package be requiring it in this way:
344
+ <code class="scode">require 'zucker/default'</code>
345
+ and
346
+ <code class="scode">require 'zucker/debug'</code>
347
+
348
+ Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
349
+
350
+ <code class="scode">require 'zucker/1/egonil'</code>
351
+ </p>
352
+ <p class="text"><em>Please note:</em> To cherry-pick cubes, you have to allude to the gem version you want to use. Future releases of the gem will include all previous versions, so the behaviour of directly required cubes will never change (except for critical bugs).</p>
353
+
354
+ <h2 title="require 'zucker/all'">Cubes</h2>
355
+ <div class="cubes">
356
+ ....
357
+ </div>
358
+ <br/>
359
+ </div>
360
+ <div id="foot">
361
+ <div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
362
+ This is the Ruby Zucker ..version.. documentation.
363
+ The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
364
+ Gem source at <a href="http://github.com/janlelis/zucker">github</a>.
365
+ </div>
366
+
367
+ </body>
368
+ </html>
369
+
@@ -0,0 +1,14 @@
1
+ def egonil(nil_value = nil)
2
+ yield
3
+ rescue NoMethodError => e
4
+ if e.message =~ /NilClass$/
5
+ nil_value
6
+ else
7
+ raise NoMethodError
8
+ end
9
+ end
10
+
11
+ alias nn egonil
12
+
13
+ # J-_-L
14
+
@@ -0,0 +1,12 @@
1
+ module Enumerable
2
+ def mash
3
+ ret = {}
4
+ each{ |kv|
5
+ ret.store *( yield(kv)[0,2] )
6
+ }
7
+ ret
8
+ end
9
+ end
10
+
11
+ # J-_-L
12
+
@@ -0,0 +1,19 @@
1
+ class Hash
2
+ def self.zip(keys,values)
3
+ Hash[ *[1,2,3].zip( [4,5,6] ).flatten ]
4
+ end
5
+
6
+ def <<(other)
7
+ case
8
+ when other.is_a?(Hash)
9
+ merge! other
10
+ when other.respond_to?(:to_splat)
11
+ merge! Hash[*other]
12
+ else
13
+ raise TypeError, 'can only append other Hashs and Enumerables'
14
+ end
15
+ end
16
+ end
17
+
18
+ # J-_-L
19
+
@@ -0,0 +1,12 @@
1
+ class Hash
2
+ def to_proc
3
+ Proc.new{ |obj|
4
+ if self.member? obj
5
+ self[obj].to_proc.call obj
6
+ else
7
+ obj
8
+ end
9
+ }
10
+ end
11
+ end
12
+
@@ -0,0 +1,23 @@
1
+ def iterate(*params)
2
+ # params.shift.zip(*params).each{ |*elements| yield *elements }
3
+
4
+ first = params.shift
5
+ if params.empty? # single param - like each
6
+ if block_given?
7
+ first.map{|e| yield e }
8
+ else
9
+ first.map
10
+ end
11
+ else
12
+ padded_first = Array.new( [first, *params].max_by(&:size).size ){|i| first[i] } # append nils
13
+ obj = padded_first.zip *params
14
+ if block_given?
15
+ obj.map{|es| yield *es }
16
+ else
17
+ obj.map.to_enum
18
+ end
19
+ end
20
+ end
21
+
22
+ # J-_-L
23
+