configurator 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +1 -0
- data/lib/configurator/configurator.rb +122 -0
- data/lib/configurator/version.rb +1 -1
- data/website/index.html +1 -1
- metadata +2 -1
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -0,0 +1,122 @@
|
|
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
|
+
|
data/lib/configurator/version.rb
CHANGED
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.1.
|
36
|
+
<a href="http://rubyforge.org/projects/configurator" class="numbers">1.1.1</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.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wz
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- config/hoe.rb
|
35
35
|
- config/requirements.rb
|
36
36
|
- lib/configurator.rb
|
37
|
+
- lib/configurator/configurator.rb
|
37
38
|
- lib/configurator/version.rb
|
38
39
|
- log/debug.log
|
39
40
|
- script/destroy
|