zucker 13.1 → 100.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +17 -2
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +22 -0
  5. data/Rakefile +17 -58
  6. data/doc/{zucker_doc.rb → create_documentation.rb} +67 -129
  7. data/doc/zucker.html +1774 -0
  8. data/lib/zucker.rb +12 -42
  9. data/lib/zucker/alias_for.rb +9 -16
  10. data/lib/zucker/all.rb +0 -2
  11. data/lib/zucker/array_op.rb +15 -0
  12. data/lib/zucker/blank.rb +39 -21
  13. data/lib/zucker/camel_snake.rb +15 -0
  14. data/lib/zucker/constantize.rb +29 -0
  15. data/lib/zucker/dir_extras.rb +20 -0
  16. data/lib/zucker/egonil.rb +13 -24
  17. data/lib/zucker/file_extras.rb +33 -0
  18. data/lib/zucker/hash_extras.rb +11 -0
  19. data/lib/zucker/hash_op.rb +28 -0
  20. data/lib/zucker/instance_variables_from.rb +34 -0
  21. data/lib/zucker/iterate.rb +24 -21
  22. data/lib/zucker/marshal_copy.rb +11 -0
  23. data/lib/zucker/mash.rb +22 -0
  24. data/lib/zucker/ords.rb +17 -0
  25. data/lib/zucker/regexp_union.rb +17 -0
  26. data/lib/zucker/square_brackets_for.rb +12 -15
  27. data/lib/zucker/string_extras.rb +15 -0
  28. data/lib/zucker/string_op.rb +20 -0
  29. data/lib/zucker/tap.rb +13 -8
  30. data/lib/zucker/unary_conversion.rb +25 -0
  31. data/spec/alias_for_spec.rb +11 -73
  32. data/spec/array_op_spec.rb +17 -0
  33. data/spec/blank_spec.rb +2 -0
  34. data/spec/camel_snake_spec.rb +15 -0
  35. data/spec/constantize_spec.rb +33 -0
  36. data/spec/dir_extras_spec.rb +32 -0
  37. data/spec/egonil_spec.rb +1 -0
  38. data/spec/file_extras_spec.rb +57 -0
  39. data/spec/hash_extras_spec.rb +11 -0
  40. data/spec/hash_op_spec.rb +28 -0
  41. data/spec/instance_variables_from_spec.rb +24 -0
  42. data/spec/iterate_spec.rb +2 -0
  43. data/spec/{mcopy_spec.rb → marshal_copy_spec.rb} +5 -3
  44. data/spec/mash_spec.rb +17 -0
  45. data/spec/ords_spec.rb +15 -0
  46. data/spec/{union_spec.rb → regexp_union_spec.rb} +4 -2
  47. data/spec/square_brackets_for_spec.rb +4 -12
  48. data/spec/string_extras_spec.rb +20 -0
  49. data/spec/string_op_spec.rb +22 -0
  50. data/spec/tap_spec.rb +2 -0
  51. data/spec/{unary_spec.rb → unary_conversion_spec.rb} +4 -2
  52. data/zucker.gemspec +8 -18
  53. metadata +75 -147
  54. data/.gemtest +0 -0
  55. data/README +0 -7
  56. data/desc/alias_for.yaml +0 -17
  57. data/desc/aliases.yaml +0 -18
  58. data/desc/array.yaml +0 -11
  59. data/desc/array_to_proc.yaml +0 -16
  60. data/desc/binding.yaml +0 -22
  61. data/desc/blank.yaml +0 -11
  62. data/desc/case.yaml +0 -9
  63. data/desc/cc.yaml +0 -10
  64. data/desc/class_to_proc.yaml +0 -11
  65. data/desc/dd.yaml +0 -23
  66. data/desc/egonil.yaml +0 -15
  67. data/desc/engine.yaml +0 -15
  68. data/desc/enumerable.yaml +0 -10
  69. data/desc/file.yaml +0 -14
  70. data/desc/hash.yaml +0 -14
  71. data/desc/hash_to_proc.yaml +0 -10
  72. data/desc/info.yaml +0 -20
  73. data/desc/iterate.yaml +0 -16
  74. data/desc/ivars.yaml +0 -19
  75. data/desc/kernel.yaml +0 -15
  76. data/desc/mcopy.yaml +0 -11
  77. data/desc/mm.yaml +0 -15
  78. data/desc/not.yaml +0 -11
  79. data/desc/oo.yaml +0 -10
  80. data/desc/os.yaml +0 -19
  81. data/desc/qq.yaml +0 -11
  82. data/desc/regexp_to_proc.yaml +0 -12
  83. data/desc/regexp_visualize.yaml +0 -23
  84. data/desc/ruby_version.yaml +0 -28
  85. data/desc/sandbox.yaml +0 -13
  86. data/desc/square_brackets_for.yaml +0 -22
  87. data/desc/string.yaml +0 -22
  88. data/desc/tap.yaml +0 -17
  89. data/desc/unary.yaml +0 -15
  90. data/desc/union.yaml +0 -10
  91. data/doc/zucker_doc.html +0 -3042
  92. data/lib/zucker/aliases.rb +0 -52
  93. data/lib/zucker/array.rb +0 -15
  94. data/lib/zucker/array_to_proc.rb +0 -19
  95. data/lib/zucker/binding.rb +0 -35
  96. data/lib/zucker/case.rb +0 -14
  97. data/lib/zucker/cc.rb +0 -31
  98. data/lib/zucker/class_to_proc.rb +0 -13
  99. data/lib/zucker/control.rb +0 -4
  100. data/lib/zucker/dd.rb +0 -24
  101. data/lib/zucker/debug.rb +0 -4
  102. data/lib/zucker/default.rb +0 -4
  103. data/lib/zucker/engine.rb +0 -70
  104. data/lib/zucker/enumerable.rb +0 -15
  105. data/lib/zucker/env.rb +0 -4
  106. data/lib/zucker/extensions.rb +0 -4
  107. data/lib/zucker/file.rb +0 -28
  108. data/lib/zucker/hash.rb +0 -28
  109. data/lib/zucker/hash_to_proc.rb +0 -17
  110. data/lib/zucker/info.rb +0 -193
  111. data/lib/zucker/ivars.rb +0 -29
  112. data/lib/zucker/kernel.rb +0 -42
  113. data/lib/zucker/mcopy.rb +0 -11
  114. data/lib/zucker/mm.rb +0 -41
  115. data/lib/zucker/not.rb +0 -20
  116. data/lib/zucker/object.rb +0 -4
  117. data/lib/zucker/oo.rb +0 -18
  118. data/lib/zucker/os.rb +0 -55
  119. data/lib/zucker/qq.rb +0 -13
  120. data/lib/zucker/regexp_to_proc.rb +0 -13
  121. data/lib/zucker/regexp_visualize.rb +0 -31
  122. data/lib/zucker/ruby_version.rb +0 -120
  123. data/lib/zucker/sandbox.rb +0 -17
  124. data/lib/zucker/shortcuts.rb +0 -4
  125. data/lib/zucker/string.rb +0 -55
  126. data/lib/zucker/to_proc.rb +0 -4
  127. data/lib/zucker/unary.rb +0 -25
  128. data/lib/zucker/union.rb +0 -17
  129. data/spec/aliases_spec.rb +0 -13
  130. data/spec/array_spec.rb +0 -16
  131. data/spec/array_to_proc_spec.rb +0 -12
  132. data/spec/binding_spec.rb +0 -6
  133. data/spec/case_spec.rb +0 -15
  134. data/spec/cc_spec.rb +0 -6
  135. data/spec/class_to_proc_spec.rb +0 -9
  136. data/spec/dd_spec.rb +0 -37
  137. data/spec/engine_spec.rb +0 -11
  138. data/spec/enumerable_spec.rb +0 -8
  139. data/spec/file_spec.rb +0 -46
  140. data/spec/hash_spec.rb +0 -25
  141. data/spec/hash_to_proc_spec.rb +0 -12
  142. data/spec/info_spec.rb +0 -8
  143. data/spec/ivars_spec.rb +0 -20
  144. data/spec/kernel_spec.rb +0 -42
  145. data/spec/mm_spec.rb +0 -6
  146. data/spec/not_spec.rb +0 -10
  147. data/spec/oo_spec.rb +0 -6
  148. data/spec/os_spec.rb +0 -37
  149. data/spec/qq_spec.rb +0 -19
  150. data/spec/regexp_to_proc_spec.rb +0 -9
  151. data/spec/regexp_visualize_spec.rb +0 -38
  152. data/spec/ruby_version_spec.rb +0 -55
  153. data/spec/sandbox_spec.rb +0 -14
  154. data/spec/string_spec.rb +0 -65
