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
data/lib/zucker.rb CHANGED
@@ -1,52 +1,22 @@
1
- # documentation at http://rubyzucker.info or doc/zucker_doc.html
1
+ begin
2
+ require 'refine'
3
+ rescue LoadError
4
+ if RUBY_VERSION < '2.0'
5
+ warn "The zucker library depends on refinements, please install the refine gem, when running a RubyVersion < 2.0"
6
+ end
7
+ end
2
8
 
3
9
  module Zucker
4
- # version and date get modified by the :prepare_release rake task
5
- VERSION = '13.1'
6
- DATE = '2013-04-30'
7
-
8
- # cube list
9
- PACKS = {
10
- :control => %w|egonil iterate tap sandbox kernel|,
11
- :env => %w|engine info os ruby_version|,
12
- :extensions => %w|array case enumerable file hash string unary union|,
13
- :object => %w|blank mcopy not|,
14
- :to_proc => %w|array_to_proc class_to_proc hash_to_proc regexp_to_proc|,
15
- :shortcuts => %w|aliases alias_for square_brackets_for ivars|,
16
- :debug => %w|binding cc dd mm oo qq regexp_visualize|,
17
- }
18
-
19
- NON_1_8_CUBES = %w|not case|
10
+ VERSION = '100.0.0'
11
+ DATE = '2014-04-01'
20
12
 
21
13
  class << self
22
- # Zucker require helpers
23
- def require_cube(cube)
24
- unless RUBY_VERSION < '1.9' && Zucker::NON_1_8_CUBES.include?(cube)
25
- require "zucker/#{cube}"
26
- end
27
- end
28
-
29
- def require_pack(pack)
30
- PACKS[pack.to_sym].each{ |cube|
31
- require_cube cube
32
- }
33
- end
34
-
35
- def require_this(filename) # pack shortcut
36
- pack = ::File.basename( filename ).chomp( ::File.extname( filename ))
37
- require_pack(pack)
38
- end
39
-
40
- def require_default
41
- PACKS.map{ |pack, _|
42
- require_pack pack if pack != :debug
43
- }.compact
14
+ def require(cube)
15
+ require_relative "zucker/#{cube}"
44
16
  end
45
17
 
46
18
  def require_all
47
- require_default + [require_pack( :debug )]
19
+ Dir[File.dirname(__FILE__) + '/zucker/*'].each{ |f| self.require File.basename(f) }
48
20
  end
49
21
  end
50
22
  end
51
-
52
- # J-_-L
@@ -1,22 +1,15 @@
1
1
  require 'zucker'
2
- module Zucker AliasFor = true end
3
2
 
4
- def alias_for(m, *aliases)
5
- aliases.each{ |a|
6
- class_eval "alias :'#{ a.to_s }' :'#{ m.to_s }'"
7
- }
8
- end
9
- alias aliases_for alias_for
3
+ module Zucker
4
+ module AliasFor
5
+ refine Module do
6
+ private
10
7
 
11
- class Module
12
- def alias_method_for(m, *alias_methods)
13
- alias_methods.each{ |a|
14
- class_eval do
15
- alias_method a.to_sym, m.to_sym
8
+ def alias_for(m, *aliases)
9
+ aliases.each{ |a| class_eval "alias :'#{ a }' :'#{ m }'" }
16
10
  end
17
- }
11
+
12
+ alias aliases_for alias_for
13
+ end
18
14
  end
19
- alias alias_methods_for alias_method_for
20
15
  end
21
-
22
- # J-_-L
data/lib/zucker/all.rb CHANGED
@@ -1,4 +1,2 @@
1
1
  require 'zucker'
2
2
  Zucker.require_all
3
-
4
- # J-_-L
@@ -0,0 +1,15 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
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
data/lib/zucker/blank.rb CHANGED
@@ -1,29 +1,47 @@
1
1
  require 'zucker'
2
- module Zucker Blank = true end
3
2
 
4
- class Object
5
- def blank?
6
- if respond_to? :empty? then empty? else !self end
7
- end
3
+ module Zucker
4
+ module Blank
5
+ refine Object do
6
+ def blank?
7
+ if respond_to? :empty? then empty? else !self end
8
+ end
8
9
 
9
- def present?
10
- !blank?
11
- end
12
- end
10
+ def present?
11
+ !blank?
12
+ end
13
+ end
14
+
15
+ refine NilClass do
16
+ def blank?() true end
17
+ end
13
18
 
19
+ refine FalseClass do
20
+ def blank?() true end
21
+ end
14
22
 
