sugar_refinery 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +157 -0
  3. data/MIT-LICENSE.txt +20 -0
  4. data/README.md +42 -0
  5. data/Rakefile +49 -0
  6. data/doc/create_documentation.rb +471 -0
  7. data/doc/index.html +1687 -0
  8. data/lib/sugar_refinery.rb +1 -0
  9. data/lib/sugar_refinery/alias_for.rb +15 -0
  10. data/lib/sugar_refinery/all.rb +5 -0
  11. data/lib/sugar_refinery/array_op.rb +15 -0
  12. data/lib/sugar_refinery/array_stats.rb +31 -0
  13. data/lib/sugar_refinery/blank.rb +47 -0
  14. data/lib/sugar_refinery/camel_snake.rb +15 -0
  15. data/lib/sugar_refinery/chain_map.rb +11 -0
  16. data/lib/sugar_refinery/constantize.rb +29 -0
  17. data/lib/sugar_refinery/dir_utils.rb +20 -0
  18. data/lib/sugar_refinery/file_force_delete.rb +12 -0
  19. data/lib/sugar_refinery/file_gsub.rb +23 -0
  20. data/lib/sugar_refinery/hash_op.rb +28 -0
  21. data/lib/sugar_refinery/hash_zip.rb +11 -0
  22. data/lib/sugar_refinery/inner_map.rb +15 -0
  23. data/lib/sugar_refinery/lchomp.rb +15 -0
  24. data/lib/sugar_refinery/marshal_copy.rb +11 -0
  25. data/lib/sugar_refinery/mash.rb +22 -0
  26. data/lib/sugar_refinery/ords.rb +17 -0
  27. data/lib/sugar_refinery/regexp_union.rb +17 -0
  28. data/lib/sugar_refinery/same.rb +11 -0
  29. data/lib/sugar_refinery/string_op.rb +20 -0
  30. data/lib/sugar_refinery/version.rb +3 -0
  31. data/spec/alias_for_spec.rb +55 -0
  32. data/spec/array_op_spec.rb +17 -0
  33. data/spec/array_stats_spec.rb +82 -0
  34. data/spec/blank_spec.rb +22 -0
  35. data/spec/camel_snake_spec.rb +15 -0
  36. data/spec/chain_map_spec.rb +16 -0
  37. data/spec/constantize_spec.rb +33 -0
  38. data/spec/dir_utils_spec.rb +32 -0
  39. data/spec/file_force_delete_spec.rb +30 -0
  40. data/spec/file_gsub_spec.rb +26 -0
  41. data/spec/hash_op_spec.rb +28 -0
  42. data/spec/hash_zip_spec.rb +11 -0
  43. data/spec/inner_map_spec.rb +24 -0
  44. data/spec/lchomp_spec.rb +20 -0
  45. data/spec/marshal_copy_spec.rb +15 -0
  46. data/spec/mash_spec.rb +17 -0
  47. data/spec/ords_spec.rb +15 -0
  48. data/spec/regexp_union_spec.rb +19 -0
  49. data/spec/same_spec.rb +10 -0
  50. data/spec/spec_helper.rb +18 -0
  51. data/spec/string_op_spec.rb +22 -0
  52. data/sugar_refinery.gemspec +20 -0
  53. metadata +138 -0