@@ -0,0 +1,11 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module MarshalCopy
5
+ refine Object do
6
+ def marshal_copy
7
+ Marshal.load Marshal.dump self
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module Mash
5
+ refine Enumerator do
6
+ def mash
7
+ ret = {}
8
+ each{ |kv| ret.store( *(yield(kv)[0,2]) ) }
9
+ ret
10
+ end
11
+ end
12
+
13
+ refine Array do
14
+ def mash
15
+ ret = {}
16
+ each{ |kv| ret.store( *(yield(kv)[0,2]) ) }
17
+ ret
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,17 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module Ords
5
+ refine Array do
6
+ def chrs
7
+ self.pack 'C*'
8
+ end
9
+ end
10
+
11
+ refine String do
12
+ def ords
13
+ self.unpack 'C*'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module RegexpUnion
5
+ refine Regexp do
6
+ def |(arg)
7
+ Regexp.union self, arg.is_a?(Regexp) ? arg : arg.to_s
8
+ end
9
+ end
10
+
11
+ refine String do
12
+ def |(arg)
13
+ Regexp.union self, arg.is_a?(Regexp) ? arg : arg.to_s
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,22 +1,19 @@
1
1
  require 'zucker'
2
- module Zucker SquareBracketsFor = true end
3
2
 
