tagen 1.1.7 → 2.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 (92) hide show
  1. data/.gitignore +4 -1
  2. data/.travis.yml +5 -0
  3. data/CHANGELOG +6 -0
  4. data/Gemfile +2 -3
  5. data/Gemfile.lock +13 -15
  6. data/README.md +20 -39
  7. data/Rakefile +2 -1
  8. data/benchmark/a.rb +14 -0
  9. data/lib/tagen/core.rb +17 -22
  10. data/lib/tagen/core/array.rb +5 -109
  11. data/lib/tagen/core/array/append.rb +3 -0
  12. data/lib/tagen/core/array/delete_values.rb +43 -0
  13. data/lib/tagen/core/array/extract_options.rb +35 -94
  14. data/lib/tagen/core/class.rb +1 -0
  15. data/lib/tagen/core/enumerable.rb +15 -18
  16. data/lib/tagen/core/enumerator.rb +22 -12
  17. data/lib/tagen/core/exception.rb +5 -3
  18. data/lib/tagen/core/file.rb +1 -0
  19. data/lib/tagen/core/hash.rb +4 -39
  20. data/lib/tagen/core/integer.rb +2 -0
  21. data/lib/tagen/core/io.rb +59 -26
  22. data/lib/tagen/core/kernel.rb +5 -64
  23. data/lib/tagen/core/kernel/deprecate.rb +5 -0
  24. data/lib/tagen/core/kernel/platform.rb +46 -0
  25. data/lib/tagen/core/kernel/shell.rb +45 -0
  26. data/lib/tagen/core/module.rb +9 -11
  27. data/lib/tagen/core/numeric.rb +4 -2
  28. data/lib/tagen/core/object.rb +6 -21
  29. data/lib/tagen/core/process.rb +6 -4
  30. data/lib/tagen/core/re.rb +2 -2
  31. data/lib/tagen/core/string.rb +12 -47
  32. data/lib/tagen/core/time.rb +30 -38
  33. data/lib/tagen/erb.rb +10 -8
  34. data/lib/tagen/net/http.rb +33 -29
  35. data/lib/tagen/pathname.rb +1 -5
  36. data/lib/tagen/rbconfig.rb +8 -0
  37. data/lib/tagen/socket.rb +8 -2
  38. data/lib/tagen/uri.rb +9 -0
  39. data/lib/tagen/version.rb +1 -1
  40. data/lib/tagen/vim.rb +8 -8
  41. data/lib/tagen/xmpp4r.rb +23 -23
  42. data/spec/spec_helper.rb +23 -17
  43. data/spec/tagen/core/array/delete_values_spec.rb +19 -0
  44. data/spec/tagen/core/array/extract_options_spec.rb +9 -29
  45. data/spec/tagen/core/enumerable_spec.rb +9 -0
  46. data/spec/tagen/core/enumerator_spec.rb +10 -12
  47. data/spec/tagen/core/exception_spec.rb +14 -9
  48. data/spec/tagen/core/io_spec.rb +20 -0
  49. data/spec/tagen/core/kernel/platform_spec.rb +92 -0
  50. data/spec/tagen/core/kernel/shell_spec.rb +27 -0
  51. data/spec/tagen/core/numeric_spec.rb +10 -0
  52. data/spec/tagen/core/process_spec.rb +14 -0
  53. data/spec/tagen/core/re_spec.rb +9 -0
  54. data/spec/tagen/core/string_spec.rb +9 -0
  55. data/spec/tagen/core/time_spec.rb +6 -51
  56. data/spec/tagen/erb_spec.rb +3 -10
  57. data/spec/tagen/pathname_spec.rb +11 -0
  58. data/spec/tagen/rbconfig_spec.rb +11 -0
  59. data/spec/tagen/socket_spec.rb +7 -5
  60. data/spec/tagen/uri_spec.rb +13 -0
  61. data/tagen.gemspec +3 -4
  62. metadata +45 -51
  63. data/TODO +0 -1
  64. data/docs/Architecture.md +0 -17
  65. data/docs/CoreExtensions.md +0 -40
  66. data/docs/ExtraExtensions.md +0 -20
  67. data/lib/tagen/RMagick.rb +0 -12
  68. data/lib/tagen/audioinfo.rb +0 -22
  69. data/lib/tagen/cairo.rb +0 -811
  70. data/lib/tagen/core/extend_hash.rb +0 -46
  71. data/lib/tagen/core/marshal.rb +0 -34
  72. data/lib/tagen/core/open_option.rb +0 -161
  73. data/lib/tagen/core/string/pyformat.rb +0 -336
  74. data/lib/tagen/core/symbol.rb +0 -8
  75. data/lib/tagen/date.rb +0 -80
  76. data/lib/tagen/gdk_pixbuf2.rb +0 -26
  77. data/lib/tagen/gtk2.rb +0 -122
  78. data/lib/tagen/ncurses.rb +0 -246
  79. data/lib/tagen/poppler.rb +0 -48
  80. data/lib/tagen/tree.rb +0 -77
  81. data/lib/tagen/xmpp4r/roster.rb +0 -20
  82. data/lib/tagen/yaml.rb +0 -38
  83. data/spec/tagen/cairo_spec.rb +0 -137
  84. data/spec/tagen/core/array_spec.rb +0 -42
  85. data/spec/tagen/core/extend_hash_spec.rb +0 -54
  86. data/spec/tagen/core/hash_spec.rb +0 -10
  87. data/spec/tagen/core/module_spec.rb +0 -14
  88. data/spec/tagen/core/open_option_spec.rb +0 -83
  89. data/spec/tagen/core/string/pyformat_spec.rb +0 -98
  90. data/spec/tagen/core/symbol_spec.rb +0 -15
  91. data/spec/tagen/core_spec.rb +0 -6
  92. data/spec/tagen/date_spec.rb +0 -146
