o 0.1.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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *~
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ gem 'watchr'
5
+ gem 'rag'
6
+ end
7
+
8
+ gemspec
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 ADDED
@@ -0,0 +1,107 @@
1
+ O, a configuration libraray for Ruby
2
+ ====================================
3
+
4
+ **Homepage**: [https://github.com/GutenLinux/o](https://github.com/GutenLinux/o) <br/>
5
+ **Author**: Guten <br/>
6
+ **License**: MIT-LICENSE <br/>
7
+ **Documentation**: [http://rubydoc.info/gems/o/frames](http://rubydoc.info/gems/o/frames) <br/>
8
+ **Issue Tracker**: [https://github.com/GutenLinux/o/issues](https://github.com/GutenLinux/o/issues) <br/>
9
+
10
+ Overview
11
+ --------
12
+
13
+ descripe your prjoect here.
14
+
15
+ Features
16
+ --------
17
+
18
+ a clearly list of features.
19
+
20
+ Introduction
21
+ -------------
22
+
23
+ option = O.new
24
+
25
+ # assigment
26
+ option["a"] = 1
27
+ option[:a] = 1
28
+ option.a = 1
29
+
30
+ # access
31
+ option["a"]
32
+ option[:a]
33
+ option.a
34
+ option.a? #=> true
35
+
36
+ #access Hash methods.
37
+ option._keys #=> [:a]
38
+
39
+ assign default value
40
+
41
+ option = O.new
42
+ option.a #=> nil
43
+
44
+ option = O.new 0
45
+ option.a #=> 0
46
+
47
+ another syntax
48
+
49
+ option = O do
50
+ base = 1
51
+ @a = base
52
+ @b = base + 1
53
+ end
54
+ option.a #=> 1
55
+ option.b #=> 2
56
+
57
+
58
+ read option from a file
59
+
60
+ # ~/.gutenrc
61
+ @a = 1
62
+ @path = Pathname('/home')
63
+
64
+ # a.rb
65
+ require "pathname"
66
+ option = O.load("~/.gutenrc")
67
+ option.a #=> 1
68
+
69
+ configuration file
70
+ ------------------
71
+
72
+ use instance variable to export field.
73
+
74
+ base = 1
75
+ @a = base
76
+ @b = O do
77
+ p @a #=> nil # instance variable can't pass into block
78
+ p base #=> 1 # local variable can pass into block
79
+ @a = base + 1
80
+ end
81
+
82
+ # after O.load(file)
83
+ option.a #=> 1
84
+ option.b.a #=> 2
85
+
86
+ Contributing
87
+ -------------
88
+
89
+ * join the project.
90
+ * report bugs/featues to issue tracker.
91
+ * fork it and pull a request.
92
+ * improve documentation.
93
+ * feel free to post any ideas.
94
+
95
+ Install
96
+ ----------
97
+
98
+ gem install o
99
+
100
+ Resources
101
+ ---------
102
+
103
+ some related resources to help each other.
104
+
105
+ Copyright
106
+ ---------
107
+ Copyright &copy; 2011 by Guten. this library released under MIT-LICENSE, See {file:LICENSE} for futher details.
data/Ragfile ADDED
@@ -0,0 +1,5 @@
1
+ # add your own task in Ragfile or in tasks/*.rag
2
+ class Rag < Thor
3
+ end
4
+
5
+ # vim: filetype=ruby
data/lib/o.rb ADDED
@@ -0,0 +1,175 @@
1
+ #
2
+ # internal: store data in a Hash, the key of the Hash is always converted to symbol.
3
+ #
4
+ #
5
+ #
6
+ class O < Hash
7
+ # PATH for O.load
8
+ PATH = []
9
+ Error = Exception.new
10
+ LoadError = Exception.new(Error)
11
+
12
+ class O_Eval
13
+ def _data
14
+ _data = {}
15
+ self.instance_variables.each do |k|
16
+ key = k[1..-1].to_sym
17
+ value = self.instance_variable_get(k)
18
+ _data[key] = value
19
+ end
20
+ _data
21
+ end
22
+ end
23
+
24
+ class << self
25
+ # convert <#Hash> to <#O>
26
+ #
27
+ # @param [hash] hash
28
+ # @return O
29
+ def from_hash hash
30
+ o = O.new
31
+ o._replace hash
32
+ end
33
+
34
+ # load a configuration file,
35
+ # support PATH, and '~/.gutenrc'
36
+ #
37
+ # first try name.rb, then use name
38
+ #
39
+ # @example
40
+ # option = O.load("~/.gutenrc")
41
+ #
42
+ # O::Path << "/home"
43
+ # option = O.load("guten") #=> try guten.rb; then try guten
44
+ # option = O.load("guten.rb")
45
+ #
46
+ # @param [String] name
47
+ # @return [O]
48
+ def load name
49
+ path = nil
50
+ if name =~ /^~/
51
+ file = File.expand_path(name)
52
+ path = file if File.exists?(file)
53
+ else
54
+ catch :break do
55
+ PATH.each do |p|
56
+ ['.rb', ''].each {|ext|
57
+ file = File.join(p, name+ext)
58
+ if File.exists? file
59
+ path = file
60
+ throw :break
61
+ end
62
+ }
63
+ end
64
+ end
65
+ end
66
+
67
+ raise LoadError, "can't find file -- #{name}" unless path
68
+
69
+ eval_file path
70
+ end
71
+
72
+ # relative load a configuration file
73
+ # @see load
74
+ #
75
+ # @param [String] name
76
+ # @return [O] option
77
+ def relative_load name
78
+ pd caller if $TEST
79
+ a,file, line, method = caller[0].match(/^(.*):(\d+):.*`(.*)'$/).to_a
80
+ raise LoadError, "#{type} is called in #{file}" if file=~/\(.*\)/ # eval, etc.
81
+
82
+ file = File.readlink(file) if File.symlink?(file)
83
+
84
+ path = nil
85
+ [".rb", ""].each do |ext|
86
+ f = File.absolute_path(File.join(File.dirname(file), name+ext))
87
+ if File.exists?(f)
88
+ path = f
89
+ break
90
+ end
91
+ end
92
+
93
+ raise LoadError, "can't find file -- #{name}" unless path
94
+
95
+ eval_file path
96
+ end
97
+
98
+ private
99
+ def eval_file path
100
+ content = File.open(path){|f| f.read}
101
+ o_eval = O_Eval.new
102
+ o_eval.instance_eval(content)
103
+ O.from_hash(o_eval._data)
104
+ end
105
+
106
+ end
107
+
108
+ attr_reader :_data
109
+
110
+ def initialize default=nil, &blk
111
+ @_data = Hash.new(default)
112
+ if blk
113
+ o_eval = O_Eval.new
114
+ o_eval.instance_eval &blk
115
+ @_data.merge!(o_eval._data)
116
+ end
117
+ end
118
+
119
+ def []= key, value
120
+ @_data[key.to_sym] = value
121
+ end
122
+
123
+ def [] key
124
+ @_data[key.to_sym]
125
+ end
126
+
127
+ def + other
128
+ O.new(@_data, other._data)
129
+ end
130
+
131
+ def _replace data
132
+ case data
133
+ when Hash
134
+ @_data = data
135
+ when O
136
+ @_data = data._data
137
+ end
138
+ self
139
+ end
140
+
141
+ #
142
+ # _method goes to @_data.send(_method, ..)
143
+ # method? #=> !! @_data[:method]
144
+ # method #=> @_data[:method]
145
+ # method=value #=> @_data[:method]=value
146
+ #
147
+ def method_missing method, *args, &blk
148
+ if method =~ /(.*)=$/
149
+ @_data[$1.to_sym] = args[0]
150
+ elsif method =~ /(.*)\?$/
151
+ !! @_data[$1.to_sym]
152
+ elsif method =~ /^_(.*)/
153
+ @_data.send($1.to_sym, *args, &blk)
154
+ else
155
+ @_data[method]
156
+ end
157
+ end
158
+
159
+ def inspect
160
+ rst = "<#O "
161
+ @_data.each do |k,v|
162
+ rst << "#{k}:#{v.inspect} "
163
+ end
164
+ rst << " >"
165
+ end
166
+
167
+ alias to_s inspect
168
+
169
+ end
170
+
171
+ module Kernel
172
+ def O default=nil, &blk
173
+ O.new(default, &blk)
174
+ end
175
+ end
data/o.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ $: << "."
2
+ require "version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "o"
6
+ s.version = O::VERSION::IS
7
+ s.summary = "a configuration libraray for Ruby"
8
+ s.description = <<-EOF
9
+ a coonfiguration libraray for Ruby
10
+ EOF
11
+
12
+ s.author = "Guten"
13
+ s.email = "ywzhaifei@Gmail.com"
14
+ s.homepage = "http://github.com/GutenLinux/o"
15
+ s.rubyforge_project = "xx"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ #s.executables = ["x"]
19
+
20
+ #s.add_dependency "x"
21
+ end
data/o.watchr ADDED
@@ -0,0 +1,22 @@
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 #{path}"
19
+ puts cmd
20
+ system cmd
21
+ end
22
+
@@ -0,0 +1 @@
1
+ @a = 1
@@ -0,0 +1,2 @@
1
+ @a = 1
2
+
@@ -0,0 +1 @@
1
+ @a = 1
@@ -0,0 +1,12 @@
1
+
2
+ base = 1
3
+
4
+ @a = base
5
+
6
+ @b = O do
7
+ @a = 2
8
+ @c = base
9
+ end
10
+
11
+
12
+ # vim: filetype=ruby
data/spec/o_spec.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "spec_helper"
2
+
3
+ class O
4
+ attr_reader :_data
5
+ end
6
+
7
+ o = O.relative_load "data/lib/test"
8
+ p o._data
9
+
10
+ describe O do
11
+ before :each do
12
+ @o = O.new
13
+ @o[:a] = 1
14
+ end
15
+
16
+ describe "#[]=" do
17
+ it "converts key to symbol" do
18
+ @o["b"] = 2
19
+ @o._data[:b].should == 2
20
+ end
21
+ end
22
+
23
+ describe "#[]" do
24
+ it "converts key to symbol" do
25
+ @o["a"].should == 1
26
+ end
27
+ end
28
+
29
+ describe "#method_missing" do
30
+ it "calls #key" do
31
+ @o.a.should == 1
32
+ end
33
+
34
+ it "calls #key?" do
35
+ @o.a?.should == true
36
+ end
37
+
38
+ it "calls #key=" do
39
+ @o.b = 2
40
+ @o._data[:b].should == 2
41
+ end
42
+
43
+ it "calls #_method" do
44
+ @o._keys.should == [:a]
45
+ end
46
+
47
+
48
+ end
49
+
50
+ describe ".new" do
51
+ it "has default value" do
52
+ O.new.a.should == nil
53
+ O.new(1).a.should == 1
54
+ end
55
+ it "retrive a block" do
56
+ o = O.new do
57
+ base = 1
58
+ @a = base
59
+ @b = base + 1
60
+ end
61
+ o.a.should == 1
62
+ o.b.should == 2
63
+ end
64
+ end
65
+
66
+ describe ".load" do
67
+ it "support ~/path" do
68
+ ENV["HOME"] = File.join($spec_dir, "data/home")
69
+ o = O.load("~/gutenrc")
70
+ o._data.should == {a: 1}
71
+ end
72
+
73
+ it "support PATH" do
74
+ O::PATH << File.join($spec_dir, "data/lib")
75
+
76
+ o = O.load("tag.rb")
77
+ o._data.should == {a: 1}
78
+
79
+ o = O.load("guten")
80
+ o._data.should == {a: 1}
81
+
82
+ end
83
+ end
84
+
85
+ describe ".relative_load" do
86
+ o = O.relative_load "data/lib/guten"
87
+ o._data.should == {a: 1}
88
+
89
+ o = O.relative_load "data/lib/tag.rb"
90
+ o._data.should == {a: 1}
91
+ end
92
+
93
+ end
94
+
95
+ describe O::O_Eval do
96
+ it "works" do
97
+ o = O::O_Eval.new
98
+ o.instance_eval <<-EOF
99
+ @a = 1
100
+ EOF
101
+
102
+ o._data.should == {a: 1}
103
+ end
104
+ end
105
+
106
+ describe "#O" do
107
+ option = O do
108
+ @a = 1
109
+ end
110
+ option._data.should == {a: 1}
111
+ end
@@ -0,0 +1,3 @@
1
+ require "o"
2
+
3
+ $spec_dir = File.dirname(__FILE__)
data/version.rb ADDED
@@ -0,0 +1,9 @@
1
+ module O
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+
7
+ IS = [MAJOR, MINOR, PATCH].join(".")
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: o
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Guten
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-10 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: |
17
+ a coonfiguration libraray for Ruby
18
+
19
+ email: ywzhaifei@Gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - .rspec
29
+ - Gemfile
30
+ - LICENSE
31
+ - README.md
32
+ - Ragfile
33
+ - lib/o.rb
34
+ - o.gemspec
35
+ - o.watchr
36
+ - spec/data/home/gutenrc
37
+ - spec/data/lib/guten
38
+ - spec/data/lib/tag.rb
39
+ - spec/data/lib/test
40
+ - spec/o_spec.rb
41
+ - spec/spec_helper.rb
42
+ - version.rb
43
+ homepage: http://github.com/GutenLinux/o
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project: xx
66
+ rubygems_version: 1.8.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: a configuration libraray for Ruby
70
+ test_files: []
71
+