4
- class Module
5
- def square_brackets_for(ivar, assignment = true)
6
- # undef [] if respond_to? :[]
7
- # undef []= if respond_to? :[]=
3
+ module Zucker
4
+ module SquareBracketsFor
5
+ refine Object do
6
+ def square_brackets_for(ivar, assignment = true)
7
+ define_method :[] do |key|
8
+ (instance_variable_get :"@#{ivar}")[key]
9
+ end
8
10
 
9
- define_method :[] do |key|
10
- (instance_variable_get :"@#{ivar}")[key]
11
- end
12
-
13
- if assignment
14
- define_method :[]= do |key, value|
15
- (instance_variable_get :"@#{ivar}")[key] = value
11
+ if assignment
12
+ define_method :[]= do |key, value|
13
+ (instance_variable_get :"@#{ivar}")[key] = value
14
+ end
15
+ end
16
16
  end
17
17
  end
18
18
  end
19
19
  end
20
-
21
- # J-_-L
22
-
@@ -0,0 +1,15 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module StringExtras
5
+ refine String do
6
+ def lchomp(arg = $/)
7
+ reverse.chomp(arg).reverse
8
+ end
9
+
10
+ def lchomp!(arg = $/)
11
+ replace reverse.chomp(arg).reverse
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module StringOp
5
+ refine String do
6
+ def -(rem)
7
+ gsub( Regexp === rem ? rem : rem.to_s, '' )
8
+ end
9
+
10
+ def ^(pos)
11
+ pos = pos.to_i
12
+ if pos >= 0
13
+ self[pos..-1]
14
+ else
15
+ self[0...pos]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/zucker/tap.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  require 'zucker'
2
- module Zucker Tap = true end
3
2
 
4
- def tap_on(obj, &block)
5
- obj.tap(&block)
6
- end
3
+ module Zucker
4
+ module Tap
5
+ refine Object do
6
+ private
7
7
 
8
- def make_new(what, *args, &block)
9
- what.new(*args).tap(&block)
10
- end
8
+ def tap_on(obj, &block)
9
+ obj.tap(&block)
10
+ end
11
11
 
12
- # J-_-L
12
+ def make_new(what, *args, &block)
13
+ what.new(*args).tap(&block)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module UnaryConversion
5
+ refine String do
6
+ def +@
7
+ self
8
+ end
9
+
10
+ def -@
11
+ to_sym
12
+ end
13
+ end
14
+
15
+ refine Symbol do
16
+ def +@
17
+ to_s
18
+ end
19
+
20
+ def -@
21
+ self
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,18 +1,8 @@
1
1
  require 'zucker/alias_for'
2
+ using Zucker::AliasFor
2
3
 
3
- describe 'alias_for' do
4
- it 'should create an alias for global methods' do
5
- # rspec bug?
6
- # def m1
7
- # 1
8
- # end
9
- # alias_for :m1, :a1
10
- #
11
- # proc do
12
- # a1.should == 1
13
- # end.should_not raise_exception
14
- end
15
4
 