@@ -0,0 +1 @@
1
+ require "active_support/core_ext/class"
@@ -1,20 +1,17 @@
1
- =begin
2
-
3
- == enumerate it
4
-
5
- * collect{k,v} Array#map!
6
- * each_cons(n),{}=>nil each_slice
7
-
8
- == retrive value
9
- * [slice]
10
- * first/last(),(n) Array#first!
11
- * find{} find_index(v),{} find_all Array#find! Array#find_all!
12
- * grep(obj),{=>v}
13
-
14
- * Array#delete(v) delete_at(i) clear()
15
-
16
- =end
17
1
  module Enumerable
18
-
19
-
2
+ # Returns an array of every element in enum for each pattern === element
3
+ #
4
+ # @example
5
+ #
6
+ # a = %w[aa ab bb cc]
7
+ # a.grep_values(/a./, "bb") -> ["aa", "ab", "bb"]
8
+ #
9
+ # @return [Array]
10
+ def grep_values(*patterns, &blk)
11
+ patterns.each.with_object([]) {|pat,m|
12
+ m.push *grep(pat, &blk)
13
+ }
14
+ end
20
15
  end
16
+
17
+ require "active_support/core_ext/enumerable"
@@ -1,11 +1,18 @@
1
1
  class Enumerator
2
- # combine with_index and with_object
3
- # @see with_index, with_object
2
+ # Combine with_index and with_object.
4
3
  #
5
4
  # @example
6
- # (1..7).each.with_iobject([]){|(*args), idx, memo|}
7
- # (1..7).each.with_iobject(2, []){|(*args), idx, memo|} # offset is 2
8
- #
5
+ #
6
+ # %w[a b].each.with_iobject([]){|v, i, memo|
7
+ # memo << [v, i]
8
+ # }
9
+ # -> [a, 0], [b, 1]
10
+ #
11
+ # %w[a b].each.with_iobject(2, []){|v, i, memo|
12
+ # memo << [v, i]
13
+ # }
14
+ # -> [a, 2], [b, 3]
15
+ #
9
16
  # @overload with_iobject(*args)
10
17
  # @param [Fixnum,Object] *args pass Fixnum as offset, otherwise as memo_obj
11
18
  # @return Enumerator
@@ -14,16 +21,19 @@ class Enumerator
14
21
  # @yieldparam [Object] (*args)
15
22
  # @yieldparam [Fixnum] idx index
16
23
  # @yieldparam [Object] memo_obj
17
- def with_iobject *args, &blk
24
+ #
25
+ # @see with_index
26
+ # @see with_object
27
+ def with_iobject(*args, &blk)
18
28
  return self.to_enum(:with_iobject, *args) unless blk
