tagen 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tagen (0.2.3)
4
+ tagen (0.2.4)
5
5
  activesupport
6
6
  pd
7
7
 
@@ -11,14 +11,14 @@ GEM
11
11
  activesupport (3.0.5)
12
12
  diff-lcs (1.1.2)
13
13
  pd (1.0.1)
14
- rspec (2.4.0)
15
- rspec-core (~> 2.4.0)
16
- rspec-expectations (~> 2.4.0)
17
- rspec-mocks (~> 2.4.0)
18
- rspec-core (2.4.0)
19
- rspec-expectations (2.4.0)
14
+ rspec (2.5.0)
15
+ rspec-core (~> 2.5.0)
16
+ rspec-expectations (~> 2.5.0)
17
+ rspec-mocks (~> 2.5.0)
18
+ rspec-core (2.5.1)
19
+ rspec-expectations (2.5.0)
20
20
  diff-lcs (~> 1.1.2)
21
- rspec-mocks (2.4.0)
21
+ rspec-mocks (2.5.0)
22
22
 
23
23
  PLATFORMS
24
24
  ruby
data/README.md CHANGED
@@ -15,8 +15,6 @@ Ruby has an 'Open Class' feature, so we can extend any class by ourself.
15
15
 
16
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.
17
17
 
18
- not support ruby1.8, win32
19
-
20
18
  This library comes with a path lib named {Pa} and a string format lib named {PyFormat}.
21
19
 
22
20
  Usage
@@ -38,6 +36,11 @@ or
38
36
 
39
37
  this will add #path method to Pathname, see API doc.
40
38
 
39
+ Requirements
40
+ ------------
41
+
42
+ tested: ruby1.9 linux
43
+
41
44
  An Introduction to Pa
42
45
  ---------------------
43
46
 
data/lib/tagen/core.rb CHANGED
@@ -23,6 +23,7 @@ require "pd"
23
23
  core/enumerator
24
24
  core/numeric
25
25
  core/string
26
+ core/symbol
26
27
  core/array
27
28
  core/hash
28
29
  core/re
@@ -32,6 +33,7 @@ require "pd"
32
33
  core/process
33
34
 
34
35
  core/pa
36
+ core/open_option
35
37
  ).each {|n| require_relative n }
36
38
 
37
39
  # from stdlib
@@ -24,7 +24,7 @@ private
24
24
  # @option o [Boolean] :verbose print cmd if verbose
25
25
  # @return [Boolean,nil] true false nil
26
26
  def system *cmds
27
- o = args.extract_extend_options!
27
+ o = cmds.extract_extend_options!
28
28
  cmd = cmds.join(" ")
29
29
  puts cmd if o[:verbose]
30
30
  original_system cmd
@@ -45,7 +45,6 @@ private
45
45
  method(:__blk2method)
46
46
  end
47
47
 
48
-
49
48
  # detect Platform information.
50
49
  #
51
50
  # RUBY_PLATFORM is "i686-linux" "i386-migw32"
@@ -23,4 +23,14 @@ class Module
23
23
  base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
24
24
  end
25
25
 
26
+ # return hash instead of names
27
+ # @see constants
28
+ #
29
+ # @return [Hash] {key: value}
30
+ def consts
31
+ constants.each.with_object ({}) do |k,m|
32
+ m[k] = const_get(k)
33
+ end
34
+ end
35
+
26
36
  end #class Module