5
+ describe 'alias_for' do
16
6
  it 'should create an alias for instance methods' do
17
7
  class Array
18
8
  def m2
@@ -36,9 +26,9 @@ describe 'alias_for' do
36
26
  end
37
27
  end
38
28
 
39
- proc do
29
+ proc{
40
30
  Array.a3.should == 3
41
- end.should_not raise_exception
31
+ }.should_not raise_exception
42
32
  end
43
33
 
44
34
 
@@ -47,71 +37,19 @@ describe 'alias_for' do
47
37
  def m4
48
38
  4
49
39
  end
50
- alias_for :m4, :ma, :mb, :mc
40
+ alias_for :m4, :ma, :mb, :mc
51
41
  end
52
- proc do
42
+
43
+ proc{
53
44
  1.ma.should == 4
54
45
  "1".mb.should == 4
55
46
  [1].mc.should == 4
56
- end.should_not raise_exception
57
- end
58
- end
59
-
60
- # alias_method
61
- module Blip
62
- def blip
63
- 'blip'
64
- end
65
- alias_methods_for :blip, :blap, :blup
66
-
67
- class << self
68
- def self_blip
69
- 'blip'
70
- end
71
- alias_methods_for :self_blip, :self_blap, :self_blup
72
- end
73
- end
74
-
75
- class Hello
76
- include Blip
77
-
78
- def hello
79
- 'hello'
80
- end
81
- alias_methods_for :hello, :hi, :howdy
82
-
83
- class << self
84
- def self_hello
85
- 'hello'
86
- end
87
- alias_methods_for :self_hello, :self_hi, :self_howdy
88
- end
89
- end
90
-
91
- describe 'alias_methods_for' do
92
- let(:h) { Hello.new }
93
-
94
- context "module context" do
95
- it "should alias instance methods" do
96
- h.blap.should == h.blip
97
- h.blup.should == h.blip
98
- end
99
-
100
- it "should alias class methods" do
101
- Blip.self_blap.should == Blip.self_blip
102
- Blip.self_blup.should == Blip.self_blip
103
- end
47
+ }.should_not raise_exception
104
48
  end
105
49
 
106
- context "class context" do
107
- it "should alias instance methods" do
108
- h.hi.should == h.hello
109
- h.howdy.should == h.hello
110
- end
111
-
112
- it "should alias class methods" do
113
- Hello.self_hi.should == Hello.self_hello
114
- Hello.self_howdy.should == Hello.self_hello
50
+ it 'works with uncommon chars' do
51
+ class Object
52
+ alias_for :tainted?, :"tain-ted?"
115
53
  end
116
54
  end
117
55
  end
@@ -0,0 +1,17 @@
1
+ require 'zucker/array_op'
2
+ using Zucker::ArrayOp
3
+
4
+
5
+ describe 'Array#^' do
6
+ it 'does an exclusive or' do
7
+ a = [1,2,3,4]
8
+ b = [3,4,5,6]
9
+ (a^b).should == [1,2,5,6]
10
+ end
11
+ end
12
+
13
+ describe 'Array#**' do
14
+ it 'returns the array product' do
15
+ ([1,2] ** %w[a b]).should == [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]
16
+ end
17
+ end
data/spec/blank_spec.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'zucker/blank'
2
+ using Zucker::Blank
3
+
2
4
 
3
5
  describe 'Object#blank?' do
4
6
  it 'should be blank for blank values' do