19
29
 
20
- offset = args.find!{|v| Fixnum===v} || 0
21
- raise ArgumentError "must provide memo_obj" if args.empty?
22
- memo = args[0]
30
+ (offset,), (memo,) = args.partition{|v| Fixnum === v}
31
+ index = offset || 0
32
+ raise ArgumentError, "must provide memo object" unless memo
23
33
 
24
- i = offset-1
25
- self.with_object memo do |args, m|
26
- blk.call args,i+=1,m
34
+ with_object(memo) do |args2, m|
35
+ blk.call args2, index, m
36
+ index += 1
27
37
  end
28
38
  end
29
39
  end
@@ -1,16 +1,18 @@
1
- # +exit code+ for shell programming.
1
+ # +exit_code+ for shell programming.
2
2
  #
3
3
  # @example
4
4
  #
5
5
  # MyError = Class.new(Exception)
6
6
  # MyError.exit_code = 1
7
7
  #
8
- # class MyError2 < Exception; @@eixt_code=1 end
8
+ # class MyError2 < Exception
9
+ # @@eixt_code=1
10
+ # end
9
11
  #
10
12
  # begin
11
13
  # ...
12
14
  # rescue MyError => e
13
- # p e.exit_code #=> 1
15
+ # p e.exit_code -> 1
14
16
  # end
15
17
  class Exception
16
18
  class << self
@@ -0,0 +1 @@
1
+ require "tagen/core/io"
@@ -1,39 +1,4 @@
1
- class Hash
2
- alias :original_delete :delete
3
-
4
- # support delete more than one keys
5
- #
6
- # original: delete(key)
7
- # current: delete(*keys)
8
- #
9
- # return [Hash]
10
- def delete *keys, &blk
11
- values = keys.each.with_object [] do |k,m|
12
- m << original_delete(k, &blk)
13
- end
14
- keys.length==1 ? values[0] : values
15
- end
16
-
17
- # confict with awesome_print which extend Array#grep
18
- #
19
- # grep pat at hash's keys, and return a new hash.
20
- # @see Array#grep
21
- #
22
- # @example
23
- #
24
- # foo = {a: 1, b: 2}
25
- # foo.grep(:a) #=> {a: 1}
26
- #
27
- # @return [Hash]
28
- #def grep(pat_s)
29
- # pats = Array===pat_s ? pat_s : [pat_s]
30
- #
31
- # filtered_keys = pats.each.with_object([]) { |pat, memo|
32
- # memo.push *self.keys.grep(pat)
33
- # }
34
- # filtered_keys.each.with_object({}) { |k,memo|
35
- # memo[k] = self[k]
36
- # }
37
- # end
38
-
39
- end # class Hash
1
+ require "active_support/core_ext/hash/keys"
2
+ require "active_support/core_ext/hash/slice"
3
+ require "active_support/core_ext/hash/deep_dup"
4
+ require "active_support/core_ext/hash/deep_merge"
@@ -0,0 +1,2 @@
1
+ require "active_support/core_ext/integer/multiple"
2
+ require "active_support/core_ext/integer/inflections"
@@ -1,29 +1,62 @@
1
- =begin
2
- Additional Method list
3
- ----------------------
1
+ require "tagen/core/array/extract_options"
4
2
 
5
- * `#fd` _alias from fileno_
6
-
7
- =end
8
3
  class IO
