tagen 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
- source :rubygems
1
+ source "http://rubygems.org"
2
2
 
3
3
  group :development do
4
4
  gem "rspec"
5
+ gem "watchr"
5
6
  end
6
7
 
7
- gemspec
8
+ #gemspec
data/README.md CHANGED
@@ -8,10 +8,11 @@ Tagen, a core and extra extension to Ruby library.
8
8
  | Documentation: | http://rubydoc.info/gems/tagen/frames |
9
9
  | Issue Tracker: | https://github.com/GutenYe/tagen/issues |
10
10
 
11
-
12
11
  Overview
13
12
  --------
14
13
 
14
+ extensions defined in "active_support" will not appear in here, so you can use it with "active_support".
15
+
15
16
  Ruby has an 'Open Class' feature, so we can extend any class by ourself.
16
17
 
17
18
  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.
@@ -20,20 +21,16 @@ This library comes with a string format lib named {PyFormat}.
20
21
 
21
22
  Usage
22
23
  -----
24
+
23
25
  use core extension
24
26
 
25
27
  require "tagen/core"
26
28
 
27
- then we have String#blank? method, for a list of core extensions, see {file:docs/CoreExtensions.md docs/CoreExtensions}.
29
+ then we have Time.time method, for a list of core extensions, see {file:docs/CoreExtensions.md docs/CoreExtensions}.
28
30
 
29
31
  use extra extension
30
32
 
31
- require "pathname"
32
- require "tagen/pathname"
33
-
34
- or
35
-
36
- require "tagen/pathname" # auto require "pathname"
33
+ require "tagen/pathname" # it also require "pathname"
37
34
 
38
35
  this will add #path method to Pathname, see API doc.
39
36
 
@@ -58,6 +55,7 @@ Documentation
58
55
 
59
56
  Install
60
57
  ----------
58
+
61
59
  gem install tagen
62
60
 
63
61
  Contributing
@@ -70,8 +68,7 @@ Contributing
70
68
  Contributors
71
69
  ------------
72
70
 
