kung_figure 0.0.1
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/README.rdoc +88 -0
- data/Rakefile +46 -0
- data/lib/kung_figure.rb +79 -0
- data/test/kung_figure_test.rb +60 -0
- data/test/test_helper.rb +9 -0
- metadata +59 -0
data/README.rdoc
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
= Simple Rest
|
2
|
+
|
3
|
+
* http://github.com/niquola/kung_figure
|
4
|
+
|
5
|
+
== DESCRIPTION
|
6
|
+
|
7
|
+
Simple RUBY configuration DSL
|
8
|
+
|
9
|
+
== INSTALL:
|
10
|
+
|
11
|
+
sudo gem install kung_figure
|
12
|
+
|
13
|
+
or for rails in config/environment.rb
|
14
|
+
|
15
|
+
config.gem 'kung_figure'
|
16
|
+
|
17
|
+
then
|
18
|
+
|
19
|
+
sudo rake gems:install
|
20
|
+
|
21
|
+
== USAGE
|
22
|
+
|
23
|
+
Example:
|
24
|
+
|
25
|
+
module MyModule
|
26
|
+
include KungFigure
|
27
|
+
class Config < KungFigure::Base
|
28
|
+
define_prop :prop1,'default1'
|
29
|
+
define_prop :prop2, 2
|
30
|
+
|
31
|
+
class NestedConfig < KungFigure::Base
|
32
|
+
define_prop :prop3,'prop3'
|
33
|
+
|
34
|
+
class NestedNestedConfig < KungFigure::Base
|
35
|
+
define_prop :prop4,'prop4'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
This will add method configure,config,load_config into MyModule:
|
42
|
+
|
43
|
+
MyModule.configure do
|
44
|
+
prop1 'new value'
|
45
|
+
nested_config do
|
46
|
+
prop3 'new value'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
and accessors for configs:
|
51
|
+
|
52
|
+
MyModule.config.prop1
|
53
|
+
MyModule.config.nested_config.prop1
|
54
|
+
|
55
|
+
You can also include KungFigure into nested in module classes and declare Config class (subclassing KungFigure::Base)
|
56
|
+
to get similar result:
|
57
|
+
|
58
|
+
|
59
|
+
module MyModule
|
60
|
+
include KungFigure
|
61
|
+
class Config < KungFigure::Base
|
62
|
+
define_prop :prop1,'default1'
|
63
|
+
end
|
64
|
+
#somewhere in another file
|
65
|
+
class WorkHorse
|
66
|
+
include KungFigure
|
67
|
+
class Config < KungFigure::Base
|
68
|
+
define_prop :my_config,'default'
|
69
|
+
end
|
70
|
+
|
71
|
+
def meth
|
72
|
+
config.my_config
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
Then your can access configuration with:
|
78
|
+
|
79
|
+
MyModule::WorkHorse.config or MyModule.config.work_horse
|
80
|
+
|
81
|
+
Or inside instances of WorkHorse as config method.
|
82
|
+
|
83
|
+
|
84
|
+
== CHANGE LOG
|
85
|
+
|
86
|
+
== MORE
|
87
|
+
|
88
|
+
For more info see tests and source code :)
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the pg_gnostic plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the pg_gnostic plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'kung_figure'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
|
28
|
+
|
29
|
+
spec = Gem::Specification.new do |s|
|
30
|
+
s.name = "kung_figure"
|
31
|
+
s.version = "0.0.1"
|
32
|
+
s.author = "niquola"
|
33
|
+
s.email = "niquola@gmail.com"
|
34
|
+
s.homepage = "http://github.com/niquola/kung_figure"
|
35
|
+
s.platform = Gem::Platform::RUBY
|
36
|
+
s.summary = "Ruby configuration DSL base"
|
37
|
+
s.files = PKG_FILES.to_a
|
38
|
+
s.require_path = "lib"
|
39
|
+
s.has_rdoc = false
|
40
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Turn this plugin into a gem.'
|
44
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
45
|
+
pkg.gem_spec = spec
|
46
|
+
end
|
data/lib/kung_figure.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module KungFigure
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def config
|
8
|
+
self.class.config
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def set_root_config_class(klazz)
|
13
|
+
@root_config_class = klazz
|
14
|
+
end
|
15
|
+
|
16
|
+
def root_config_class
|
17
|
+
self.const_get(@root_config_class || :Config)
|
18
|
+
end
|
19
|
+
|
20
|
+
def config
|
21
|
+
@config ||= root_config_class.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure(&block)
|
25
|
+
config.instance_eval &block
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_config(path)
|
29
|
+
load path
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Base
|
34
|
+
class << self
|
35
|
+
def define_prop(name,default)
|
36
|
+
define_method(name) do |*args|
|
37
|
+
@props ||= {}
|
38
|
+
if args.length > 0
|
39
|
+
@props[name] = args[0]
|
40
|
+
else
|
41
|
+
@props[name] || default
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def camelize(str)
|
48
|
+
str.split('_').map{|l| l.capitalize}.join('')
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_from_enclosing_module(klazz_name)
|
52
|
+
config_klazz_path=self.class.name.to_s.split('::')[0..-2]
|
53
|
+
config_klazz_path<< klazz_name
|
54
|
+
config_klazz_path.inject(Object){|parent,nxt|
|
55
|
+
break unless parent.const_defined?(nxt.to_sym)
|
56
|
+
parent.const_get(nxt.to_sym)
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def method_missing(key,&block)
|
61
|
+
@props ||= {}
|
62
|
+
klazz_name = camelize(key.to_s).to_sym
|
63
|
+
child_cfg_clazz = self.class.const_get(klazz_name) if self.class.const_defined?(klazz_name)
|
64
|
+
child_cfg_clazz ||= get_from_enclosing_module(klazz_name)
|
65
|
+
|
66
|
+
raise "No such configuration #{key}" unless child_cfg_clazz
|
67
|
+
|
68
|
+
unless @props.key?(key)
|
69
|
+
@props[key] = if child_cfg_clazz.ancestors.include?(KungFigure::Base)
|
70
|
+
child_cfg_clazz.new
|
71
|
+
elsif child_cfg_clazz.respond_to?(:config)
|
72
|
+
child_cfg_clazz.config
|
73
|
+
end
|
74
|
+
end
|
75
|
+
@props[key].instance_eval(&block) if block_given?
|
76
|
+
return @props[key]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module MyModule
|
4
|
+
include KungFigure
|
5
|
+
class Config < KungFigure::Base
|
6
|
+
define_prop :prop1,'default1'
|
7
|
+
define_prop :prop2, 2
|
8
|
+
|
9
|
+
class NestedConfig < KungFigure::Base
|
10
|
+
define_prop :prop3,'prop3'
|
11
|
+
|
12
|
+
class NestedNestedConfig < KungFigure::Base
|
13
|
+
define_prop :prop4,'prop4'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
module MyModule
|
21
|
+
class WorkHorse
|
22
|
+
include KungFigure
|
23
|
+
class Config < KungFigure::Base
|
24
|
+
define_prop :my_config,'default'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class TestKungFigure < Test::Unit::TestCase
|
30
|
+
def test_configuration
|
31
|
+
assert_equal('default1',MyModule.config.prop1)
|
32
|
+
assert_equal(2,MyModule.config.prop2)
|
33
|
+
assert_equal('prop3',MyModule.config.nested_config.prop3)
|
34
|
+
assert_equal('prop4',MyModule.config.nested_config.nested_nested_config.prop4)
|
35
|
+
|
36
|
+
MyModule.config.nested_config.nested_nested_config.prop4 'new value'
|
37
|
+
assert_equal('new value',MyModule.config.nested_config.nested_nested_config.prop4)
|
38
|
+
|
39
|
+
MyModule.configure do
|
40
|
+
nested_config do
|
41
|
+
prop3 'value3'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
assert_equal('value3',MyModule.config.nested_config.prop3)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_nested_config_declaration
|
49
|
+
assert_equal('default',MyModule.config.work_horse.my_config)
|
50
|
+
assert_equal('default',MyModule::WorkHorse.config.my_config)
|
51
|
+
MyModule.configure do
|
52
|
+
work_horse do
|
53
|
+
my_config 'new value'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
assert_equal('new value',MyModule.config.work_horse.my_config)
|
57
|
+
assert_equal('new value',MyModule::WorkHorse.config.my_config)
|
58
|
+
assert_equal(MyModule::WorkHorse.config,MyModule::WorkHorse.new.config)
|
59
|
+
end
|
60
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kung_figure
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- niquola
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-10 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: niquola@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- README.rdoc
|
27
|
+
- lib/kung_figure.rb
|
28
|
+
- test/kung_figure_test.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/niquola/kung_figure
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Ruby configuration DSL base
|
58
|
+
test_files: []
|
59
|
+
|