4
+ class << self
5
+ # Extends IO#write with :mkdir option
6
+ #
7
+ # @overload write(name, string, [offset], [open_args])
8
+ # @param [Hash] open_args
9
+ # @option open_args [Boolean] :mkdir auto create missing directory in path
10
+ def write_with_tagen(name, string, *args)
11
+ (offset,), o = args.extract_options
12
+ mkdir_p(File.dirname(name)) if o[:mkdir]
13
+
14
+ write_without_tagen(name, string, offset, o)
15
+ end
16
+
17
+ alias write_without_tagen write
18
+ alias write write_with_tagen
19
+
20
+ # A convient method to append a file.
21
+ #
22
+ # @overload append(name, string, [offset], [open_args])
23
+ # @param [Hash] open_args
24
+ # @option open_args [Boolean] :mkdir auto create missing directory in path
25
+ #
26
+ # @example
27
+ #
28
+ # File.append("/tmp/a", "hello world")
29
+ #
30
+ # @see write
31
+ def append(name, string, *args)
32
+ (offset,), o = args.extract_options
33
+ o[:mode] = "a"
34
+
35
+ write(name, string, offset, o)
36
+ end
37
+
38
+ private
39
+
40
+ # @param [Hash] o options
41
+ # @option o [Fixnum] :mode (0775)
42
+ def mkdir_p(path, o={})
43
+ return if File.exists?(path)
44
+
45
+ o[:mode] ||= 0775
46
+
47
+ stack = []
48
+ until path == stack.last
49
+ break if File.exists?(path)
50
+ stack << path
51
+ path = File.dirname(path)
52
+ end
53
+
54
+ stack.reverse.each do |p|
55
+ Dir.mkdir(p)
56
+ File.chmod(o[:mode], p)
57
+ end
58
+ end
59
+ end
9
60
 