@@ -0,0 +1,17 @@
1
+ require 'sugar_refinery/array_op'
2
+ using SugarRefinery::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
@@ -0,0 +1,82 @@
1
+ require "sugar_refinery/array_stats"
2
+ using SugarRefinery::ArrayStats
3
+
4
+ describe "doing statistics on arrays" do
5
+ let(:list) { (2..5).to_a }
6
+
7
+ describe "Array#mean" do
8
+ it "should return the average" do
9
+ list.mean.should == 3.5
10
+ end
11
+ end
12
+
13
+ describe "Array#stdev_sample" do
14
+ it "should return the standard deviation of the sample" do
15
+ list.stdev_sample.should be_close(1.2909944487358056, 1e-8)
16
+ end
17
+ end
18
+
19
+ describe "Array#stdev_population" do
20
+ it "should return the standard deviation of the population" do
21
+ list.stdev_population.should be_close(1.118033988749895, 1e-8)
22
+ end
23
+ end
24
+
25
+ describe "Array#stdev" do
26
+ it "should default to population" do
27
+ list.stdev.should be_close(list.stdev_population, 1e-8)
28
+ end
29
+
30
+ it "should delegate sample correctly" do
31
+ list.stdev(:sample).should be_close(list.stdev_sample, 1e-8)
32
+ end
33
+
34
+ it "should delegate population correctly" do
35
+ list.stdev(:population).should be_close(list.stdev_population, 1e-8)
36
+ end
37
+
38
+ it "should raise an error with any other key" do
39
+ expect { list.stdev(:pony) }.to raise_error(ArgumentError)
40
+ end
41
+ end
42
+
43
+ describe "Array#z_score" do
44
+ it "should default to population" do
45
+ list.z_score.zip(list.z_score(:population)).each do |value, actual|
46
+ value.should be_close(actual, 1e-8)
47
+ end
48
+ end
49
+
50
+ it "should delegate sample correctly" do
51
+ sample_z_score = [
52
+ -1.161895003862225,
53
+ -0.3872983346207417,
54
+ 0.3872983346207417,
55
+ 1.161895003862225
56
+ ]
57
+
58
+ p list
59
+
60
+ list.z_score(:sample).zip(sample_z_score).each do |value, actual|
61
+ value.should be_close(actual, 1e-8)
62
+ end
63
+ end
64
+
65
+ it "should delegate population correctly" do
66
+ population_z_score = [
67
+ -1.3416407864998738,
68
+ -0.4472135954999579,
69
+ 0.4472135954999579,
70
+ 1.3416407864998738
71
+ ]
72
+
73
+ list.z_score(:population).zip(population_z_score).each do |value, actual|
74
+ value.should be_close(actual, 1e-8)
75
+ end
76
+ end
77
+
78
+ it "should raise an error with any other key" do
79
+ expect { list.z_score(:pony) }.to raise_error(ArgumentError)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,22 @@
1
+ require 'sugar_refinery/blank'
2
+ using SugarRefinery::Blank
3
+
4
+
5
+ describe 'Object#blank?' do
6
+ it 'should be blank for blank values' do
7
+ blank_values = [ nil, false, '', ' ', " \n\t \r ", [], {}, // ]
8
+
9
+ blank_values.each{ |blank|
10
+ blank.blank?.should == true
11
+ }
12
+ end
13
+
14
+ it 'should not be blank for non blank values' do
15
+ present_values = [ Object.new, true, 0, 1, 'a', [nil], { nil => nil } ]
16
+
17
+ present_values.each{ |present|
18
+ present.blank?.should == false
19
+ }
20
+ end
21
+ end
22
+
@@ -0,0 +1,15 @@
1
+ require 'sugar_refinery/camel_snake'
2
+ using SugarRefinery::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,16 @@
1
+ require "sugar_refinery/chain_map"
2
+ using SugarRefinery::ChainMap
3
+
4
+ describe "Array#chain_map" do
5
+ it "should repeatedly chain symbols as map calls" do
6
+ list = [-3, -2, -1]
7
+
8
+ list.chain_map(:abs, :to_s) == %w{3 2 1}
9
+ end
10
+
11
+ it "should repeatedly chain blocks as map calls" do
12
+ list = [-3, -2, -1]
13
+
14
+ list.chain_map(->(e) { e ** 2 }, ->(e) { e * -1 }) == [-9, -4, -1]
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ require 'sugar_refinery/constantize'
2
+ using SugarRefinery::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
+ 'SugarRefinery::VERSION'.constantize.should == SugarRefinery::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
+ 'ObfsefsefsefafesafaefRubySugarRefineryafdfselijfesject'.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
+ 'ObfsefsefsefafesafaefRubySugarRefineryafdfselijfesject'.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
+ 'ObfsefsefsefafesafaefRubySugarRefineryafdfselijfesject'.constantize(Array).should == Array
31
+ end.should_not raise_exception
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require 'sugar_refinery/dir_utils'
2
+ require 'securerandom'
3
+ using SugarRefinery::DirUtils
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
@@ -0,0 +1,30 @@
1
+ require 'sugar_refinery/file_force_delete'
2
+ require 'fileutils'
3
+ using SugarRefinery::FileForceDelete
4
+
5
+
6
+ describe File do
7
+ describe '.delete!' do
8
+ after{ File.delete! 'random_filename' }
9
+ let :random_filename do
10
+ 'test_' + (0..20).map{|e| [*'a'..'z'].sample }*''
11
+ end
12
+
13
+ it 'should delete the filename given as argument if it exists + return non-nil' do
14
+ FileUtils.touch random_filename
15
+ res = false
16
+ proc do
17
+ res = File.delete! random_filename
18
+ end.should_not raise_exception
19
+ res.should be_truthy
20
+ end
21
+
22
+ it 'should do nothing if the filename given as argument does not exist + return nil' do
23
+ res = false
24
+ proc do
25
+ res = File.delete! random_filename
26
+ end.should_not raise_exception
27
+ res.should be_falsey
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ require 'sugar_refinery/file_gsub'
2
+ require 'fileutils'
3
+ using SugarRefinery::FileGsub
4
+
5
+
6
+ describe File do
7
+ describe '.gsub' do
8
+ let :random_filename do
9
+ 'test_' + (0..20).map{|e| [*'a'..'z'].sample }*''
10
+ end
11
+
12
+ it 'should read filename in arg1, substitute every key in the arg2 with its value and save the file' do
13
+ File.open(random_filename,'w'){ |file|
14
+ file.print 'should read filename in arg1, substitute every key in the arg2 with its value and save the file'
15
+ }
16
+ File.gsub random_filename,
17
+ /read/ => 'write',
18
+ /\d+/ => proc{|m| (m.to_i+1).to_s }
19
+
20
+ File.read(random_filename).should ==
21
+ 'should write filename in arg2, substitute every key in the arg3 with its value and save the file'
22
+
23
+ FileUtils.rm random_filename
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ require 'sugar_refinery/hash_op'
2
+ using SugarRefinery::HashOp
3
+
4
+
5
+ describe 'Hash#<<' do
6
+ it 'appends new elements to the hash' do
7
+ a = { 1=>4, 2=>5, 3=>6 }
8
+ a << { 4=>7 }
9
+ a << [5, 8]
10
+ a.should == { 1=>4, 2=>5, 3=>6, 4=>7, 5=>8 }
11
+ end
12
+ end
13
+
14
+ describe 'Hash#&' do
15
+ it 'selects a sub hash containt only equal key-value pairs' do
16
+ a = { 1=>4, 2=>5, 3=>6 }
17
+ b = { 1=>4, 2=>7 }
18
+ (a & b).should == { 1=>4 }
19
+ end
20
+ end
21
+
22
+ describe 'Hash#+' do
23
+ it 'merges two hashes' do
24
+ a = { 1=>4, 2=>5, 3=>6 }
25
+ b = { 1=>4, 2=>7, 4=>0 }
26
+ (a + b).should == { 1=>4, 2=>7, 3=>6, 4=>0 }
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'sugar_refinery/hash_zip'
2
+ using SugarRefinery::HashZip
3
+
4
+
5
+ describe Hash do
6
+ describe '.zip' do
7
+ it 'should zip together both given enumerables and take them as key=>values for a new hash' do
8
+ Hash.zip( [1,2,3], [4,5,6] ).should == { 1=>4, 2=>5, 3=>6 }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ require "sugar_refinery/inner_map"
2
+ using SugarRefinery::InnerMap
3
+
4
+ describe "Array#inner_map" do
5
+ it "should delegate map to inner lists" do
6
+ list = [[1, 2], [3, 4]]
7
+
8
+ list.inner_map { |i| i + 1 }.should == [[2, 3], [4, 5]]
9
+ end
10
+ end
11
+
12
+ describe "Array#inner_inject" do
13
+ it "should delegate inject to inner lists" do
14
+ list = [%w{a b c}, %w{d e f}]
15
+
16
+ list.inner_inject(&:+).should == list.map(&:join)
17
+ end
18
+
19
+ it "should take default values" do
20
+ list = [[3, 2, 1], [-4]]
21
+
22
+ list.inner_inject(4, &:+).should == [10, 0]
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'sugar_refinery/lchomp'
2
+ using SugarRefinery::Lchomp
3
+
4
+ describe String do
5
+ describe '#lchomp' do
6
+ it 'should chomp on the left side' do
7
+ string = 'ameise'
8
+ expect( string.lchomp('a') ).to eq 'meise'
9
+ expect( string ).to eq 'ameise'
10
+ end
11
+ end
12
+
13
+ describe '#lchomp!' do
14
+ it 'should chomp on the left side (mutating)' do
15
+ string = 'ameise'
16
+ expect( string.lchomp!('a') ).to eq 'meise'
17
+ expect( string ).to eq 'meise'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'sugar_refinery/marshal_copy'
2
+ using SugarRefinery::MarshalCopy
3
+
4
+
5
+ describe 'Object#marshal_copy' do
6
+ it 'create a (deep) copy via marshalling' do
7
+ a = %w[hello world]
8
+ b = a.marshal_copy
9
+ b.should == a
10
+
11
+ b[0][1,1] = ''
12
+ b.should_not == a
13
+ end
14
+ end
15
+
@@ -0,0 +1,17 @@
1
+ require 'sugar_refinery/mash'
2
+ using SugarRefinery::Mash
3
+
4
+
5
+ describe 'Array#mash' do
6
+ it 'should "map" a hash' do
7
+ [1,2,3].mash{|e| [e, e.to_s] }.should == {1=>'1',2=>'2',3=>'3',}
8
+ end
9
+ end
10
+
11
+
12
+ describe 'Enumerator#mash' do
13
+ it 'should "map" a hash' do
14
+ [1,2,3].each.mash{|e| [e, e.to_s] }.should == {1=>'1',2=>'2',3=>'3',}
15
+ end
16
+ end
17
+
@@ -0,0 +1,15 @@
1
+ require 'sugar_refinery/ords'
2
+ using SugarRefinery::Ords
3
+
4
+
5
+ describe 'Array#chrs' do
6
+ it 'converts the array to a string, using each element as ord value for the char' do
7
+ [72, 97, 108, 108, 111].chrs.should == 'Hallo'
8
+ end
9
+ end
10
+
11
+ describe 'String#ords' do
12
+ it 'unpacks characters' do
13
+ 'Hallo'.ords.should == [72, 97, 108, 108, 111]
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ require 'sugar_refinery/regexp_union'
2
+ using SugarRefinery::RegexpUnion
3
+
4
+
5
+ shared_examples_for "Regexp.union operator" do
6
+ it "creates a Regexp.union of both operands" do
7
+ (/Ruby\d/ | /test/i | "cheat").should ==
8
+ Regexp.union( Regexp.union( /Ruby\d/, /test/i ), "cheat" )
9
+ end
10
+ end
11
+
12
+ describe 'Regexp#|' do
13
+ it_should_behave_like 'Regexp.union operator'
14
+ end
15
+
16
+ describe 'String#|' do
17
+ it_should_behave_like 'Regexp.union operator'
18
+ end
19
+