configurator 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +1 -1
- data/README.txt +43 -1
- data/lib/configurator.rb +6 -2
- data/lib/configurator/extension_methods.rb +79 -0
- data/lib/configurator/version.rb +2 -2
- data/test/test_configurator.rb +1 -1
- data/website/index.html +1 -1
- metadata +3 -3
- data/lib/configurator/configurator.rb +0 -122
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1 +1,43 @@
|
|
1
|
-
|
1
|
+
Configurator extends configuration behavior to class.
|
2
|
+
|
3
|
+
==make class with configurator extension
|
4
|
+
Configurator allows you to delcare config paramaters with keys to class. It works as hash.
|
5
|
+
require 'rubygems'
|
6
|
+
require 'configurator'
|
7
|
+
class Klass
|
8
|
+
extend Congigurator
|
9
|
+
config :name, 'Matsumoto'
|
10
|
+
config :age, 18
|
11
|
+
end
|
12
|
+
|
13
|
+
==refer to config paramaters
|
14
|
+
Paramaters are referable from both Class and Instance.
|
15
|
+
|
16
|
+
from class
|
17
|
+
Klass.config[:name] #=> 'Matsumoto'
|
18
|
+
Klass.config[:age] #=> 18
|
19
|
+
from instance
|
20
|
+
kls = Klass.new
|
21
|
+
kls.config[:name] #=> 'Matsumoto'
|
22
|
+
kls.config[:age] #=> 18
|
23
|
+
|
24
|
+
==inherite and rewrite
|
25
|
+
Paramaters are also inheritable and rewritable.
|
26
|
+
class SubKlass < Klass
|
27
|
+
config :name, 'Matz'
|
28
|
+
config :sex, 'm'
|
29
|
+
end
|
30
|
+
sub = SubKlass.new
|
31
|
+
sub.config[:name] # => 'Matz'
|
32
|
+
sub.config[:age] # => 18
|
33
|
+
sub.config[:sex] # => 'm'
|
34
|
+
kls.config[:name] # => 'Matsumoto'
|
35
|
+
|
36
|
+
==instance specific config paramaters
|
37
|
+
It works as like as instance specific method.
|
38
|
+
Klass.config :name, 'Matz'
|
39
|
+
kls = Klass.new
|
40
|
+
kls.config :name, 'Yukihiro'
|
41
|
+
kls.config[:name] # => 'Yukihiro'
|
42
|
+
kls2 = Klass.new
|
43
|
+
kls2.config[:name] # => 'Matz'
|
data/lib/configurator.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
2
|
|
3
|
+
require 'configurator/extension_methods'
|
4
|
+
|
3
5
|
module Configurator
|
6
|
+
include Configurator::ExtensionMethods
|
7
|
+
def self.extended( mod )
|
8
|
+
mod.module_eval { include Configurator::ExtensionMethods }
|
9
|
+
end
|
4
10
|
end
|
5
|
-
|
6
|
-
require 'configurator/configurator'
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Configurator
|
2
|
+
|
3
|
+
module ExtensionMethods
|
4
|
+
|
5
|
+
public
|
6
|
+
|
7
|
+
# set config paramaters within class block code.
|
8
|
+
# class Klass
|
9
|
+
# extend Configurator
|
10
|
+
# config key, value
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Another way to set paramaters.
|
14
|
+
# Klass.config key, value
|
15
|
+
# Klass.config[key] = value
|
16
|
+
# Klass.new.config key, value
|
17
|
+
# Klass.new.config[key] = value
|
18
|
+
#
|
19
|
+
# Reffer to config paramaters.
|
20
|
+
# Klass.config[key] # => value
|
21
|
+
# Klass.new.config[key] # => value
|
22
|
+
#
|
23
|
+
# This method also works as instance method because instance includes this module automatically.
|
24
|
+
# kls = Klass.new
|
25
|
+
# kls.config[key] # => value
|
26
|
+
#
|
27
|
+
def config( key = nil, value = nil )
|
28
|
+
@__self_config ||= {}
|
29
|
+
@__config = __check_config
|
30
|
+
case
|
31
|
+
when ( key and value ) : @__self_config[ key ] = value; @__config = __merge_config; return value
|
32
|
+
when key : return @__config[ key ]
|
33
|
+
else return @__config
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def __merge_config
|
40
|
+
__inherited_config.merge( @__self_config )
|
41
|
+
end
|
42
|
+
|
43
|
+
def __check_config
|
44
|
+
@__config ||= {}
|
45
|
+
__config = __merge_config
|
46
|
+
@__config.each do |k, v|
|
47
|
+
unless __config[ k ] == v
|
48
|
+
@__self_config[ k ] = v
|
49
|
+
end
|
50
|
+
end
|
51
|
+
@__config = __merge_config
|
52
|
+
end
|
53
|
+
|
54
|
+
def __inherited_config
|
55
|
+
if self.respond_to?( :ancestors )
|
56
|
+
@__inherited_config = {}
|
57
|
+
ancestors[ 1 .. ( ancestors.size - 1 ) ].each do |ancestor|
|
58
|
+
if ancestor.respond_to?( :config ) and Hash === ancestor.config
|
59
|
+
@__inherited_config = ancestor.config.merge( @__inherited_config || {} )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
else
|
63
|
+
@__inherited_config = self.class.config
|
64
|
+
end
|
65
|
+
return @__inherited_config
|
66
|
+
end
|
67
|
+
|
68
|
+
def method_missing( name, *args, &block )
|
69
|
+
if @__config.keys.include?( name.to_sym )
|
70
|
+
@__config[ name.to_sym ]
|
71
|
+
else
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
data/lib/configurator/version.rb
CHANGED
data/test/test_configurator.rb
CHANGED
@@ -19,7 +19,7 @@ class TestConfigurator < Test::Unit::TestCase
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_config
|
22
|
-
sample = SampleClass.new
|
22
|
+
assert sample = SampleClass.new
|
23
23
|
assert sample.config[:name] == "sample"
|
24
24
|
assert sample.config[:address] == "127.0.0.1"
|
25
25
|
assert sample.config[:array] == [ 1, 2, 3 ]
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>configurator</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/configurator"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/configurator" class="numbers">1.
|
36
|
+
<a href="http://rubyforge.org/projects/configurator" class="numbers">1.2.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘configurator’</h1>
|
39
39
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configurator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-03-
|
12
|
+
date: 2008-03-24 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -34,8 +34,8 @@ files:
|
|
34
34
|
- config/hoe.rb
|
35
35
|
- config/requirements.rb
|
36
36
|
- lib/configurator.rb
|
37
|
-
- lib/configurator/configurator.rb
|
38
37
|
- lib/configurator/version.rb
|
38
|
+
- lib/configurator/extension_methods.rb
|
39
39
|
- log/debug.log
|
40
40
|
- script/destroy
|
41
41
|
- script/generate
|
@@ -1,122 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Configurator extends configuration behavior to class.
|
3
|
-
#
|
4
|
-
# ==make class with configurator extension
|
5
|
-
# Configurator allows you to delcare config paramaters with keys to class. It works as hash.
|
6
|
-
# require 'rubygems'
|
7
|
-
# require 'configurator'
|
8
|
-
# class Klass
|
9
|
-
# extend Congigurator
|
10
|
-
# config :name, 'Matsumoto'
|
11
|
-
# config :age, 18
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# ==refer to config paramaters
|
15
|
-
# Paramaters are referable from both Class and Instance.
|
16
|
-
#
|
17
|
-
# from class
|
18
|
-
# Klass.config[:name] #=> 'Matsumoto'
|
19
|
-
# Klass.config[:age] #=> 18
|
20
|
-
# from instance
|
21
|
-
# kls = Klass.new
|
22
|
-
# kls.config[:name] #=> 'Matsumoto'
|
23
|
-
# kls.config[:age] #=> 18
|
24
|
-
#
|
25
|
-
# ==inherite and rewrite
|
26
|
-
# Paramaters are also inheritable and rewritable.
|
27
|
-
# class SubKlass < Klass
|
28
|
-
# config :name, 'Matz'
|
29
|
-
# config :sex, 'm'
|
30
|
-
# end
|
31
|
-
# sub = SubKlass.new
|
32
|
-
# sub.config[:name] # => 'Matz'
|
33
|
-
# sub.config[:age] # => 18
|
34
|
-
# sub.config[:sex] # => 'm'
|
35
|
-
# kls.config[:name] # => 'Matsumoto'
|
36
|
-
#
|
37
|
-
# ==instance specific config paramaters
|
38
|
-
# It works as like as instance specific method.
|
39
|
-
# Klass.config :name, 'Matz'
|
40
|
-
# kls = Klass.new
|
41
|
-
# kls.config :name, 'Yukihiro'
|
42
|
-
# kls.config[:name] # => 'Yukihiro'
|
43
|
-
# kls2 = Klass.new
|
44
|
-
# kls2.config[:name] # => 'Matz'
|
45
|
-
module Configurator
|
46
|
-
|
47
|
-
public
|
48
|
-
# set config paramaters within class block code.
|
49
|
-
# class Klass
|
50
|
-
# extend Configurator
|
51
|
-
# config key, value
|
52
|
-
# end
|
53
|
-
#
|
54
|
-
# Another way to set paramaters.
|
55
|
-
# Klass.config key, value
|
56
|
-
# Klass.config[key] = value
|
57
|
-
# Klass.new.config key, value
|
58
|
-
# Klass.new.config[key] = value
|
59
|
-
#
|
60
|
-
# Reffer to config paramaters.
|
61
|
-
# Klass.config[key] # => value
|
62
|
-
# Klass.new.config[key] # => value
|
63
|
-
#
|
64
|
-
# This method also works as instance method because instance includes this module automatically.
|
65
|
-
# kls = Klass.new
|
66
|
-
# kls.config[key] # => value
|
67
|
-
#
|
68
|
-
def config( key = nil, value = nil )
|
69
|
-
@__self_config ||= {}
|
70
|
-
@__config = __check_config
|
71
|
-
case
|
72
|
-
when ( key and value ) : @__self_config[ key ] = value; @__config = __merge_config; return value
|
73
|
-
when key : return @__config[ key ]
|
74
|
-
else return @__config
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
private
|
79
|
-
|
80
|
-
def __merge_config
|
81
|
-
__inherited_config.merge( @__self_config )
|
82
|
-
end
|
83
|
-
|
84
|
-
def __check_config
|
85
|
-
@__config ||= {}
|
86
|
-
__config = __merge_config
|
87
|
-
@__config.each do |k, v|
|
88
|
-
unless __config[ k ] == v
|
89
|
-
@__self_config[ k ] = v
|
90
|
-
end
|
91
|
-
end
|
92
|
-
@__config = __merge_config
|
93
|
-
end
|
94
|
-
|
95
|
-
def __inherited_config
|
96
|
-
if self.respond_to?( :ancestors )
|
97
|
-
@__inherited_config = {}
|
98
|
-
ancestors[ 1 .. ( ancestors.size - 1 ) ].each do |ancestor|
|
99
|
-
if ancestor.respond_to?( :config ) and Hash === ancestor.config
|
100
|
-
@__inherited_config = ancestor.config.merge( @__inherited_config || {} )
|
101
|
-
end
|
102
|
-
end
|
103
|
-
else
|
104
|
-
@__inherited_config = self.class.config
|
105
|
-
end
|
106
|
-
return @__inherited_config
|
107
|
-
end
|
108
|
-
|
109
|
-
def method_missing( name, *args, &block )
|
110
|
-
if @__config.keys.include?( name.to_sym )
|
111
|
-
@__config[ name.to_sym ]
|
112
|
-
else
|
113
|
-
super
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.extended( mod )
|
118
|
-
mod.module_eval { include Configurator } #::InstanceMethods }
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|