@@ -0,0 +1,152 @@
1
+ =begin
2
+
3
+ main purpose of this Class is provide an option support.
4
+
5
+ Example:
6
+
7
+ o = OpenOption.new
8
+ o.name = 'alice'
9
+ p o.name #=> 'alice'
10
+
11
+ o.force = true
12
+ p o.force? #=> true
13
+
14
+ Overview:
15
+
16
+ o = OpenOption.new
17
+
18
+ define value, they are all the same
19
+
20
+ o.key = value
21
+ o[:symbol] = value
22
+ o["string"] = value
23
+
24
+ access value, they are all the same
25
+
26
+ o[:symbol]
27
+ o["string"]
28
+ o.key
29
+ o.key?
30
+
31
+ access hash method, some are special
32
+
33
+ o._keys #=> [:a]
34
+ o._merge(a: 1) #=> a new <#OpenOption a: 1>
35
+ o._merge!(a: 1) #=> self
36
+
37
+ access data
38
+
39
+ o._data #=> {a: 1}
40
+
41
+
42
+ new(data={})
43
+ ------------
44
+
45
+ data's key can be string or symbol, but internal store key is use symbol.
46
+
47
+ data = { "a" => 1 }
48
+ same as
49
+ data = { :a => 1 }
50
+
51
+ it is a deep convertion of Hash.
52
+
53
+ a = { a: {b: 1} }
54
+ o = OpenOption.new(a)
55
+ o #=> <#OpenOption a: <#OpenOption b:1> >
56
+ # so you can access b by o.a.b
57
+
58
+
59
+
60
+
61
+ =end
62
+ class OpenOption
63
+
64
+ class <<self
65
+ # I'm rescurive
66
+ # deep convert hash to OpenOption
67
+ def convert_hash data, ret={}
68
+ data.each do |k,v|
69
+ if Hash === v
70
+ new_v = self.new(v)
71
+ ret[k.to_sym] = new_v
72
+ convert_hash(data[k], ret[k.to_sym])
73
+ else
74
+ ret[k.to_sym] = v
75
+ end
76
+ end
77
+ ret
78
+ end
79
+ end
80
+
81
+ attr_accessor :_data
82
+
83
+ def initialize(data={})
84
+ @data = OpenOption.convert_hash(data)
85
+ end # def initialize
86
+
87
+ def _data() @data end
88
+ def _data=(data) @data = data end
89
+
90
+ # method return value
91
+ # _method goes to Hash
92
+ # method? return !!value
93
+ # method= define a new key
94
+ def method_missing(name, *args, &blk)
95
+ if name =~ /^_(.*)/
96
+ return @data.send($1.to_sym, *args, &blk)
97
+ elsif name =~ /(.*)\?$/
98
+ return !!@data[$1.to_sym]
99
+ elsif name =~ /(.*)=$/
100
+ @data[$1.to_sym] = args[0]
101
+ else
102
+ return @data[name.to_sym]
103
+ end
104
+ end
105
+
106
+ def marshal_dump
107
+ @data
108
+ end
109
+
110
+ def marshal_load data
111
+ @data = data
112
+ end
113
+
114
+ def ==(other)
115
+ return false unless other.kind_of?(self.class)
116
+ @data == other._data
117
+ end
118
+
119
+ def eql?(other) @data == other._data end
120
+
121
+ def []=(key, value) @data[key.to_sym] = value end
122
+
123
+ def [](key) @data[key.to_sym] end
124
+
125
+ def hash() @data.hash end
126
+
127
+ def dup
128
+ @data.dup
129
+ end
130
+
131
+ def inspect
132
+ out = "#<#{self.class} "
133
+ out << @data.map{|k,v| "#{k}: #{v.inspect}"}.join(', ')
134
+ out << ">"
135
+ end
136
+
137
+ alias to_s inspect
138
+
139
+ def _replace data
140
+ @data = data
141
+ end
142
+
143
+ def _merge *args
144
+ self.class.new @data.merge(*args)
145
+ end
146
+
147
+ def _merge! *args
148
+ @data.merge! *args
149
+ return self
150
+ end
151
+
152
+ end
@@ -38,6 +38,11 @@ class String
38
38
  # @return [String]
39
39
  def letters; @@letters end
40
40
  end
41
+
42
+ # return ascii code
43
+ # @example
44
+ # 'a'.ascii #=> 97
45
+ def ascii() bytes.first end
41
46
  end # class String
42
47
 
43
48
  require_relative "string/pyformat"
@@ -0,0 +1,9 @@
1
+ class Symbol
2
+
3
+ # goes to String, return Symbol
4
+ def method_missing name, *args
5
+ ret = to_s.send name, *args
6
+ String===ret ? ret.to_sym : ret
7
+ end
8
+
9
+ end
data/lib/tagen/erb.rb CHANGED
@@ -9,6 +9,7 @@ class ERB
9
9
  # erb = Erb.new("<%=a%>")
10
10
  # erb.result(nil, a: 1) #=> "1"
11
11
  #
12
+ # @param [Hash,OpenOption] locals
12
13
  def result bind=nil, locals={}
13
14
  bind ||= TOPLEVEL_BINDING
14
15
  if locals.empty?
@@ -20,6 +21,8 @@ class ERB
20
21
 
21
22
  private
22
23
  def result_with_locals bind, locals
24
+ locals = locals.class.to_s=="OpenOption" ? locals._data : locals
25
+
23
26
  @locals = locals
24
27
  evalstr = <<-EOF