15
- { # what to do # for which classes
16
- lambda{ true } => [FalseClass, NilClass],
17
- lambda{ false } => [TrueClass, Numeric],
18
- lambda{ empty? } => [Array, Hash],
19
- lambda{ self !~ /\S/ } => [String],
20
- lambda{ self == // } => [Regexp],
23
+ refine TrueClass do
24
+ def blank?() false end
25
+ end
21
26
 
22
- }.each{ |action, klass_array|
23
- klass_array.each{ |klass|
24
- klass.send :define_method, :blank?, &action
25
- }
26
- }
27
+ refine Numeric do
28
+ def blank?() false end
29
+ end
27
30
 
28
- # J-_-L
31
+ refine Array do
32
+ def blank?() empty? end
33
+ end
29
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 'zucker'
2
+
3
+ module Zucker
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,29 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
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 'zucker'
2
+ require 'fileutils'
3
+
4
+ module Zucker
5
+ module DirExtras
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
data/lib/zucker/egonil.rb CHANGED
@@ -1,28 +1,17 @@
1
1
  require 'zucker'
2
- module Zucker Egonil = true end
3
2
 
4
- # code by Yohan, slightly edited and comments by me
5
- def egonil(&block)
6
- # grip methods
7
- ori_method_missing = NilClass.instance_method(:method_missing)
8
- catch_method_missing = NilClass.instance_method(:catch_method_missing)
9
- # activate ego mode
10
- NilClass.send :define_method, :method_missing, catch_method_missing
11
- # run code
12
- yield
13
- ensure
14
- # no matter what happens: restore default nil behaviour
15
- NilClass.send :define_method, :method_missing, ori_method_missing
16
- end
3
+ module Zucker
4
+ module Egonil
5
+ refine Object do
6
+ private
17
7
 
18
- # this is the ego nil
19
- class NilClass
20
- def catch_method_missing(m, *args, &block)
21
- nil
8
+ def egonil(&block)
9
+ mm = NilClass.instance_method(:method_missing)
10
+ NilClass.send :define_method, :method_missing, ->(*){}
11
+ yield
12
+ ensure
13
+ NilClass.send :define_method, :method_missing, mm
14
+ end
15
+ end
22
16
  end
23
- end
24
-
25
- alias nn egonil
26
-
27
- # J-_-L
28
-
17
+ end
@@ -0,0 +1,33 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module FileExtras
5
+ refine File.singleton_class do
6
+ def filename(*args, &block)
7
+ basename(*args, &block)
8
+ end
9
+
10
+ def gsub(filename, regex_hash)
11
+ data = File.read filename
12
+ File.open(filename,'w'){ |file|
13
+ regex_hash.each{ |regex, new_string|
14
+ regex = regex.to_s unless regex.is_a? Regexp
15
+
16
+ if new_string.is_a? Proc
17
+ data.gsub! regex, &new_string
18
+ else
19
+ data.gsub! regex, new_string
20
+ end
21
+ }
22
+ file.print data
23
+ }
24
+ end
25
+
26
+
27
+ def delete!(filename)
28
+ return nil if !File.exist?(filename)
29
+ File.delete filename
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module HashExtras
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,28 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
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,34 @@
1
+ require 'zucker'
2
+
3
+ module Zucker
4
+ module InstanceVariablesFrom
5
+ refine Object do
6
+ private
7
+
8
+ def instance_variables_from(obj, *only)
9
+ iter =
10
+ if obj.is_a? Binding
11
+ obj.eval('local_variables').map{|e| [obj.eval("#{e}"), e] }
12
+ elsif obj.is_a? Hash
13
+ obj.map{|k,v| [v,k] }
14
+ else
15
+ # elsif obj.is_a? Enumerable
16
+ obj.each.with_index
17
+ end
18
+
19
+ ret = []
20
+ iter.each{ |value, arg|
21
+ arg = arg.to_s
22
+ if only.include?(arg) || only.include?(arg.to_sym) || only.empty?
23
+ arg = '_' + arg if (48..57).member? arg.unpack('C')[0] # 1.8+1.9
24
+ ret << ivar = :"@#{arg}"
25
+ self.instance_variable_set ivar, value
26
+ end
27
+ }
28
+ ret
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+
@@ -1,28 +1,31 @@
1
1
  require 'zucker'
2
- module Zucker Iterate = true end
3
2
 
4
- def iterate(*params)
5
- # params.shift.zip(*params).each{ |*elements| yield *elements }
6
- raise ArgumentError, "wrong number of arguments (0)" if params.empty?
3
+ module Zucker
4
+ module Iterate
5
+ refine Object do
6
+ def iterate(*params)
7
+ # params.shift.zip(*params).each{ |*elements| yield *elements }
8
+ raise ArgumentError, "wrong number of arguments (0)" if params.empty?
7
9
 
8
- first = params.shift
9
- if params.empty? # single param - like each
10
- if block_given?
11
- first.map{|e| yield e }
12
- else
13
- first.map.to_enum
14
- end
15
- else # multiple params
16
- max_size = [first, *params].max_by(&:count).size
17
- padded_first = first.to_a + [nil]*(max_size - first.count) # append nils
18
- obj = padded_first.zip(*params)
19
- if block_given?
20
- obj.map{|es| yield(*es) }
21
- else
22
- obj.map.to_enum
10
+ first = params.shift
11
+ if params.empty? # single param - like each
12
+ if block_given?
13
+ first.map{|e| yield e }
14
+ else
15
+ first.map.to_enum
16
+ end
17
+ else # multiple params
18
+ max_size = [first, *params].max_by(&:count).size
19
+ padded_first = first.to_a + [nil]*(max_size - first.count) # append nils
20
+ obj = padded_first.zip(*params)
21
+ if block_given?
22
+ obj.map{|es| yield(*es) }
23
+ else
24
+ obj.map.to_enum
25
+ end
26
+ end
27
+ end
23
28
  end
24
29
  end
25
30
  end
26
31
 
27
- # J-_-L
28
-