73
- * [contributors](https://github.com/<%=github.username%>/<%=project%>/contributors)
74
-
71
+ * [contributors](https://github.com/GutenYe/tagen/contributors)
75
72
 
76
73
  Copyright
77
74
  ---------
@@ -1,10 +1,13 @@
1
1
  Core extensions
2
2
  ==============
3
- some come from ActiveSupport.
4
3
 
5
4
  Usage
6
5
  -----
7
- require "tagen/core" # include "time", "date"
6
+
7
+ require "tagen/core"
8
+ # or
9
+ require "tagen/core/string"
10
+
8
11
 
9
12
  From Tagen
10
13
  ----------
@@ -32,39 +35,6 @@ From Tagen
32
35
 
33
36
  * {Process.exists?}(pid)
34
37
 
35
- * {Marshal}.load dump _add Pa support_
36
-
37
38
  * {MatchData#to_hash}
38
39
 
39
- * {Pa} _a path library_
40
-
41
40
  * {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
-
50
- From ActiveSupport
51
- ------------------
52
- see [ActiveSupport Core Extensions Guide](http://edgeguides.rubyonrails.org/active_support_core_extensions.html)
53
-
54
- * `Object #blank? present? presence try`
55
-
56
- * `Module #mattr_x`
57
-
58
- * `Class #cattr_x`
59
-
60
- * `String #strip_heredoc at from to`
61
-
62
- * `Numeric #bytes kilobytes megabytes gigabytes terabytes petabytes exabytes`
63
-
64
- * `Enumerable #sum many? exclude?`
65
-
66
- * `Array #from to second third fourth fifth`
67
-
68
- * `Array.wrap`
69
-
70
- * `Hash #deep_merge[!]`
@@ -1,18 +1,3 @@
1
- # from ActiveSupport
2
- %w(
3
- object/blank object/try
4
- module/attribute_accessors
5
- class/attribute_accessors
6
- string/strip string/access
7
- numeric/bytes
8
- enumerable
9
- array/access array/wrap
10
- hash/deep_merge
11
- ).each {|n| require "active_support/core_ext/#{n}"}
12
-
13
- # from pd
14
- require "pd"
15
-
16
1
  # from core
17
2
  %w(
18
3
  core/kernel
@@ -35,7 +20,3 @@ require "pd"
35
20
 
36
21
  core/open_option
37
22
  ).each {|n| require_relative n }
38
-
39
- # from stdlib
40
- require "time"
41
- require "date"
@@ -100,7 +100,7 @@ class Array
100
100
 
101
101
  # add grep(arr rb/tage)
102
102
  def grep(pat_s, &blk)
103
- pats = Array.wrap(pat_s)
103
+ pats = Array===pat_s ? pat_s : [pat_s]
104
104
  pats.each.with_object([]) { |k, memo|
105
105
  memo.push *self.original_grep(k)
106
106
  }
@@ -0,0 +1,29 @@
1
+ # +exit code+ for shell programming.
2
+ #
3
+ # @example
4
+ #
5
+ # MyError = Class.new(Exception)
6
+ # MyError.exit_code = 1
7
+ #
8
+ # class MyError2 < Exception; @@eixt_code=1 end
9
+ #
10
+ # begin
11
+ # ...
12
+ # rescue MyError => e
13
+ # p e.exit_code #=> 1
14
+ # end
15
+ class Exception
16
+ class << self
17
+ def exit_code
18
+ @@exit_code
19
+ end
20
+
21
+ def exit_code=(code)
22
+ @@exit_code = code
23
+ end
24
+ end
25
+
26
+ def exit_code
27
+ self.class.exit_code
28
+ end
29
+ end
@@ -1,4 +1,29 @@
1
1
  class ExtendHash < Hash
2
+ class << self
3
+ def [](hash)
4
+ case hash
5
+ when ExtendHash
6
+ hash
7
+ when Hash
8
+ eh = self.new
9
+ eh.replace deep_convert(hash)
10
+ else
11
+ raise ArgumentError, "must be a Hash or ExtendHash"
12
+ end
13
+ end
14
+
15
+ private
16
+ # convert string key to symbol key.
17
+ # I'm rescurive
18
+ def deep_convert(hash)
19
+ ret = {}
20
+ hash.each { |k,v|
21
+ ret[k.to_sym] = Hash===v ? deep_convert(v) : v
22
+ }
23
+ ret
24
+ end
25
+ end
26
+
2
27
  def []=(key, value)
3
28
  key = key.to_sym if String === key
4
29
  super(key, value)
@@ -25,7 +25,7 @@ class Hash
25
25
  #
26
26
  # @return [Hash]
27
27
  def grep(pat_s)
28
- pats = Array.wrap(pat_s)
28
+ pats = Array===pat_s ? pat_s : [pat_s]
29
29
 
30
30
  filtered_keys = pats.each.with_object([]) { |pat, memo|
31
31
  memo.push *self.keys.grep(pat)
@@ -1,28 +1,4 @@
1
1
  class Module
2
-
3
- alias :original_append_features :append_features
4
-
5
- # after include module, convert methods in ClassMethods to class methods. a very clean design.
6
- # @see ruby-core Module#append_features
7
- #
8
- # @example
9
- # module Guten
10
- # module ClassMethods
11
- # def foo; end # this is class method.
12
- # end
13
- #
14
- # def bar; end # this is instance method.
15
- # end
16
- #
17
- # class Tag
18
- # include Guten # will auto Tag.extend(Guten::Classmethods)
19
- # end
20
- #
21
- def append_features base
22
- original_append_features base
23
- base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
24
- end
25
-
26
2
  # return hash instead of names
27
3
  # @see constants
28
4
  #
@@ -32,5 +8,4 @@ class Module
32
8
  m[k] = const_get(k)
33
9
  end
34
10
  end
35
-
36
11
  end #class Module
@@ -1,10 +1,3 @@
1
1
  module Tagen
2
- module VERSION
3
- MAJOR = 1
4
- MINOR = 0
5
- PATCH = 4
6
- PRE = nil
7
-
8
- IS = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
9
- end
2
+ VERSION="1.1.0"
10
3
  end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+ require "tagen/core/exception"
3
+
4
+ describe "Exception" do
5
+ it "works with syntax 1" do
6
+ MyError = Class.new Exception
7
+ MyError.exit_code = 1
8
+
9
+ MyError.new.exit_code.should == 1
10
+ end
11
+
12
+ it "works with syntax 2" do
13
+ class MyError2 < Exception; @@exit_code = 1 end
14
+ MyError2.new.exit_code.should == 1
15
+ end
16
+
17
+ end
18
+
@@ -1,7 +1,26 @@
1
1
  require "spec_helper"
2
2
  require "tagen/core/extend_hash"
3
3
 
4
+ class ExtendHash
5
+ class << self
6
+ public :deep_convert
7
+ end
8
+ end
9
+
4
10
  describe ExtendHash do
11
+ describe ".deep_convert" do
12
+ it "convert string key to symbol key" do
13
+ ExtendHash.deep_convert({"a" => 1, "b" => {"c" => 2}}).should == {a: 1, b: {c: 2}}
14
+ end
15
+ end
16
+
17
+ describe ".[]" do
18
+ it "convert Hash to ExtendHash with string key" do
19
+ ret = ExtendHash[a: 1, "b" => 2]
20
+ ret.should == {a: 1, b: 2}
21
+ end
22
+ end
23
+
5
24
  describe "#[]" do
6
25
  it "convert string-key to symbol-key" do
7
26
  h = ExtendHash.new
@@ -3,10 +3,10 @@ require "tagen/version"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tagen"
6
- s.version = Tagen::VERSION::IS
6
+ s.version = Tagen::VERSION
7
7
  s.summary = "a core and extra extension to ruby library"
8
8
  s.description = <<-EOF
9
- 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.
9
+ a core and extra extension to ruby library.
10
10
  EOF
11
11
 
12
12
  s.author = "Guten"
@@ -15,7 +15,4 @@ an extension to ruby core and gem library. use some active_support/core_ext, but
15
15
  s.rubyforge_project = "xx"
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
18
-
19
- s.add_dependency "activesupport"
20
- s.add_dependency "pd"
21
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,35 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-31 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: activesupport
16
- requirement: &23815820 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *23815820
25
- - !ruby/object:Gem::Dependency
26
- name: pd
27
- requirement: &23813820 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: *23813820
36
- description: ! 'an extension to ruby core and gem library. use some active_support/core_ext,
37
- but smaller than active_support. active_support is mainly target to Rails, but tagen
38
- is target to generic ruby development.
39
-
40
- '
12
+ date: 2011-09-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "a core and extra extension to ruby library. \n"
41
15
  email: ywzhaifei@gmail.com
42
16
  executables: []
43
17
  extensions: []
@@ -61,6 +35,7 @@ files:
61
35
  - lib/tagen/core/array/extract_options.rb
62
36
  - lib/tagen/core/enumerable.rb
63
37
  - lib/tagen/core/enumerator.rb
38
+ - lib/tagen/core/exception.rb
64
39
  - lib/tagen/core/extend_hash.rb
65
40
  - lib/tagen/core/hash.rb
66
41
  - lib/tagen/core/io.rb
@@ -95,6 +70,7 @@ files:
95
70
  - spec/tagen/core/array/extract_options_spec.rb
96
71
  - spec/tagen/core/array_spec.rb
97
72
  - spec/tagen/core/enumerator_spec.rb
73
+ - spec/tagen/core/exception_spec.rb
98
74
  - spec/tagen/core/extend_hash_spec.rb
99
75
  - spec/tagen/core/hash_spec.rb
100
76
  - spec/tagen/core/module_spec.rb