zucker 13.1 → 100.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +17 -2
- data/MIT-LICENSE +1 -1
- data/README.md +22 -0
- data/Rakefile +17 -58
- data/doc/{zucker_doc.rb → create_documentation.rb} +67 -129
- data/doc/zucker.html +1774 -0
- data/lib/zucker.rb +12 -42
- data/lib/zucker/alias_for.rb +9 -16
- data/lib/zucker/all.rb +0 -2
- data/lib/zucker/array_op.rb +15 -0
- data/lib/zucker/blank.rb +39 -21
- data/lib/zucker/camel_snake.rb +15 -0
- data/lib/zucker/constantize.rb +29 -0
- data/lib/zucker/dir_extras.rb +20 -0
- data/lib/zucker/egonil.rb +13 -24
- data/lib/zucker/file_extras.rb +33 -0
- data/lib/zucker/hash_extras.rb +11 -0
- data/lib/zucker/hash_op.rb +28 -0
- data/lib/zucker/instance_variables_from.rb +34 -0
- data/lib/zucker/iterate.rb +24 -21
- data/lib/zucker/marshal_copy.rb +11 -0
- data/lib/zucker/mash.rb +22 -0
- data/lib/zucker/ords.rb +17 -0
- data/lib/zucker/regexp_union.rb +17 -0
- data/lib/zucker/square_brackets_for.rb +12 -15
- data/lib/zucker/string_extras.rb +15 -0
- data/lib/zucker/string_op.rb +20 -0
- data/lib/zucker/tap.rb +13 -8
- data/lib/zucker/unary_conversion.rb +25 -0
- data/spec/alias_for_spec.rb +11 -73
- data/spec/array_op_spec.rb +17 -0
- data/spec/blank_spec.rb +2 -0
- data/spec/camel_snake_spec.rb +15 -0
- data/spec/constantize_spec.rb +33 -0
- data/spec/dir_extras_spec.rb +32 -0
- data/spec/egonil_spec.rb +1 -0
- data/spec/file_extras_spec.rb +57 -0
- data/spec/hash_extras_spec.rb +11 -0
- data/spec/hash_op_spec.rb +28 -0
- data/spec/instance_variables_from_spec.rb +24 -0
- data/spec/iterate_spec.rb +2 -0
- data/spec/{mcopy_spec.rb → marshal_copy_spec.rb} +5 -3
- data/spec/mash_spec.rb +17 -0
- data/spec/ords_spec.rb +15 -0
- data/spec/{union_spec.rb → regexp_union_spec.rb} +4 -2
- data/spec/square_brackets_for_spec.rb +4 -12
- data/spec/string_extras_spec.rb +20 -0
- data/spec/string_op_spec.rb +22 -0
- data/spec/tap_spec.rb +2 -0
- data/spec/{unary_spec.rb → unary_conversion_spec.rb} +4 -2
- data/zucker.gemspec +8 -18
- metadata +75 -147
- data/.gemtest +0 -0
- data/README +0 -7
- data/desc/alias_for.yaml +0 -17
- data/desc/aliases.yaml +0 -18
- data/desc/array.yaml +0 -11
- data/desc/array_to_proc.yaml +0 -16
- data/desc/binding.yaml +0 -22
- data/desc/blank.yaml +0 -11
- data/desc/case.yaml +0 -9
- data/desc/cc.yaml +0 -10
- data/desc/class_to_proc.yaml +0 -11
- data/desc/dd.yaml +0 -23
- data/desc/egonil.yaml +0 -15
- data/desc/engine.yaml +0 -15
- data/desc/enumerable.yaml +0 -10
- data/desc/file.yaml +0 -14
- data/desc/hash.yaml +0 -14
- data/desc/hash_to_proc.yaml +0 -10
- data/desc/info.yaml +0 -20
- data/desc/iterate.yaml +0 -16
- data/desc/ivars.yaml +0 -19
- data/desc/kernel.yaml +0 -15
- data/desc/mcopy.yaml +0 -11
- data/desc/mm.yaml +0 -15
- data/desc/not.yaml +0 -11
- data/desc/oo.yaml +0 -10
- data/desc/os.yaml +0 -19
- data/desc/qq.yaml +0 -11
- data/desc/regexp_to_proc.yaml +0 -12
- data/desc/regexp_visualize.yaml +0 -23
- data/desc/ruby_version.yaml +0 -28
- data/desc/sandbox.yaml +0 -13
- data/desc/square_brackets_for.yaml +0 -22
- data/desc/string.yaml +0 -22
- data/desc/tap.yaml +0 -17
- data/desc/unary.yaml +0 -15
- data/desc/union.yaml +0 -10
- data/doc/zucker_doc.html +0 -3042
- data/lib/zucker/aliases.rb +0 -52
- data/lib/zucker/array.rb +0 -15
- data/lib/zucker/array_to_proc.rb +0 -19
- data/lib/zucker/binding.rb +0 -35
- data/lib/zucker/case.rb +0 -14
- data/lib/zucker/cc.rb +0 -31
- data/lib/zucker/class_to_proc.rb +0 -13
- data/lib/zucker/control.rb +0 -4
- data/lib/zucker/dd.rb +0 -24
- data/lib/zucker/debug.rb +0 -4
- data/lib/zucker/default.rb +0 -4
- data/lib/zucker/engine.rb +0 -70
- data/lib/zucker/enumerable.rb +0 -15
- data/lib/zucker/env.rb +0 -4
- data/lib/zucker/extensions.rb +0 -4
- data/lib/zucker/file.rb +0 -28
- data/lib/zucker/hash.rb +0 -28
- data/lib/zucker/hash_to_proc.rb +0 -17
- data/lib/zucker/info.rb +0 -193
- data/lib/zucker/ivars.rb +0 -29
- data/lib/zucker/kernel.rb +0 -42
- data/lib/zucker/mcopy.rb +0 -11
- data/lib/zucker/mm.rb +0 -41
- data/lib/zucker/not.rb +0 -20
- data/lib/zucker/object.rb +0 -4
- data/lib/zucker/oo.rb +0 -18
- data/lib/zucker/os.rb +0 -55
- data/lib/zucker/qq.rb +0 -13
- data/lib/zucker/regexp_to_proc.rb +0 -13
- data/lib/zucker/regexp_visualize.rb +0 -31
- data/lib/zucker/ruby_version.rb +0 -120
- data/lib/zucker/sandbox.rb +0 -17
- data/lib/zucker/shortcuts.rb +0 -4
- data/lib/zucker/string.rb +0 -55
- data/lib/zucker/to_proc.rb +0 -4
- data/lib/zucker/unary.rb +0 -25
- data/lib/zucker/union.rb +0 -17
- data/spec/aliases_spec.rb +0 -13
- data/spec/array_spec.rb +0 -16
- data/spec/array_to_proc_spec.rb +0 -12
- data/spec/binding_spec.rb +0 -6
- data/spec/case_spec.rb +0 -15
- data/spec/cc_spec.rb +0 -6
- data/spec/class_to_proc_spec.rb +0 -9
- data/spec/dd_spec.rb +0 -37
- data/spec/engine_spec.rb +0 -11
- data/spec/enumerable_spec.rb +0 -8
- data/spec/file_spec.rb +0 -46
- data/spec/hash_spec.rb +0 -25
- data/spec/hash_to_proc_spec.rb +0 -12
- data/spec/info_spec.rb +0 -8
- data/spec/ivars_spec.rb +0 -20
- data/spec/kernel_spec.rb +0 -42
- data/spec/mm_spec.rb +0 -6
- data/spec/not_spec.rb +0 -10
- data/spec/oo_spec.rb +0 -6
- data/spec/os_spec.rb +0 -37
- data/spec/qq_spec.rb +0 -19
- data/spec/regexp_to_proc_spec.rb +0 -9
- data/spec/regexp_visualize_spec.rb +0 -38
- data/spec/ruby_version_spec.rb +0 -55
- data/spec/sandbox_spec.rb +0 -14
- data/spec/string_spec.rb +0 -65
data/lib/zucker.rb
CHANGED
@@ -1,52 +1,22 @@
|
|
1
|
-
|
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
|
-
|
5
|
-
|
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
|
-
|
23
|
-
|
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
|
-
|
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
|
data/lib/zucker/alias_for.rb
CHANGED
@@ -1,22 +1,15 @@
|
|
1
1
|
require 'zucker'
|
2
|
-
module Zucker AliasFor = true end
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
alias aliases_for alias_for
|
3
|
+
module Zucker
|
4
|
+
module AliasFor
|
5
|
+
refine Module do
|
6
|
+
private
|
10
7
|
|
11
|
-
|
12
|
-
|
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
data/lib/zucker/blank.rb
CHANGED
@@ -1,29 +1,47 @@
|
|
1
1
|
require 'zucker'
|
2
|
-
module Zucker Blank = true end
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
}
|
26
|
-
}
|
27
|
+
refine Numeric do
|
28
|
+
def blank?() false end
|
29
|
+
end
|
27
30
|
|
28
|
-
|
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,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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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,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
|
+
|
data/lib/zucker/iterate.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
1
|
require 'zucker'
|
2
|
-
module Zucker Iterate = true end
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|