tagen 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +0 -1
- data/LICENSE +20 -0
- data/README.md +17 -18
- data/Rakefile +5 -2
- data/docs/CoreExtensions.md +17 -3
- data/lib/tagen/core.rb +3 -0
- data/lib/tagen/core/array/extract_options.rb +64 -0
- data/lib/tagen/core/kernel.rb +25 -36
- data/lib/tagen/core/string/pyformat.rb +33 -26
- data/spec/spec_helper.rb +1 -0
- data/spec/{cairo_spec.rb → tagen/cairo_spec.rb} +0 -0
- data/spec/tagen/core/array/extract_options_spec.rb +42 -0
- data/spec/{core → tagen/core}/pa/cmd_spec.rb +1 -1
- data/spec/{core → tagen/core}/pa/dir_spec.rb +1 -1
- data/spec/{core → tagen/core}/string/pyformat_spec.rb +0 -1
- data/tagen.gemspec +1 -0
- data/tagen.watchr +21 -0
- data/version.rb +1 -1
- metadata +20 -6
data/.yardopts
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Guten
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
Tagen, a core and extra extension to Ruby library.
|
2
2
|
==========================================
|
3
|
+
|
4
|
+
**Homepage**: [https://github.com/GutenLinux/tagen](https://github.com/GutenLinux/tagen) <br/>
|
5
|
+
**Author**: Guten <br/>
|
6
|
+
**Contributors**: See Contributors section below <br/>
|
7
|
+
**License**: MIT License <br/>
|
8
|
+
**Documentation**: [http://rubydoc.info/gems/tagen/frames](http://rubydoc.info/gems/tagen/frames) <br/>
|
9
|
+
**Issue Tracker**: [https://github.com/GutenLinux/tagen/issues](https://github.com/GutenLinux/tagen/issues) <br/>
|
10
|
+
|
11
|
+
Overview
|
12
|
+
--------
|
13
|
+
|
3
14
|
Ruby has an 'Open Class' feature, so we can extend any class by ourself.
|
4
15
|
|
5
16
|
This library provides some usefull Ruby core extension. some comes from ActiveSupport. ActiveSupport is mainly target to Rails, but tagen is target to generic ruby development, and tagen is smaller. It is a colletion of most common core,extra extensions.
|
@@ -35,26 +46,14 @@ Install
|
|
35
46
|
|
36
47
|
Contributing
|
37
48
|
-------------
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
See Also
|
44
|
-
--------
|
45
|
-
**ActiveSupport** [ActiveSupport Core Extension Guide](http://edgeguides.rubyonrails.org/active_support_core_extensions.html)
|
46
|
-
|
47
|
-
|
48
|
-
Info & Links
|
49
|
-
------------
|
50
|
-
**Homepage**: [http://github.com/GutenLinux/tagen](http://github.com/GutenLinux/tagen)
|
51
|
-
**Author**: Guten
|
52
|
-
**API-Docs**: yard
|
53
|
-
**Bugs**: github
|
49
|
+
* report bugs/featues to issue tracker.
|
50
|
+
* fork it and pull a request.
|
51
|
+
* improve documentation.
|
52
|
+
* any ideas are welcome.
|
54
53
|
|
55
54
|
Contributors
|
56
|
-
|
55
|
+
------------
|
57
56
|
|
58
57
|
Copyright
|
59
58
|
---------
|
60
|
-
Copyright © 2011 by Guten. this library released under MIT-License, See {file:
|
59
|
+
Copyright © 2011 by Guten. this library released under MIT-License, See {file:LICENSE} for futher details.
|
data/Rakefile
CHANGED
data/docs/CoreExtensions.md
CHANGED
@@ -10,13 +10,18 @@ From Tagen
|
|
10
10
|
----------
|
11
11
|
* {Object#deepdup}
|
12
12
|
|
13
|
+
* {Kernel}
|
14
|
+
* `#linux? win32?`
|
15
|
+
* `sh`
|
16
|
+
* `blk2method`
|
17
|
+
|
13
18
|
* {Numeric#div2}
|
14
19
|
|
15
20
|
* {String}.hexdigits octdigits letters uppercase lowercase
|
16
21
|
|
17
22
|
* {Array}
|
18
23
|
* `#extract_extend_options! extract_options!`
|
19
|
-
*
|
24
|
+
* `#append`
|
20
25
|
* `#delete(*values) delete_at` _support delete more than one values_
|
21
26
|
|
22
27
|
* {Hash#delete}
|
@@ -29,10 +34,19 @@ From Tagen
|
|
29
34
|
|
30
35
|
* {Marshal}.load dump _add Pa support_
|
31
36
|
|
32
|
-
* {Pa} _a path lib_
|
33
|
-
|
34
37
|
* {MatchData#to_hash}
|
35
38
|
|
39
|
+
* {Pa} _a path library_
|
40
|
+
|
41
|
+
* {PyFormat} _a string format libraray_
|
42
|
+
|
43
|
+
From [pd][1]
|
44
|
+
--------
|
45
|
+
|
46
|
+
* Kernel#pd phr
|
47
|
+
|
48
|
+
[1]: http://rubydoc.info/gems/pd/frames/Kernel
|
49
|
+
|
36
50
|
From ActiveSupport
|
37
51
|
------------------
|
38
52
|
see [ActiveSupport Core Extensions Guide](http://edgeguides.rubyonrails.org/active_support_core_extensions.html)
|
data/lib/tagen/core.rb
CHANGED
@@ -1,4 +1,68 @@
|
|
1
1
|
class Array
|
2
|
+
|
3
|
+
# extend options are symbols and hash, symbol as a boolean option.
|
4
|
+
#
|
5
|
+
# :a #=> { a: true }
|
6
|
+
# :_a #=> { a: false}
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# def foo(*args)
|
10
|
+
# paths, o = args.extract_extend_options
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# foo(1, :a, :_b, :c => 2)
|
14
|
+
# #=> paths is [1]
|
15
|
+
# #=> o is {a: true, b: false, c: 2}
|
16
|
+
#
|
17
|
+
# @param [Symbol, Hash] *defaults
|
18
|
+
# @return [Array<Object>, Hash] \[args, options]
|
19
|
+
def extract_extend_options *defaults
|
20
|
+
args, o = _parse_o(defaults)
|
21
|
+
args1, o1 = _parse_o(self)
|
22
|
+
[args+args1, o.merge(o1)]
|
23
|
+
end
|
24
|
+
|
25
|
+
# modify args IN PLACE.
|
26
|
+
# @ see extract_extend_options
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
# def foo(*args)
|
30
|
+
# options = args.extract_extend_options!
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# foo(1, :a, :_b, c: 2)
|
34
|
+
# #=> args is [1]
|
35
|
+
# #=> o is {a: true, b: false, c:2}
|
36
|
+
#
|
37
|
+
# @param [Symbol, Hash] *defaults
|
38
|
+
# @return [Hash] options
|
39
|
+
def extract_extend_options! *defaults
|
40
|
+
args, o = extract_extend_options *defaults
|
41
|
+
self.replace args
|
42
|
+
o
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [Array,Hash] args
|
46
|
+
def _parse_o args
|
47
|
+
args = args.dup
|
48
|
+
# name:1
|
49
|
+
o1 = Hash === args.last ? args.pop : {}
|
50
|
+
# :force :_force
|
51
|
+
rst = args.select{|v| Symbol===v}
|
52
|
+
args.delete_if{|v| Symbol===v}
|
53
|
+
o2={}
|
54
|
+
rst.each do |k|
|
55
|
+
v = true
|
56
|
+
if k=~/^_/
|
57
|
+
k = k[1..-1].to_sym
|
58
|
+
v = false
|
59
|
+
end
|
60
|
+
o2[k] = v
|
61
|
+
end
|
62
|
+
[args, o1.merge(o2)]
|
63
|
+
end
|
64
|
+
private :_parse_o
|
65
|
+
|
2
66
|
# Extracts options from a set of arguments. Removes and returns the last
|
3
67
|
# element in the array if it's a hash, otherwise returns a blank hash.
|
4
68
|
# you can also pass a default option.
|
data/lib/tagen/core/kernel.rb
CHANGED
@@ -1,17 +1,35 @@
|
|
1
1
|
module Kernel
|
2
2
|
private
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# @
|
7
|
-
#
|
8
|
-
#
|
9
|
-
|
10
|
-
|
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!
|
11
13
|
puts cmd if o[:verbose]
|
12
14
|
`#{cmd}`
|
13
15
|
end
|
14
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 = args.extract_extend_options!
|
28
|
+
cmd = cmds.join(" ")
|
29
|
+
puts cmd if o[:verbose]
|
30
|
+
original_system cmd
|
31
|
+
end
|
32
|
+
|
15
33
|
# convert block to method.
|
16
34
|
#
|
17
35
|
# you can call a block with arguments
|
@@ -41,33 +59,4 @@ private
|
|
41
59
|
# @see {#linux?}
|
42
60
|
def win32?; RUBY_PLATFORM=~/mingw32|mswin/ end
|
43
61
|
|
44
|
-
|
45
|
-
# *for debug* pd(print debug), search 'pd' is much easier than 'p' in a text-editor.
|
46
|
-
#
|
47
|
-
# like p, but use " " in each argument instead of "\n".
|
48
|
-
#
|
49
|
-
# @example
|
50
|
-
# p 1,2
|
51
|
-
# =>
|
52
|
-
# 1
|
53
|
-
# 2
|
54
|
-
# pd 1,2
|
55
|
-
# =>
|
56
|
-
# 1 2
|
57
|
-
#
|
58
|
-
# @param [Object] *args
|
59
|
-
# @return nil
|
60
|
-
def pd *args
|
61
|
-
args.each do |arg| print arg.inspect," " end
|
62
|
-
print "\n"
|
63
|
-
end
|
64
|
-
|
65
|
-
# *for debug* print hr. puts '='*14 + " #{name}"
|
66
|
-
#
|
67
|
-
# sometime, we just need a horizonal line to separate message for debug.
|
68
|
-
# @param [String] name
|
69
|
-
def phr name=nil
|
70
|
-
puts '='*14 + " #{name}"
|
71
|
-
end
|
72
|
-
|
73
62
|
end # module Kernel
|
@@ -1,48 +1,55 @@
|
|
1
|
-
=begin
|
1
|
+
=begin rdoc
|
2
|
+
|
2
3
|
== Overview
|
4
|
+
|
3
5
|
a python like string format libraray.
|
4
6
|
|
5
|
-
1. "this is #{guten}" # Ruby Builtin
|
7
|
+
1. "this is #{guten}" # Ruby Builtin
|
6
8
|
2. "this is %s" % "guten" # Ruby Builtin
|
7
9
|
3. "this is {guten}".format(guten: 'x')
|
8
10
|
|
9
11
|
use "#{var}" is easy and quick in many cases, but some times we need a more powerful format support.
|
10
|
-
|
11
|
-
|
12
|
+
|
13
|
+
"I like %s and %s" % %(apple, football)
|
14
|
+
"I like {fruit} and {sport}".format(%w(apple football)) # it has semantic meaning.
|
12
15
|
|
13
16
|
== Usage
|
14
|
-
|
15
|
-
|
17
|
+
|
18
|
+
require "tagen/core"
|
19
|
+
"it costs {:.2f} dollar".format(1.123) #=> "it costs 1.12 dollar"
|
16
20
|
|
17
21
|
* support abritry-argument or hash-argument
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
"{} {}".format(1,2) #=> "1 2"
|
23
|
+
"{a} {b}".format(a:1, b:2) #=> "1 2"
|
24
|
+
"{a} {b}".format(1, b:2) #=> "1 2"
|
21
25
|
|
22
26
|
* escape
|
23
|
-
|
27
|
+
"my {{name}} is {name}".format("guten") #=> my name is guten.
|
24
28
|
|
25
29
|
== Examples
|
26
|
-
|
27
|
-
|
30
|
+
|
31
|
+
"{:.2f}"
|
32
|
+
"{name:.2f}"
|
28
33
|
|
29
34
|
== Specification
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
35
|
+
|
36
|
+
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
|
37
|
+
fill ::= <a character other than '}'> default is " "
|
38
|
+
align ::= "<" | ">" | "=" | "^" default is >. = is padding after sign. eg. +000000120
|
39
|
+
sign ::= "+" | "-" | " "
|
40
|
+
# ::= <prefix 0b 0o 0x>
|
41
|
+
0 ::= <zero_padding> equal to fill is 0
|
42
|
+
, ::= <comma_sep> also type n
|
43
|
+
precision ::= string truncate with
|
44
|
+
type ::= s c b o d x X f/F g/G e/E n %
|
45
|
+
|
46
|
+
f/F fixed point. nan/NAN inf/INF
|
47
|
+
e/E exponent notation.
|
48
|
+
g/G gernal format. 1.0 => 1
|
49
|
+
n number. thounds sep based on local setting
|
44
50
|
|
45
51
|
== Resources
|
52
|
+
|
46
53
|
* http://docs.python.org/py3k/library/string.html#formatstrings
|
47
54
|
|
48
55
|
=end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require "tagen/core"
|
File without changes
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#extract_options" do
|
5
|
+
it "returns [args, option]" do
|
6
|
+
args = [1, a:1, b:2]
|
7
|
+
nums, o = args.extract_options
|
8
|
+
nums.should == [1]
|
9
|
+
o.should == {a:1, b:2}
|
10
|
+
args.should == [1, a:1, b:2]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#extract_options!" do
|
15
|
+
it "returns option" do
|
16
|
+
args = [1, a:1, b:2]
|
17
|
+
o = args.extract_options!
|
18
|
+
o.should == {a:1, b:2}
|
19
|
+
args.should == [1]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#extract_extend_options" do
|
24
|
+
|
25
|
+
it "returns [args, option]" do
|
26
|
+
args = [1, :a, :_b, c:2]
|
27
|
+
nums, o = args.extract_extend_options
|
28
|
+
nums.should == [1]
|
29
|
+
o.should == {a:true, b:false, c:2}
|
30
|
+
args.should == [1, :a, :_b, c:2]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#extract_extend_options!" do
|
35
|
+
it "modify args and returns option only" do
|
36
|
+
args = [1, :a, :_b, c:2]
|
37
|
+
o = args.extract_extend_options!
|
38
|
+
args.should == [1]
|
39
|
+
o.should == {a:true, b:false, c:2}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/tagen.gemspec
CHANGED
data/tagen.watchr
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# lib/**/*.rb
|
2
|
+
watch %r~lib/(.*)\.rb~ do |m|
|
3
|
+
test "spec/#{m[1]}_spec.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
# spec/**/*_spec.rb
|
7
|
+
watch %r~spec/.*_spec\.rb~ do |m|
|
8
|
+
test m[0]
|
9
|
+
end
|
10
|
+
|
11
|
+
# Ctrl-\
|
12
|
+
Signal.trap('QUIT') do
|
13
|
+
puts "--- Running all tests ---\n\n"
|
14
|
+
test "spec"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test path
|
18
|
+
cmd = "rspec --color #{path}"
|
19
|
+
puts cmd
|
20
|
+
system cmd
|
21
|
+
end
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: tagen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Guten
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-10 00:00:00 +08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -24,6 +24,17 @@ dependencies:
|
|
24
24
|
version: "0"
|
25
25
|
type: :runtime
|
26
26
|
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pd
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
27
38
|
description: |
|
28
39
|
an extension to ruby core and gem library. use some active_support/core_ext, but smaller than active_support. active_support is mainly target to Rails, but tagen is target to generic ruby development.
|
29
40
|
|
@@ -39,6 +50,7 @@ files:
|
|
39
50
|
- .yardopts
|
40
51
|
- Gemfile
|
41
52
|
- Gemfile.lock
|
53
|
+
- LICENSE
|
42
54
|
- README.md
|
43
55
|
- Rakefile
|
44
56
|
- docs/Architecture.md
|
@@ -78,12 +90,14 @@ files:
|
|
78
90
|
- lib/tagen/vim.rb
|
79
91
|
- lib/tagen/xmpp4r.rb
|
80
92
|
- lib/tagen/xmpp4r/roster.rb
|
81
|
-
- spec/cairo_spec.rb
|
82
|
-
- spec/core/pa/cmd_spec.rb
|
83
|
-
- spec/core/pa/dir_spec.rb
|
84
|
-
- spec/core/string/pyformat_spec.rb
|
85
93
|
- spec/spec_helper.rb
|
94
|
+
- spec/tagen/cairo_spec.rb
|
95
|
+
- spec/tagen/core/array/extract_options_spec.rb
|
96
|
+
- spec/tagen/core/pa/cmd_spec.rb
|
97
|
+
- spec/tagen/core/pa/dir_spec.rb
|
98
|
+
- spec/tagen/core/string/pyformat_spec.rb
|
86
99
|
- tagen.gemspec
|
100
|
+
- tagen.watchr
|
87
101
|
- version.rb
|
88
102
|
has_rdoc: true
|
89
103
|
homepage: http://github.com/GutenLinux/tagen
|