saveconf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in saveconf.gemspec
4
+ gem 'rake'
5
+ gemspec
@@ -0,0 +1,24 @@
1
+ Lets you specify configuration schemes and configurations in a manner, that they can be ckecked at compile time.
2
+
3
+ `
4
+ class ConfigSchema < Saveconf::Base
5
+ schema(
6
+ host: 'some important server',
7
+ web_app: {
8
+ host: 'another important server',
9
+ user: 'user for basic auth',
10
+ password: 'basic auth pw'
11
+ }
12
+ )
13
+ end
14
+
15
+ class Production < ConfigSchema
16
+ host! "http://localhost:8080"
17
+
18
+ web_app! do
19
+ host! "http://testing.local/"
20
+ user! "matt"
21
+ password! "xxx"
22
+ end
23
+ end
24
+ `
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,11 @@
1
+ module Saveconf
2
+ extend self
3
+
4
+ def root
5
+ @root ||= File.expand_path('../../', __FILE__)
6
+ end
7
+ end
8
+
9
+ $: << Saveconf.root
10
+
11
+ require 'lib/saveconf/config'
@@ -0,0 +1,109 @@
1
+ module Saveconf
2
+
3
+ # the object that is capable of giving me the children
4
+ # of complex attributes
5
+ class Accessor
6
+ def initialize(hash, prefix)
7
+ @hash = hash
8
+ @prefix = prefix
9
+ end
10
+
11
+ def get(name)
12
+ @hash[(@prefix.dup << name).join('.')]
13
+ end
14
+ end
15
+
16
+ # Base class for all config. Subclass this to be able to define your own
17
+ # schema.
18
+ class Base
19
+
20
+ @@CURRENT_PATH = []
21
+ @@GETTER = Object.new()
22
+ @@VALUES = {}
23
+
24
+ def self.get(path)
25
+ @@VALUES[path]
26
+ end
27
+
28
+ def self.schema schema_hash
29
+ @@SCHEMA = schema_hash
30
+ setter = hash_keys(schema_hash)
31
+ setter.each do |setter_name|
32
+ puts "call def_setter for #{setter_name}"
33
+ def_setter(setter_name)
34
+ end
35
+
36
+ schema_hash.each_pair do |k,v|
37
+ if v.is_a?(Hash)
38
+ child_accessor = mk_child_accessor([k], v)
39
+ send :define_singleton_method, k do
40
+ child_accessor
41
+ end
42
+ else
43
+ send :define_singleton_method, k do
44
+ @@VALUES[k.to_s]
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def self.mk_child_accessor(prefix, hash)
51
+ accessor = Accessor.new(@@VALUES, prefix)
52
+ hash.each_pair do |child_name,child|
53
+ if child.is_a?(Hash)
54
+ child_accessor = mk_child_accessor(prefix.dup << child_name, child)
55
+ accessor.instance_eval do
56
+ def child_name
57
+ child_accessor
58
+ end
59
+ end
60
+ else
61
+ accessor.instance_eval <<-"end_eval"
62
+ def #{child_name} *args
63
+ self.get("#{child_name}")
64
+ end
65
+ end_eval
66
+ end
67
+ end
68
+ accessor
69
+ end
70
+
71
+
72
+ def self.def_setter(name)
73
+ send :define_singleton_method, name.to_s + '!' do |value = @@GETTER, &blk|
74
+ if blk != nil
75
+ @@CURRENT_PATH << name
76
+ blk.call
77
+ @@CURRENT_PATH.pop()
78
+ else
79
+ @@CURRENT_PATH << name
80
+ set_value(@@CURRENT_PATH, value)
81
+ @@CURRENT_PATH.pop()
82
+ end
83
+ end
84
+ end
85
+
86
+ def self.set_value(keys, value)
87
+ @@VALUES[keys.join('.')] = value
88
+ end
89
+
90
+ def self.hash_keys hash
91
+ res = []
92
+ hash.each_pair do |k,v|
93
+ res << k
94
+ if v.is_a?(Hash)
95
+ res += hash_keys(v)
96
+ end
97
+ end
98
+ res
99
+ end
100
+
101
+ def self.print
102
+ @@VALUES.each_pair do |k,v|
103
+ puts "#{k}: #{v}"
104
+ end
105
+ nil
106
+ end
107
+ end
108
+ end
109
+
@@ -0,0 +1,3 @@
1
+ module Saveconf
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "saveconf/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "saveconf"
7
+ s.version = Saveconf::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Axel Tetzlaff"]
10
+ s.email = ["axel.tetzlaff@fortytools.com"]
11
+ s.homepage = "http://www.fortytools.com"
12
+ s.summary = %q{A configuration DSL}
13
+ s.description = %q{Lets you specify configuration schemes and configurations in a manner, that they can be ckecked at compile time}
14
+
15
+ s.rubyforge_project = "saveconf"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'saveconf'
3
+
4
+ class ConfigSchema < Saveconf::Base
5
+ schema(
6
+ host: 'some important server',
7
+ web_app: {
8
+ host: 'another important server',
9
+ user: 'user for basic auth',
10
+ password: 'basic auth pw'
11
+ }
12
+ )
13
+ end
14
+
15
+ class Production < ConfigSchema
16
+ host! "http://localhost:8080"
17
+
18
+ web_app! do
19
+ host! "http://testing.local/"
20
+ user! "matt"
21
+ password! "xxx"
22
+ end
23
+
24
+ end
25
+
26
+ class UnitTests < Test::Unit::TestCase
27
+ def test_flat_attribut
28
+ Production.print
29
+ assert_equal "http://localhost:8080", Production.host
30
+ end
31
+
32
+ def test_complex_attribut
33
+ assert_equal "xxx", Production.web_app.password
34
+ assert_equal "http://testing.local/", Production.web_app.host
35
+ assert_equal "matt", Production.web_app.user
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saveconf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Axel Tetzlaff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-20 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Lets you specify configuration schemes and configurations in a manner,
15
+ that they can be ckecked at compile time
16
+ email:
17
+ - axel.tetzlaff@fortytools.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - lib/saveconf.rb
27
+ - lib/saveconf/config.rb
28
+ - lib/saveconf/version.rb
29
+ - saveconf.gemspec
30
+ - test/.unit.rb.swp
31
+ - test/unit.rb
32
+ homepage: http://www.fortytools.com
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project: saveconf
52
+ rubygems_version: 1.8.10
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A configuration DSL
56
+ test_files:
57
+ - test/unit.rb