@@ -0,0 +1,15 @@
1
+ require 'zucker/camel_snake'
2
+ using Zucker::CamelSnake
3
+
4
+
5
+ describe 'String#to_camel' do
6
+ it 'should turn a snake_cased string to CamelCase' do
7
+ 'was_snake_case'.to_camel.should == 'WasSnakeCase'
8
+ end
9
+ end
10
+
11
+ describe 'String#to_snake' do
12
+ it 'should turn a CamelCased string to snake_case' do
13
+ 'WasCamelCase'.to_snake.should == 'was_camel_case'
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'zucker/constantize'
2
+ using Zucker::Constantize
3
+
4
+
5
+ describe 'String#constantize' do
6
+ it 'should return the constant with that name' do
7
+ 'Object'.constantize.should == Object
8
+ end
9
+
10
+ it 'should also work for nested constants' do
11
+ 'Zucker::VERSION'.constantize.should == Zucker::VERSION
12
+ end
13
+
14
+ it 'should throw name error if constant does not exist (and no parameter is given)' do
15
+ proc do
16
+ 'ObfsefsefsefafesafaefRubyZuckerafdfselijfesject'.constantize
17
+ end.should raise_exception NameError
18
+ end
19
+
20
+ it 'should call the block (and not raise an error) if constant does not exist and block given' do
21
+ proc do
22
+ 'ObfsefsefsefafesafaefRubyZuckerafdfselijfesject'.constantize do |string|
23
+ Default = [1,2,3]
24
+ end.should == [1,2,3]
25
+ end.should_not raise_exception
26
+ end
27
+
28
+ it 'should return the second parameter (and not raise an error) if constant does not exist and parameter given' do
29
+ proc do
30
+ 'ObfsefsefsefafesafaefRubyZuckerafdfselijfesject'.constantize(Array).should == Array
31
+ end.should_not raise_exception
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require 'zucker/dir_extras'
2
+ require 'securerandom'
3
+ using Zucker::DirExtras
4
+
5
+
6
+ describe Dir do
7
+ describe '.join' do
8
+ it 'delegates to File.join' do
9
+ a = %w[some file path]
10
+ expect( Dir.join(a) ).to be == File.join(a)
11
+ end
12
+ end
13
+
14
+ describe '.split' do
15
+ it 'delegates to File.split' do
16
+ a = 'some/file/path'
17
+ expect( Dir.split(a) ).to be == File.split(a)
18
+ end
19
+ end
20
+
21
+ describe '.rm' do
22
+ it 'removes directories with content' do
23
+ path = "tmp_#{SecureRandom.uuid}"
24
+ FileUtils.mkdir path
25
+ FileUtils.touch "#{path}/123"
26
+ expect{
27
+ Dir.rm path
28
+ }.not_to raise_error
29
+ expect( Dir['*'] ).to_not include(path)
30
+ end
31
+ end
32
+ end
data/spec/egonil_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'zucker/egonil'
2
+ using Zucker::Egonil
2
3
 
3
4
  describe 'egonil' do
4
5
  it 'should not raise nil exceptions in the block' do
@@ -0,0 +1,57 @@
1
+ require 'zucker/file_extras'
2
+ require 'fileutils'
3
+ using Zucker::FileExtras
4
+
5
+
6
+ describe File do
7
+ describe '.filename' do
8
+ it 'behaves like basename' do
9
+ File.filename('some/path/file.png').should == 'file.png'
10
+ end
11
+ end
12
+
13
+ describe '.gsub' do
14
+ after{ File.delete! 'random_filename' }
15
+ let :random_filename do
16
+ 'test_' + (0..20).map{|e| [*'a'..'z'].send RUBY_VERSION > '1.9' ? :sample : :choice }*''
17
+ end
18
+
19
+ it 'should read filename in arg1, substitute every key in the arg2 with its value and save the file' do
20
+ File.open(random_filename,'w'){ |file|
21
+ file.print 'should read filename in arg1, substitute every key in the arg2 with its value and save the file'
22
+ }
23
+ File.gsub random_filename,
24
+ /read/ => 'write',
25
+ /\d+/ => proc{|m| (m.to_i+1).to_s }
26
+
27
+ File.read(random_filename).should ==
28
+ 'should write filename in arg2, substitute every key in the arg3 with its value and save the file'
29
+
30
+ FileUtils.rm random_filename
31
+ end
32
+ end
33
+
34
+ describe '.delete!' do
35
+ after{ File.delete! 'random_filename' }
36
+ let :random_filename do
37
+ 'test_' + (0..20).map{|e| [*'a'..'z'].send RUBY_VERSION > '1.9' ? :sample : :choice }*''
38
+ end
39
+
40
+ it 'should delete the filename given as argument if it exists + return non-nil' do
41
+ FileUtils.touch random_filename
42
+ res = false
43
+ proc do
44
+ res = File.delete! random_filename
45
+ end.should_not raise_exception
46
+ res.should be_true
47
+ end
48
+
49
+ it 'should do nothing if the filename given as argument does not exist + return nil' do
50
+ res = false
51
+ proc do
52
+ res = File.delete! random_filename
53
+ end.should_not raise_exception
54
+ res.should be_false
55
+ end
56
+ end
57
+ end