25
28
  def run_erb
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ describe Module do
4
+ describe "#consts" do
5
+ it "runs ok" do
6
+ module Constants
7
+ A = 1
8
+ end
9
+
10
+ Constants.consts.should == {A: 1}
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ describe OpenOption do
4
+ before :each do
5
+ @o = OpenOption.new a: 1, force: true
6
+ end
7
+
8
+ it "runs ok" do
9
+ @o._data.should == {a: 1, force: true}
10
+ end
11
+
12
+
13
+
14
+ it "read value using [:sym] " do
15
+ @o[:a].should == 1
16
+ end
17
+
18
+ it "read value using ['str'] " do
19
+ @o['a'].should == 1
20
+ end
21
+
22
+ it "read value using #key" do
23
+ @o.a.should == 1
24
+ end
25
+
26
+ it "read value using #key?" do
27
+ @o.force?.should be_true
28
+ end
29
+
30
+ it "return nil if no key" do
31
+ @o[:key_not_exists].should be_nil
32
+ end
33
+
34
+ it "write value using [:sym]=" do
35
+ @o[:b] = 2
36
+ @o._data[:b].should == 2
37
+ end
38
+
39
+ it "write value using ['key']=" do
40
+ @o['b'] = 3
41
+ @o._data[:b].should == 3
42
+ end
43
+
44
+ it "write value using #key=" do
45
+ @o.b = 4
46
+ @o._data[:b].should == 4
47
+ end
48
+
49
+ it "#_merge" do
50
+ o = @o._merge(a: 2)
51
+ o._data[:a].should == 2
52
+ end
53
+
54
+ it "#_merge!" do
55
+ @o._merge!(a: 2)
56
+ @o._data[:a].should == 2
57
+ end
58
+
59
+ it "#_replace runs ok" do
60
+ @o._replace ({b: 2})
61
+ @o._data.should == {b: 2}
62
+ end
63
+
64
+ it "support normal hash method" do
65
+ @o._keys.should == [:a, :force]
66
+ end
67
+
68
+ describe ".convert_hash" do
69
+ it "deep convert hash" do
70
+ data = {a: {b: 1} }
71
+ newdata = OpenOption.convert_hash(data)
72
+ newdata[:a].should be_an_instance_of OpenOption
73
+ end
74
+
75
+ it "store string-key as symbol-key internal" do
76
+ data = {'a' => 1 }
77
+ newdata = OpenOption.convert_hash(data)
78
+ newdata.keys[0].should be_an_instance_of Symbol
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ describe Symbol do
4
+ describe "#method_missing" do
5
+ it "#sub. returns a symbol" do
6
+ :_foo.sub(/_/, '').should == :foo
7
+ end
8
+
9
+ it "#chars. return a enumerator" do
10
+ :_foo.chars.should be_an_instance_of Enumerator
11
+ end
12
+
13
+ end
14
+ end
@@ -1,5 +1,6 @@
1
1
  require "spec_helper"
2
2
  require "tagen/erb"
3
+ require "tagen/core"
3
4
 
4
5
  describe ERB do
5
6
  describe "#result" do
@@ -20,5 +21,10 @@ describe ERB do
20
21
  @erb.result(nil, "a" => 2).should == "2"
21
22
  end
22
23
 
24
+ it "support OpenOption" do
25
+ o = OpenOption.new(a: 1)
26
+ @erb.result(nil, o).should == '1'
27
+ end
28
+
23
29
  end
24
30
  end
data/tagen.gemspec CHANGED
@@ -3,7 +3,7 @@ require "version"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tagen"
6
- s.version = VERSION::IS
6
+ s.version = Tagen::VERSION::IS
7
7
  s.summary = "a core and extra extension to ruby library"
8
8
  s.description = <<-EOF
9
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.
data/version.rb CHANGED
@@ -1,7 +1,9 @@
1
- module VERSION
2
- MAJOR = 0
3
- MINOR = 2
4
- PATCH = 4
1
+ module Tagen
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ PATCH = 5
5
6
 
6
- IS = [MAJOR, MINOR, PATCH].join(".")
7
+ IS = [MAJOR, MINOR, PATCH].join(".")
8
+ end
7
9
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Guten
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-20 00:00:00 +08:00
17
+ date: 2011-03-26 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -79,6 +79,7 @@ files:
79
79
  - lib/tagen/core/module.rb
80
80
  - lib/tagen/core/numeric.rb
81
81
  - lib/tagen/core/object.rb
82
+ - lib/tagen/core/open_option.rb
82
83
  - lib/tagen/core/pa.rb
83
84
  - lib/tagen/core/pa/cmd.rb
84
85
  - lib/tagen/core/pa/dir.rb
@@ -88,6 +89,7 @@ files:
88
89
  - lib/tagen/core/re.rb
89
90
  - lib/tagen/core/string.rb
90
91
  - lib/tagen/core/string/pyformat.rb
92
+ - lib/tagen/core/symbol.rb
91
93
  - lib/tagen/core/time.rb
92
94
  - lib/tagen/erb.rb
93
95
  - lib/tagen/gdk_pixbuf2.rb
@@ -108,11 +110,14 @@ files:
108
110
  - spec/tagen/core/array/extract_options_spec.rb
109
111
  - spec/tagen/core/array_spec.rb
110
112
  - spec/tagen/core/enumerator_spec.rb
113
+ - spec/tagen/core/module_spec.rb
114
+ - spec/tagen/core/open_option_spec.rb
111
115
  - spec/tagen/core/pa/cmd_spec.rb
112
116
  - spec/tagen/core/pa/dir_spec.rb
113
117
  - spec/tagen/core/pa/path_spec.rb
114
118
  - spec/tagen/core/pa/state_spec.rb
115
119
  - spec/tagen/core/string/pyformat_spec.rb
120
+ - spec/tagen/core/symbol_spec.rb
116
121
  - spec/tagen/erb_spec.rb
117
122
  - spec/tagen/yaml_spec.rb
118
123
  - tagen.gemspec