zombify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/Manifest +5 -0
  2. data/README.rdoc +0 -0
  3. data/Rakefile +14 -0
  4. data/lib/rand.rb +412 -0
  5. data/lib/zombify.rb +102 -0
  6. data/zombify.gemspec +30 -0
  7. metadata +67 -0
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ lib/rand.rb
5
+ lib/zombify.rb
File without changes
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('zombify', '0.1.0') do |p|
6
+ p.description = "Zombifies strings in your application."
7
+ p.url = "http://github.com/romaind/zombify"
8
+ p.author = "Studio Melipone"
9
+ p.email = "contact@studiomelipone.eu"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,412 @@
1
+ #
2
+ # = rand.rb -- library for picking random elements and shuffling
3
+ #
4
+ # Copyright (C) 2004 Ilmari Heikkinen <mailto:kig@misfiring.net>
5
+ #
6
+ # Documentation:: Christian Neukirchen <mailto:chneukirchen@gmail.com>
7
+ #
8
+
9
+
10
+ module Enumerable
11
+ # Choose and return a random element of the Enumerable.
12
+ # [1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
13
+ def pick
14
+ entries.pick
15
+ end
16
+
17
+ # Return an array of the elements in random order.
18
+ # [1, 2, 3, 4].shuffle #=> [3, 4, 1, 2]
19
+ def shuffle
20
+ entries.shuffle
21
+ end
22
+
23
+ # Calls _block_ once for each element in _self_ in random order,
24
+ # passing that element as a parameter.
25
+ def each_random(&block)
26
+ shuffle.each(&block)
27
+ end
28
+
29
+ # Invokes _block_ once for each element of _self_ in random order.
30
+ # Creates a new array containing the values returned by the block.
31
+ def map_random(&block)
32
+ shuffle.map(&block)
33
+ end
34
+ end
35
+
36
+
37
+ class Array
38
+ # Choose and return a random element of _self_.
39
+ # [1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
40
+ def pick
41
+ self[pick_index]
42
+ end
43
+
44
+ # Deletes a random element of _self_, returning that element.
45
+ # a = [1, 2, 3, 4]
46
+ # a.pick #=> 2
47
+ # a #=> [1, 3, 4]
48
+ def pick!
49
+ i = pick_index
50
+ rv = self[i]
51
+ delete_at(i)
52
+ rv
53
+ end
54
+
55
+ # Return the index of an random element of _self_.
56
+ # ["foo", "bar", "baz"].pick_index #=> 1 (or 0, or 2)
57
+ def pick_index
58
+ Kernel.rand(size)
59
+ end
60
+
61
+ # Destructive pick_index. Delete a random element of _self_ and
62
+ # return its index.
63
+ # a = [11, 22, 33, 44]
64
+ # a.pick_index! #=> 2
65
+ # a #=> [11, 22, 44]
66
+ def pick_index!
67
+ i = pick_index
68
+ delete_at i
69
+ i
70
+ end
71
+
72
+ # Return an array of the elements in random order.
73
+ # [11, 22, 33, 44].shuffle #=> [33, 11, 44, 22]
74
+ def shuffle
75
+ sort_by{rand}
76
+ end
77
+
78
+ # Destructive shuffle. Arrange the elements of _self_ in new order.
79
+ # a = [11, 22, 33, 44]
80
+ # a.shuffle!
81
+ # a #=> [33, 11, 44, 22]
82
+ def shuffle!
83
+ sort!{rand <=> 0.5}
84
+ end
85
+ end
86
+
87
+
88
+ class Hash
89
+ # Choose and return a random key-value pair of _self_.
90
+ # {:one => 1, :two => 2, :three => 3}.pick #=> [:one, 1]
91
+ def pick
92
+ k = keys.pick
93
+ [k, self[k]]
94
+ end
95
+
96
+ # Deletes a random key-value pair of _self_, returning that pair.
97
+ # a = {:one => 1, :two => 2, :three => 3}
98
+ # a.pick #=> [:two, 2]
99
+ # a #=> {:one => 1, :three => 3}
100
+ def pick!
101
+ rv = pick
102
+ delete rv.first
103
+ rv
104
+ end
105
+
106
+ # Return a random key of _self_.
107
+ # {:one => 1, :two => 2, :three => 3}.pick_key #=> :three
108
+ def pick_key
109
+ keys.pick
110
+ end
111
+
112
+ # Return a random value of _self_.
113
+ # {:one => 1, :two => 2, :three => 3}.pick_value #=> 3
114
+ def pick_value
115
+ values.pick
116
+ end
117
+
118
+ # Delete a random key-value pair of _self_ and return the key.
119
+ # a = {:one => 1, :two => 2, :three => 3}
120
+ # a.pick_key! #=> :two
121
+ # a #=> {:one => 1, :three => 3}
122
+ def pick_key!
123
+ pick!.first
124
+ end
125
+
126
+ # Delete a random key-value pair of _self_ and return the value.
127
+ # a = {:one => 1, :two => 2, :three => 3}
128
+ # a.pick_value! #=> 2
129
+ # a #=> {:one => 1, :three => 3}
130
+ def pick_value!
131
+ pick!.last
132
+ end
133
+
134
+ # Return the key-value pairs of _self_ with _keys_ and _values_
135
+ # shuffled independedly.
136
+ # {:one => 1, :two => 2, :three => 3}.shuffle_hash_pairs
137
+ # #=> [[:one, 3], [:two, 1], [:three, 2]]
138
+ def shuffle_hash_pairs
139
+ keys.shuffle.zip(values.shuffle)
140
+ end
141
+
142
+ # Return a copy of _self_ with _values_ arranged in random order.
143
+ # {:one => 1, :two => 2, :three => 3}.shuffle_hash
144
+ # #=> {:two=>2, :three=>1, :one=>3}
145
+ def shuffle_hash
146
+ shuffled = {}
147
+ shuffle_hash_pairs.each{|k, v|
148
+ shuffled[k] = v
149
+ }
150
+ shuffled
151
+ end
152
+
153
+ # Destructive shuffle_hash. Arrange the values of _self_ in
154
+ # new, random order.
155
+ # h = {:one => 1, :two => 2, :three => 3}
156
+ # h.shuffle_hash!
157
+ # h #=> {:two=>2, :three=>1, :one=>3}
158
+ def shuffle_hash!
159
+ shuffle_hash_pairs.each{|k, v|
160
+ self[k] = v
161
+ }
162
+ self
163
+ end
164
+ end
165
+
166
+
167
+ class String
168
+ # Return the string with characters arranged in random order.
169
+ # "Ruby rules".shuffle_chars #=> "e lybRsuur"
170
+ def shuffle_chars
171
+ split(//).shuffle.join('')
172
+ end
173
+
174
+ # Destructive shuffle_chars. Arrange the characters of the string
175
+ # in new, random order.
176
+ # s = "Ruby rules".shuffle_chars
177
+ # s.shuffle_chars!
178
+ # s #=> "e lybRsuur"
179
+ def shuffle_chars!
180
+ self[0,size] = shuffle_chars
181
+ end
182
+
183
+ # Return a random byte of _self_.
184
+ # "Ruby rules".pick_byte #=> 121
185
+ def pick_byte
186
+ self[pick_index]
187
+ end
188
+
189
+ # Return a single-character string of a random character in _self_.
190
+ # "Ruby rules".pick_char #=> "y"
191
+ def pick_char
192
+ pick_byte.chr
193
+ end
194
+
195
+ # Destructive pick_char. Delete a random character of the string
196
+ # and return it as a single-character string.
197
+ # s = "Ruby rules"
198
+ # s.pick_char! #=> "y"
199
+ # s #=> "Rub rules"
200
+ def pick_char!
201
+ i = pick_index
202
+ rv = self[i,1]
203
+ self[i,1] = ""
204
+ rv
205
+ end
206
+
207
+ # Destructive pick_byte. Delete a random byte of _self_ and return it.
208
+ # s = "Ruby rules"
209
+ # s.pick_byte! #=> 121
210
+ # s #=> "Rub rules"
211
+ def pick_byte!
212
+ pick_char![0]
213
+ end
214
+
215
+ # Return a random byte index of _self_.
216
+ # "Ruby rules".pick_index #=> 3
217
+ def pick_index
218
+ rand(size)
219
+ end
220
+
221
+ # Destructive pick_index. Delete a random byte of _self_ and
222
+ # return it's index.
223
+ # s = "Ruby rules"
224
+ # s.pick_index #=> 3
225
+ # s #=> "Rub rules"
226
+ def pick_index!
227
+ i = pick_index
228
+ self[i,1] = ""
229
+ i
230
+ end
231
+ end
232
+
233
+
234
+ if __FILE__ == $0
235
+
236
+ require 'test/unit'
237
+
238
+ module RandTestHelpers # :nodoc:
239
+ def picker(enum, enum_check, method, n=50)
240
+ (1..n).all?{ enum_check.include? enum.send(method) }
241
+ end
242
+
243
+ def try_shuffling(enum, enum_c, method)
244
+ rv = nil
245
+ 10.times{
246
+ rv = enum.send method
247
+ break if rv != enum_c
248
+ }
249
+ rv
250
+ end
251
+ end
252
+
253
+
254
+ class RandArrayTest < Test::Unit::TestCase # :nodoc:
255
+ include RandTestHelpers
256
+
257
+ def ar
258
+ (0..99).to_a
259
+ end
260
+
261
+ def test_pick
262
+ a = ar
263
+ results = (0...a.size).map{ a.pick }
264
+ assert true, results.all? {|r| a.include? r }
265
+ end
266
+
267
+ def test_pick!
268
+ a = ar
269
+ results = (0...a.size).map{ a.pick! }
270
+ assert true, results.sort == (0..99).to_a and a.empty?
271
+ end
272
+
273
+ def test_pick_index
274
+ a = ar
275
+ results = (0...a.size).map{ a.pick_index }
276
+ assert true, results.all? {|r| r.between?(0, a.size-1) }
277
+ end
278
+
279
+ def test_pick_index!
280
+ a = ar
281
+ # side-effect-relying block; a.size = a.size-1 after pick_index!,
282
+ # so the picked index max value is the new a.size
283
+ assert true, (0...a.size).all?{ a.pick_index!.between?(0, a.size) } and a.empty?
284
+ end
285
+
286
+ def test_shuffle
287
+ a = ar
288
+ shuffled = try_shuffling(a, a, :shuffle)
289
+ assert true, shuffled.sort == a and shuffled != a
290
+ end
291
+
292
+ def test_shuffle!
293
+ a = ar
294
+ try_shuffling(a, ar, :shuffle!)
295
+ assert true, a != ar and a.sort == ar
296
+ end
297
+ end
298
+
299
+
300
+ class RandHashTest < Test::Unit::TestCase # :nodoc:
301
+ include RandTestHelpers
302
+
303
+ def ha
304
+ Hash[*(1..100).to_a]
305
+ end
306
+
307
+ def test_pick
308
+ assert true, picker(ha, ha.entries, :pick)
309
+ end
310
+
311
+ def test_pick!
312
+ h = ha
313
+ assert true, picker(h, ha.entries, :pick!) and h.empty?
314
+ end
315
+
316
+ def test_pick_key
317
+ assert true, picker(ha, ha.keys, :pick_key)
318
+ end
319
+
320
+ def test_pick_key!
321
+ h = ha
322
+ assert true, picker(h, ha.keys, :pick_key!) and h.empty?
323
+ end
324
+
325
+ def test_pick_value
326
+ assert true, picker(ha, ha.values, :pick_value)
327
+ end
328
+
329
+ def test_pick_value!
330
+ h = ha
331
+ assert true, picker(h, ha.values, :pick_value!) and h.empty?
332
+ end
333
+
334
+ def test_shuffle_hash
335
+ h = ha
336
+ hs = try_shuffling(ha, h, :shuffle_hash)
337
+ assert true, hs != h and (hs.keys + hs.values).sort == (h.keys + h.values).sort
338
+ end
339
+
340
+ def test_shuffle_hash!
341
+ h = ha
342
+ hs = ha
343
+ try_shuffling(hs, h, :shuffle_hash!)
344
+ assert true, hs != h and (hs.keys + hs.values).sort == (h.keys + h.values).sort
345
+ end
346
+
347
+ def test_shuffle
348
+ h = ha
349
+ hs = try_shuffling(ha, h, :shuffle)
350
+ assert true, hs != h and hs.entries.sort == h.entries.sort
351
+ end
352
+ end
353
+
354
+
355
+ class RandStringTest < Test::Unit::TestCase # :nodoc:
356
+ include RandTestHelpers
357
+
358
+ def self.pick_tests(endings)
359
+ endings.each{|ending, compare_str_f|
360
+ define_method("test_pick#{ending}"){
361
+ s = str
362
+ assert true, picker(s, instance_eval(&compare_str_f), "pick#{ending}", s.size)
363
+ }
364
+ }
365
+ end
366
+
367
+ def self.pick_tests!(endings)
368
+ endings.each{|ending, compare_str_f|
369
+ define_method("test_pick#{ending}!"){
370
+ s = str
371
+ assert true, picker(s, instance_eval(&compare_str_f), "pick#{ending}!", s.size) and s.empty?
372
+ }
373
+ }
374
+ end
375
+
376
+ def str
377
+ (("a".."z").to_s + "\n") * 10
378
+ end
379
+
380
+ def test_shuffle
381
+ s = str
382
+ ss = try_shuffling(s, s.to_a, :shuffle)
383
+ assert true, ss != s.to_a and ss.sort == s.to_a.sort
384
+ end
385
+
386
+ def test_shuffle_chars
387
+ s = str
388
+ ss = try_shuffling(s, s.split(//), :shuffle_chars)
389
+ assert true, ss != s and ss.split(//).sort == s.split(//).sort
390
+ end
391
+
392
+ def test_shuffle_chars!
393
+ s = str
394
+ ss = str
395
+ try_shuffling(ss, s.split(//), :shuffle_chars!)
396
+ assert true, ss != s and ss.split(//).sort == s.split(//).sort
397
+ end
398
+
399
+ pick_tests({ "" => lambda{str.to_a},
400
+ :_char => lambda{str.split(//)},
401
+ :_byte => lambda{str.split(//).map{|c| c[0]}},
402
+ :_index => lambda{(0...str.size).to_a}
403
+ })
404
+
405
+ pick_tests!({ :_char => lambda{str.split(//)},
406
+ :_byte => lambda{str.split(//).map{|c| c[0]}},
407
+ :_index => lambda{(0...str.size).to_a}
408
+ })
409
+ end
410
+
411
+
412
+ end #if
@@ -0,0 +1,102 @@
1
+ # module Zombify
2
+ # def self.included(base)
3
+ # base.extend
4
+ # end
5
+ #
6
+ # module String
7
+ # def zombify(*args, &block)
8
+ #
9
+ #
10
+ #
11
+ # end
12
+ # end
13
+ #
14
+ # module Zombify
15
+ #
16
+ # end
17
+ # end
18
+ #
19
+ # class ActiveRecord::Base
20
+ # include Zombify
21
+ # end
22
+
23
+ require "rand.rb"
24
+
25
+ module Zombify
26
+ def self.vocabulary
27
+ [
28
+ { :word => "braiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiin", :real? => true },
29
+ { :word => "eaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat", :real? => true },
30
+ { :word => "AahfaheuahAgAkajafaaaghahehbgrahaaaeuuuha", :real? => false },
31
+ { :word => "auhueeeeeaaaAAeeuhahhggakaaa", :real? => false },
32
+ { :word => "rrsshhchhhaaaaachshhh", :real? => false },
33
+ { :word => "hhhhhhhheeaaaaahhaaschhhh", :real? => false }
34
+ ]
35
+ end
36
+
37
+ def self.talk(word_count = 30, min_length = 2, max_length = 13)
38
+ content = ""
39
+
40
+ word_count.times do
41
+ letter_count = (min_length == max_length) ? min_length : (Kernel.rand(max_length) % (max_length - min_length)) + min_length
42
+ word = Zombify::vocabulary.pick
43
+
44
+ if word[:real?]
45
+
46
+ content += word[:word][0,letter_count-1] + word[:word][-1,1] + " "
47
+ else
48
+ begin_letter = Kernel.rand(word[:word].size - letter_count)
49
+
50
+ content += word[:word][begin_letter, letter_count] + " "
51
+ end
52
+ end
53
+
54
+ content.strip
55
+ end
56
+
57
+ def self.undeadize(word, percent_change_letter = 50)
58
+ number_letter_change = word.size * percent_change_letter / 100
59
+ STDERR.puts number_letter_change
60
+
61
+ content = word.dup
62
+
63
+ number_letter_change.times do
64
+ index = Kernel.rand(content.size)
65
+ a = Zombify::alphabet.pick
66
+ STDERR.puts "##{content}: #{index} -> #{a}"
67
+
68
+ content[index] = a
69
+ end
70
+
71
+ content
72
+ end
73
+
74
+ def self.alphabet
75
+ %w(a e h u g k s r A E H U G K S R)
76
+ end
77
+
78
+ module String
79
+
80
+ module ZombifyHelper
81
+
82
+ def zombify(real_word = 40)
83
+ content = self.dup
84
+
85
+ self.scan(/\w+/) do |word|
86
+ if Kernel.rand(100) < real_word
87
+ m = Zombify::undeadize(word)
88
+ STDERR.puts m
89
+ content.gsub!(word, m)
90
+ else
91
+ content.gsub!(word, Zombify::talk(1, word.length, word.length))
92
+ end
93
+ end
94
+ content
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ class String
101
+ include Zombify::String::ZombifyHelper
102
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{zombify}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Studio Melipone"]
9
+ s.date = %q{2009-11-13}
10
+ s.description = %q{Zombifies strings in your application.}
11
+ s.email = %q{contact@studiomelipone.eu}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/rand.rb", "lib/zombify.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/rand.rb", "lib/zombify.rb", "zombify.gemspec"]
14
+ s.homepage = %q{http://github.com/romaind/zombify}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zombify", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{zombify}
18
+ s.rubygems_version = %q{1.3.4}
19
+ s.summary = %q{Zombifies strings in your application.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zombify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Studio Melipone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Zombifies strings in your application.
17
+ email: contact@studiomelipone.eu
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/rand.rb
25
+ - lib/zombify.rb
26
+ files:
27
+ - Manifest
28
+ - README.rdoc
29
+ - Rakefile
30
+ - lib/rand.rb
31
+ - lib/zombify.rb
32
+ - zombify.gemspec
33
+ has_rdoc: true
34
+ homepage: http://github.com/romaind/zombify
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --line-numbers
40
+ - --inline-source
41
+ - --title
42
+ - Zombify
43
+ - --main
44
+ - README.rdoc
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "1.2"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: zombify
62
+ rubygems_version: 1.3.4
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Zombifies strings in your application.
66
+ test_files: []
67
+