sugar_refinery 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 @@
1
+ require_relative 'sugar_refinery/version'
@@ -0,0 +1,15 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module AliasFor
5
+ refine Module do
6
+ private
7
+
8
+ def alias_for(m, *aliases)
9
+ aliases.each{ |a| class_eval "alias :'#{ a }' :'#{ m }'" }
10
+ end
11
+
12
+ alias aliases_for alias_for
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'version'
2
+
3
+ Dir["#{File.dirname(__FILE__)}/*"].each{ |f|
4
+ require_relative File.basename(f)
5
+ }
@@ -0,0 +1,15 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module ArrayOp
5
+ refine Array do
6
+ def ^(other)
7
+ (self - other) + (other - self)
8
+ end
9
+
10
+ def **(*o, &block)
11
+ product *o, &block
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module ArrayStats
5
+ refine Array do
6
+ def mean
7
+ inject(&:+) / length.to_f
8
+ end
9
+
10
+ def stdev(type = :population)
11
+ case type
12
+ when :population then stdev_population
13
+ when :sample then stdev_sample
14
+ else raise ArgumentError.new("%s is not a valid argument" % type)
15
+ end
16
+ end
17
+
18
+ def stdev_sample
19
+ Math.sqrt(inject(0.0) { |sum, x| sum + (mean - x) ** 2 } / (length - 1))
20
+ end
21
+
22
+ def stdev_population
23
+ Math.sqrt(inject(0.0) { |sum, x| sum + (mean - x) ** 2 } / length)
24
+ end
25
+
26
+ def z_score(type = :population)
27
+ map { |x| (x - mean) / stdev(type) }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module Blank
5
+ refine Object do
6
+ def blank?
7
+ if respond_to? :empty? then empty? else !self end
8
+ end
9
+
10
+ def present?
11
+ !blank?
12
+ end
13
+ end
14
+
15
+ refine NilClass do
16
+ def blank?() true end
17
+ end
18
+
19
+ refine FalseClass do
20
+ def blank?() true end
21
+ end
22
+
23
+ refine TrueClass do
24
+ def blank?() false end
25
+ end
26
+
27
+ refine Numeric do
28
+ def blank?() false end
29
+ end
30
+
31
+ refine Array do
32
+ def blank?() empty? end
33
+ end
34
+
35
+ refine Hash do
36
+ def blank?() empty? end
37
+ end
38
+
39
+ refine String do
40
+ def blank?() self !~ /\S/ end
41
+ end
42
+
43
+ refine Regexp do
44
+ def blank?() self == // end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module CamelSnake
5
+ refine String do
6
+ def to_snake
7
+ gsub(/(?<!^)[A-Z]/) do "_#$&" end.downcase
8
+ end
9
+
10
+ def to_camel
11
+ gsub(/(?:^|_)([a-z])/) do $1.upcase end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module ChainMap
5
+ refine Array do
6
+ def chain_map(*args)
7
+ args.inject(self) { |collection, action| collection.map(&action) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module Constantize
5
+ refine String do
6
+ def constantize(default_value = nil) # always uses global scope as in AS... is this good?
7
+ get_constant = lambda{
8
+ self.split(/::/).inject( Object ){ |base_constant, current_constant|
9
+ base_constant.const_get current_constant
10
+ }
11
+ }
12
+
13
+ if !default_value && !block_given?
14
+ get_constant.call
15
+ else
16
+ begin
17
+ get_constant.call
18
+ rescue NameError
19
+ if block_given?
20
+ yield self
21
+ else
22
+ default_value
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'version'
2
+ require 'fileutils'
3
+
4
+ module SugarRefinery
5
+ module DirUtils
6
+ refine Dir.singleton_class do
7
+ def join(*args)
8
+ File.join(*args)
9
+ end
10
+
11
+ def split(*args)
12
+ File.split(*args)
13
+ end
14
+
15
+ def rm(*args)
16
+ FileUtils.rm_r(*args)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module FileForceDelete
5
+ refine File.singleton_class do
6
+ def delete!(filename)
7
+ return nil if !File.exist?(filename)
8
+ File.delete filename
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module FileGsub
5
+ refine File.singleton_class do
6
+ def gsub(filename, regex_hash)
7
+ data = File.read filename
8
+ File.open(filename,'w'){ |file|
9
+ regex_hash.each{ |regex, new_string|
10
+ regex = regex.to_s unless regex.is_a? Regexp
11
+
12
+ if new_string.is_a? Proc
13
+ data.gsub! regex, &new_string
14
+ else
15
+ data.gsub! regex, new_string
16
+ end
17
+ }
18
+ file.print data
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module HashOp
5
+ refine Hash do
6
+ def <<(other)
7
+ case
8
+ when other.is_a?(Hash)
9
+ merge! other
10
+ when other.is_a?(Enumerable) || other.respond_to?(:to_splat)
11
+ merge! Hash[*other]
12
+ else
13
+ raise TypeError, 'can only append other Hashs and Enumerables (or Classes that implement to_splat)'
14
+ end
15
+ end
16
+
17
+ def &(other)
18
+ Hash[ *select{ |k,v|
19
+ other[k] == v
20
+ }.flatten ]
21
+ end
22
+
23
+ def +(*o, &block)
24
+ merge *o, &block
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module HashZip
5
+ refine Hash.singleton_class do
6
+ def zip(keys, values)
7
+ Hash[ *keys.zip(values).flatten ]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module InnerMap
5
+ refine Array do
6
+ def inner_map(&block)
7
+ map { |object| object.map(&block) }
8
+ end
9
+
10
+ def inner_inject(default = :not_used, &block)
11
+ map { |object| default == :not_used ? object.inject(&block) : object.inject(default, &block) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module Lchomp
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,11 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
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_relative 'version'
2
+
3
+ module SugarRefinery
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_relative 'version'
2
+
3
+ module SugarRefinery
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_relative 'version'
2
+
3
+ module SugarRefinery
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
@@ -0,0 +1,11 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
4
+ module Same
5
+ refine Float do
6
+ def same?(other)
7
+ (self - other).abs < Float::EPSILON
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'version'
2
+
3
+ module SugarRefinery
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
@@ -0,0 +1,3 @@
1
+ module SugarRefinery
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'sugar_refinery/alias_for'
2
+ using SugarRefinery::AliasFor
3
+
4
+
5
+ describe 'alias_for' do
6
+ it 'should create an alias for instance methods' do
7
+ class Array
8
+ def m2
9
+ 2
10
+ end
11
+ aliases_for :m2, :a2, :'a-2'
12
+ end
13
+ proc do
14
+ [1,2,3].a2.should == 2
15
+ [1,2,3].send('a-2').should == 2
16
+ end.should_not raise_exception
17
+ end
18
+
19
+ it 'should create an alias for class (singleton) methods' do
20
+ class Array
21
+ class << Array
22
+ def m3
23
+ 3
24
+ end
25
+ alias_for :m3, :a3
26
+ end
27
+ end
28
+
29
+ proc{
30
+ Array.a3.should == 3
31
+ }.should_not raise_exception
32
+ end
33
+
34
+
35
+ it 'should create aliases for the first argument with all other arguments' do
36
+ class Object
37
+ def m4
38
+ 4
39
+ end
40
+ alias_for :m4, :ma, :mb, :mc
41
+ end
42
+
43
+ proc{
44
+ 1.ma.should == 4
45
+ "1".mb.should == 4
46
+ [1].mc.should == 4
47
+ }.should_not raise_exception
48
+ end
49
+
50
+ it 'works with uncommon chars' do
51
+ class Object
52
+ alias_for :tainted?, :"tain-ted?"
53
+ end
54
+ end
55
+ end