10
- # a convient function to write text.
11
- #
12
- # use open(path, "w"){|f| f.write(text)}
13
- #
14
- # @param [String] path
15
- # @param [String] text
16
- # @return [String]
17
- def self.write(path, text) open(path, "w"){|f| f.write(text)} end
18
-
19
- # a convient function to append text.
20
- #
21
- # use open(path, "a"){|f| f.write(text)}
22
- #
23
- # @param [String] path
24
- # @param [String] text
25
- # @return [String]
26
- def self.append(path, text) open(path, "a"){|f| f.write(text)} end
27
-
28
- alias fd fileno
29
- end # class IO
61
+ alias fd fileno
62
+ end
@@ -1,64 +1,5 @@
1
- module Kernel
2
- private
3
-
4
- # like `cmd`, but with option support.
5
- #
6
- # @overload sh(cmd, o={})
7
- # @param [String] cmd a shell command
8
- # @param [Symbol, Hash] o support {Array#extract_extend_options}
9
- # @option o [Boolean] :verbose print cmd if verbose
10
- # @return [String] result
11
- def sh(cmd, *args)
12
- o = args.extract_extend_options!
13
- puts cmd if o[:verbose]
14
- `#{cmd}`
15
- end
16
-
17
- alias original_system system
18
-
19
- # like Builtin system, but add option support
20
- #
21
- # @overload system(cmd, o={})
22
- # @param [String] cmd
23
- # @param [Symbol, Hash] o support {Array#extract_extend_options}
24
- # @option o [Boolean] :verbose print cmd if verbose
25
- # @return [Boolean,nil] true false nil
26
- def system(*cmds)
27
- o = cmds.extract_extend_options!
28
- cmd = cmds.join(" ")
29
- puts cmd if o[:verbose]
30
- original_system cmd
31
- end
32
-
33
- # convert block to method.
34
- #
35
- # you can call a block with arguments
36
- #
37
- # @example USAGE
38
- # instance_eval(&blk)
39
- # blk2method(&blk).call *args
40
- #
41
- def blk2method(&blk)
42
- self.class.class_eval {
43
- define_method(:__blk2method, &blk)
44
- }
45
- method(:__blk2method)
46
- end
47
-
48
- # detect Platform information.
49
- #
50
- # RUBY_PLATFORM is "i686-linux" "i386-migw32"
51
- #
52
- # @return [Boolean]
53
- def linux?
54
- RUBY_PLATFORM =~ /linux/
55
- end
56
-
57
- # detect PLatform information.
58
- #
59
- # @return [Boolean]
60
- # @see {#linux?}
61
- def win32?
62
- RUBY_PLATFORM =~ /mingw32|mswin/
63
- end
64
- end # module Kernel
1
+ require "tagen/core/kernel/shell"
2
+ require "tagen/core/kernel/deprecate"
3
+ require "tagen/core/kernel/platform"
4
+ require "active_support/core_ext/kernel/reporting"
5
+ require "active_support/core_ext/kernel/agnostics"
@@ -0,0 +1,5 @@
1
+ module Kernel
2
+ def deprecate(msg)
3
+ puts "Deprecate: #{msg}"
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ require "rbconfig"
2
+
3
+ module Kernel
4
+ private
5
+ # Return true when ruby is running in Linux Operation System.
6
+ def linux?
7
+ RbConfig::CONFIG["host_os"] =~ /linux|cygwin/
8
+ end
9
+
10
+ # Return true when ruby is running in Mac OS X.
11
+ def mac?
12
+ RbConfig::CONFIG["host_os"] =~ /mac|darwin/
13
+ end
14
+
15
+ # Return true when ruby is running in BSD Operation System.
16
+ def bsd?
17
+ RbConfig::CONFIG["host_os"] =~ /bsd/
18
+ end
19
+
20
+ # Return true when ruby is running in Windows Operation System.
21
+ def windows?
22
+ RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
23
+ end
24
+
25
+ # Return true when ruby is running in Solaris Windows Operation System.
26
+ def solaris?
27
+ RbConfig::CONFIG["host_os"] =~ /solaris|sunos/
28
+ end
29
+
30
+ # Return true when ruby is running in symbian.
31
+ #
32
+ # TODO: who knows what symbian returns?
33
+ def symbian?
34
+ RbConfig::CONFIG["host_os"] =~ /symbian/
35
+ end
36
+
37
+ # Return true when ruby is running in Posix-compablity Operation System.
38
+ def posix?
39
+ linux? or mac? or bsd? or solaris? or begin
40
+ fork do end
41
+ true
42
+ rescue NotImplementedError, NoMethodError
43
+ false
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ module Kernel
2
+ private
3
+
4
+ # run `cmd` with :show_cmd option.
5
+ #
6
+ # @overload sh(cmd, o={})
7
+ # @param [String] cmd a shell command
8
+ # @param [Hash] o
9
+ # @option o [Boolean] :show_cmd print cmd if true
10
+ # @return [String] result
11
+ #
12
+ # @see Kernel#`
13
+ def sh(cmd, *args)
14
+ o = args.last.instance_of?(Hash) ? args.pop : {}
15
+ puts cmd if o[:show_cmd]
16
+ `#{cmd}`
17
+ end
18
+
19
+ # Extends the default Kernel#system with :show_cmd option.
20
+ #
21
+ # @overload system([env,] command...[, options])
22
+ # @option options [Boolean] :show_cmd print cmd if true
23
+ #
24
+ # @see Kernel#system
25
+ def system_with_tagen(*args)
26
+ o = args.last.instance_of?(Hash) ? args.pop : {}
27
+ if Hash === args[0] then
28
+ env, *cmds = args
29
+ else
30
+ cmds = args
31
+ end
32
+
33
+ if o[:show_cmd]
34
+ puts cmds.join(" ")
35
+ o.delete(:show_cmd)
36
+ end
37
+
38
+ system_without_tagen *args, o
39
+ end
40
+
41
+ alias system_without_tagen system
42
+ alias system system_with_tagen
43
+ end
44
+
45
+ $sudo = Process.pid != 0 && system("which sudo &>/dev/null") ? "sudo" : ""
@@ -1,11 +1,9 @@
1
- class Module
2
- # return hash instead of names
3
- # @see constants
4
- #
5
- # @return [Hash] {key: value}
6
- def consts
7
- constants.each.with_object ({}) do |k,m|
8
- m[k] = const_get(k)
9
- end
10
- end
11
- end #class Module
1
+ require "active_support/core_ext/module/aliasing"
2
+ require "active_support/core_ext/module/introspection"
3
+ require "active_support/core_ext/module/anonymous"
4
+ require "active_support/core_ext/module/reachable"
5
+ require "active_support/core_ext/module/attribute_accessors"
6
+ require "active_support/core_ext/module/attr_internal"
7
+ require "active_support/core_ext/module/delegation"
8
+ require "active_support/core_ext/module/remove_method"
9
+ require "active_support/core_ext/module/qualified_const"
@@ -1,10 +1,12 @@
1
1
  class Numeric
2
- # equivalent to a.fdiv(b).ceil. +1
2
+ # equivalent to `a.fdiv(b).ceil`. +1
3
3
  #
4
4
  # @param [Numeric] num
5
5
  # @return [Numeric]
6
- def div2 num
6
+ def div2(num)
7
7
  d, m = divmod(num)
8
8
  d + (m==0 ? 0 : 1)
9
9
  end
10
10
  end
11
+
12
+ require "active_support/core_ext/numeric/bytes"