use-config 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2008 Slava Chernobai <chernobai@gmail.com>
2
+
3
+ This is free software. You may redistribute copies of it under the terms
4
+ of the GNU General Public License.
5
+
6
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
7
+
data/README ADDED
@@ -0,0 +1,34 @@
1
+ == UseConfig 0.1.1
2
+
3
+ This library allows Ruby classes to use configuration stored in YAML file,
4
+ or several configurations in several files.
5
+
6
+ === Installation
7
+
8
+ gem ins use-config
9
+
10
+ === Usage
11
+
12
+ Create configuration file 'qwe.yaml'.
13
+
14
+ project:
15
+ name: my_project
16
+ title: Project using UseConfig
17
+
18
+ Sample code:
19
+
20
+ $:.unshift 'lib'
21
+
22
+ require 'use_config'
23
+ include UseConfig
24
+
25
+ class UseConfigExample
26
+ use_config :qwe
27
+ end
28
+
29
+ print 'UseConfig Example', "\n"
30
+
31
+ ex = UseConfigExample.new
32
+ print ' ex.qwe.common.name: ', ex.qwe.common.name, "\n"
33
+ print ' ex.qwe.common.title: ', ex.qwe.common.title, "\n"
34
+
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+ require 'rake/gempackagetask'
7
+
8
+ $:.insert(0, File.dirname(__FILE__) + '/lib')
9
+
10
+ require 'use_config'
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = 'use-config'
14
+ s.version = UseConfig::VERSION
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = %w[ README LICENSE ]
18
+ s.rdoc_options = %w[ --main=README --line-numbers --inline-source ]
19
+ s.summary = 'UseConfig. Allows Ruby classes to use configuration, stored in YAML files.'
20
+ s.description = s.summary
21
+ s.author = 'Slava Chernobai'
22
+ s.email = 'chernobai@gmail.com'
23
+ s.homepage = 'http://code.slava.pp.ru/use-config/'
24
+ s.files = %w[ README LICENSE Rakefile ] + Dir.glob("{config,lib,test}/*") + Dir.glob('lib/*/*')
25
+ s.require_path = 'lib'
26
+ s.add_dependency 'hash-access', '>= 0.1.0'
27
+ end
28
+
29
+ task :default => [ :test ]
30
+
31
+ CLEAN.include(%w[ pkg doc README ])
32
+
33
+ desc 'Generate README'
34
+ file 'README' do
35
+ %x[erb -T 1 README.erb > README] if File.exists?('README.erb')
36
+ end
37
+
38
+ desc 'Generate RDoc'
39
+ task :rdoc => [ :README ]
40
+ Rake::RDocTask.new do |rd|
41
+ rd.main = 'README'
42
+ rd.title = 'UseConfig'
43
+ rd.rdoc_dir = 'doc'
44
+ rd.template = 'jamis'
45
+ rd.options = %w[ --line-numbers --inline-source ]
46
+ rd.rdoc_files.include('README', 'LICENSE', 'lib/*.rb', 'lib/*/*.rb')
47
+ end
48
+
49
+ desc 'Upload RDoc to site'
50
+ task :rdoc_pub => [:rdoc] do
51
+ sh 'rsync -ae ssh --delete doc/ slava@slava.cn:/var/www/code/htdocs/use-config/doc/'
52
+ end
53
+
54
+ task :test => [ :README ]
55
+ Rake::TestTask.new do |t|
56
+ t.libs << 'test' << 'lib'
57
+ t.test_files = FileList[ 'test/test*.rb' ].sort
58
+ t.verbose = true
59
+ end
60
+
61
+ task :gem => [ :README ]
62
+ Rake::GemPackageTask.new(spec) do |p|
63
+ p.gem_spec = spec
64
+ p.need_tar = true
65
+ p.need_zip = true
66
+ end
67
+
@@ -0,0 +1,7 @@
1
+ common:
2
+ name: use_config_example
3
+ title: UseConfig Example
4
+
5
+ uses_use_config:
6
+ title: Class That Uses UseConfig
7
+
@@ -0,0 +1,10 @@
1
+ common:
2
+ name: use_config_example
3
+ title: UseConfig Example
4
+ other:
5
+ first: First
6
+ second: Second
7
+
8
+ uses_use_config:
9
+ title: Class That Uses UseConfig
10
+
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'use_config/configuration'
3
+
4
+ # Extends calling class with UseConfig::ClassMethods when included
5
+ module UseConfig
6
+ VERSION = '0.1.1'
7
+
8
+ def self.included(base) # :nodoc:
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ # Extends Object class with UseConfig::ClassMethods when included
13
+ module ObjectExtend
14
+ def self.included(base) # :nodoc:
15
+ Object.extend ClassMethods
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ # Adds configuration hash readed from config/name.yaml file
21
+ # to configuration class, and generates configuration access methods.
22
+ def use_config(name, options = {}, &block)
23
+ metaclass = class << self; self; end
24
+
25
+ if self.respond_to? name
26
+ conf = self.send(name)
27
+ unless conf.respond_to? :configuration? and conf.configuration?
28
+ raise "Method '#{name}' already in use"
29
+ end
30
+ end
31
+
32
+ metaclass.instance_eval do
33
+ attr_accessor name
34
+ end
35
+ self.send "#{name}=".to_sym, UseConfig::Configuration.add_conf(self, name, options, &block)
36
+
37
+ class_eval <<-EVAL
38
+ def #{name}
39
+ self.class.#{name}
40
+ end
41
+ EVAL
42
+ end # def use_config(name, options = {}, &block)
43
+
44
+ # Removes configuration access methods
45
+ def drop_config(name, options = {})
46
+ if respond_to? name
47
+ conf = send(name)
48
+ if conf.respond_to? :configuration? and conf.configuration?
49
+ UseConfig::Configuration.drop_conf(self, name)
50
+ metaclass = class << self; self; end
51
+ metaclass.instance_eval do
52
+ remove_method name.to_sym
53
+ end
54
+ remove_method name.to_sym
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ end
61
+
@@ -0,0 +1,151 @@
1
+ require 'rubygems'
2
+ require 'hash_access'
3
+ require 'yaml'
4
+ require 'thread'
5
+
6
+ module UseConfig
7
+ include HashAccess
8
+
9
+ class Configuration < Hash
10
+ class << self
11
+ attr_accessor :accessors
12
+ attr_accessor :default_config_path
13
+ attr_accessor :extentions
14
+ attr_accessor :conf
15
+ attr_accessor :settings
16
+ attr_accessor :mutex
17
+ end
18
+
19
+ self.mutex = Mutex.new
20
+
21
+ self.conf = {}
22
+ self.settings = {}
23
+
24
+ self.accessors = [ :name, :config_path, :config_file, :use_config_file, :extentions, :used_by ]
25
+ self.default_config_path = [ '.', 'config' ]
26
+ self.extentions = [ 'yml', 'yaml' ]
27
+
28
+ self.accessors.each do |a|
29
+ attr_accessor a
30
+ end
31
+
32
+ def self.add_conf(used_by, name, options = {}, &block)
33
+ sname = name.to_s
34
+ self.mutex.synchronize do
35
+ if self.conf[sname]
36
+ self.conf[sname].reload! if self.settings[:reload_when_add]
37
+ self.conf[sname].used_by_add(used_by)
38
+ yield self.conf[sname] if block_given?
39
+ else
40
+ self.conf[sname] = UseConfig::Configuration.new(sname, options, &block)
41
+ self.conf[sname].used_by_add(used_by)
42
+ end
43
+ self.conf[sname]
44
+ end # self.mutex.synchronize do
45
+ end
46
+
47
+ def self.drop_conf(used_by, name)
48
+ sname = name.to_s
49
+ self.mutex.synchronize do
50
+ if self.conf[sname]
51
+ self.conf[sname].used_by_drop(used_by)
52
+ if self.conf[sname].used_by.size == 0
53
+ self.conf.delete(sname)
54
+ end
55
+ end
56
+ end # self.mutex.synchronize do
57
+ end
58
+
59
+ # Instance methods
60
+
61
+ def configuration?
62
+ true
63
+ end
64
+
65
+ def initialize(name, options = {}, &block)
66
+ self.name = name.to_s
67
+ self.config_path = self.class.default_config_path.clone
68
+ self.config_file = nil
69
+ self.use_config_file = true
70
+ self.extentions = self.class.extentions
71
+ self.used_by = []
72
+ if options[:file]
73
+ self.config_file = options[:file]
74
+ end
75
+ if options[:path_insert]
76
+ if options[:path_insert].is_a? Array
77
+ options[:path_insert].reverse.each do |dir|
78
+ self.config_path.unshift(dir)
79
+ end
80
+ else
81
+ self.config_path.unshift(options[:path_insert])
82
+ end
83
+ end
84
+ if options[:empty]
85
+ self.use_config_file = false
86
+ end
87
+ if use_config_file and config_file.nil?
88
+ config_file_find
89
+ if config_file.nil?
90
+ raise "Configuration file not found"
91
+ end
92
+ load_configuration
93
+ end
94
+ access_by_methods
95
+ yield self if block_given?
96
+ self
97
+ end # def initialize(name, options = {}, &block)
98
+
99
+ def used_by_add(used_by)
100
+ self.used_by.push(used_by) unless self.used_by.include?(used_by)
101
+ self
102
+ end
103
+
104
+ def used_by_drop(used_by)
105
+ if self.used_by.include?(used_by)
106
+ self.used_by.delete(used_by)
107
+ end
108
+ self
109
+ end
110
+
111
+ def config_file_find
112
+ self.config_path.each do |dir|
113
+ self.extentions.each do |ext|
114
+ file = "#{dir}/#{name}.#{ext}"
115
+ if File.exist?(file)
116
+ self.config_file = file
117
+ return self
118
+ end
119
+ end
120
+ end
121
+ self
122
+ end # def config_file_find
123
+
124
+ def load_configuration
125
+ cfg = YAML.load_file(config_file)
126
+ cfg.each do |key, value|
127
+ next if self.class.accessors.include?(key.to_sym)
128
+ self[key] = value
129
+ end
130
+ self
131
+ end
132
+
133
+ def clear!
134
+ each_key do |key|
135
+ next if self.class.accessors.include?(key.to_sym)
136
+ delete(key)
137
+ end
138
+ yield self if block_given?
139
+ self
140
+ end
141
+
142
+ def reload!
143
+ clear!
144
+ load_configuration if use_config_file
145
+ yield self if block_given?
146
+ self
147
+ end
148
+
149
+ end # class Configuration
150
+ end # module UseConfig
151
+
@@ -0,0 +1,90 @@
1
+ require 'test/unit'
2
+ require 'use_config/configuration'
3
+
4
+ class UseConfigConfigurationTest < Test::Unit::TestCase
5
+ def test_010
6
+ cfg = UseConfig::Configuration.new(:zxc, :empty => true)
7
+ assert_instance_of(UseConfig::Configuration, cfg)
8
+ assert_respond_to(cfg, :name)
9
+ assert_respond_to(cfg, :config_path)
10
+ assert_respond_to(cfg, :config_file)
11
+ assert_respond_to(cfg, :config_file_find)
12
+ assert_respond_to(cfg, :configuration?)
13
+ end
14
+
15
+ def test_020
16
+ cfg = UseConfig::Configuration.new(:zxc, :empty => true)
17
+ assert_equal(true, cfg.configuration?)
18
+ assert_equal('zxc', cfg.name)
19
+ assert_equal([ '.', 'config' ], cfg.config_path)
20
+ assert_nil(cfg.config_file)
21
+ end
22
+
23
+ def test_030
24
+ assert_raise RuntimeError do
25
+ UseConfig::Configuration.new :zxc
26
+ end
27
+ assert_raise RuntimeError do
28
+ UseConfig::Configuration.new(:zxc, :empty => false)
29
+ end
30
+ end
31
+
32
+ def test_040
33
+ cfg = UseConfig::Configuration.new(:qwe)
34
+ assert_equal(true, cfg.configuration?)
35
+ assert_equal('qwe', cfg.name)
36
+ assert_equal([ '.', 'config' ], cfg.config_path)
37
+ assert_equal('config/qwe.yml', cfg.config_file)
38
+ end
39
+
40
+ def test_050
41
+ cfg = UseConfig::Configuration.new(:qwe, :path_insert => '123456')
42
+ assert_equal([ '123456', '.', 'config' ], cfg.config_path)
43
+ cfg = UseConfig::Configuration.new(:qwe, :path_insert => [ '123456', '654321' ])
44
+ assert_equal([ '123456', '654321', '.', 'config' ], cfg.config_path)
45
+ end
46
+
47
+ def test_060
48
+ cfg = UseConfig::Configuration.new(:zxc, :empty => true) do |c|
49
+ c.qwerty = 11
50
+ c.asdfgh = 22
51
+ c.zxcvbn = 33
52
+ c.one.two.three.four = 44
53
+ end
54
+ assert_equal(11, cfg.qwerty)
55
+ assert_equal(22, cfg.asdfgh)
56
+ assert_equal(33, cfg.zxcvbn)
57
+ assert_equal(44, cfg.one.two.three.four)
58
+ cfg.reload! do |c|
59
+ c.qwerty = 1111
60
+ c.asdfgh = 2222
61
+ end
62
+ assert_equal(1111, cfg.qwerty)
63
+ assert_equal(2222, cfg.asdfgh)
64
+ assert_equal({}, cfg.zxcvbn)
65
+ assert_equal({}, cfg.one.two.three.four)
66
+ end
67
+
68
+ def test_070
69
+ cfg = UseConfig::Configuration.new(:qwe)
70
+ assert_equal('use_config_example', cfg.common.name)
71
+ assert_equal('UseConfig Example', cfg.common.title)
72
+ assert_equal('First', cfg.common.other.first)
73
+ end
74
+
75
+ def test_080
76
+ cfg = UseConfig::Configuration.new(:qwe)
77
+ cfg.common = 'Common'
78
+ assert_equal('Common', cfg.common)
79
+ cfg.clear!
80
+ assert_nil(cfg['common'])
81
+ cfg.common = 'Common'
82
+ cfg.reload!
83
+ assert_equal('use_config_example', cfg.common.name)
84
+ assert_equal('UseConfig Example', cfg.common.title)
85
+ assert_equal('First', cfg.common.other.first)
86
+ cfg.common.other.first = 'Another First'
87
+ assert_equal('Another First', cfg.common.other.first)
88
+ end
89
+ end
90
+
@@ -0,0 +1,74 @@
1
+ require 'test/unit'
2
+ require 'use_config'
3
+
4
+ class UsesUseConfig
5
+ include UseConfig
6
+ use_config :qwe
7
+ use_config :asd
8
+ def self.name_in_use
9
+ true
10
+ end
11
+ end
12
+
13
+ class UsesUseConfigSecond
14
+ include UseConfig
15
+ end
16
+
17
+ class UsesUseConfigThird
18
+ include UseConfig
19
+ end
20
+
21
+ class UseConfigTest < Test::Unit::TestCase
22
+ def test_010_sender_class_extend
23
+ assert_respond_to(UsesUseConfig, :use_config)
24
+ assert_respond_to(UsesUseConfig, :qwe)
25
+ assert_respond_to(UsesUseConfig, :asd)
26
+ end
27
+
28
+ def test_020_object_class_extend
29
+ UsesUseConfig.send :include, UseConfig::ObjectExtend
30
+ assert_respond_to(Object, :use_config)
31
+ end
32
+
33
+ def test_030
34
+ 5.times do
35
+ UsesUseConfig.use_config :qwe
36
+ end
37
+ assert_equal(1, UsesUseConfig.qwe.used_by.size)
38
+ UsesUseConfigSecond.use_config :qwe
39
+ assert_equal(2, UsesUseConfig.qwe.used_by.size)
40
+ UsesUseConfigThird.use_config :qwe
41
+ assert_equal(3, UsesUseConfig.qwe.used_by.size)
42
+ UsesUseConfigThird.drop_config :qwe
43
+ assert_equal(2, UsesUseConfig.qwe.used_by.size)
44
+ UsesUseConfigSecond.drop_config :qwe
45
+ assert_equal(1, UsesUseConfig.qwe.used_by.size)
46
+ UsesUseConfig.drop_config :qwe
47
+ assert_equal(false, UsesUseConfig.respond_to?(:qwe))
48
+ assert_equal(false, UsesUseConfig.instance_methods.include?(:qwe))
49
+ UsesUseConfig.drop_config :qwe
50
+ end
51
+
52
+ def test_040
53
+ UsesUseConfig.use_config :qwe
54
+ assert_equal('use_config_example', UsesUseConfig.qwe.common.name)
55
+ UsesUseConfig.qwe.clear!
56
+ assert_equal({}, UsesUseConfig.qwe.common)
57
+ UsesUseConfig.qwe.reload!
58
+ assert_equal('use_config_example', UsesUseConfig.qwe.common.name)
59
+ end
60
+
61
+ def test_050
62
+ assert_raise RuntimeError do
63
+ UsesUseConfig.use_config :name_in_use
64
+ end
65
+ end
66
+
67
+ def test_050
68
+ UsesUseConfig.use_config :qwe
69
+ inst = UsesUseConfig.new
70
+ assert_respond_to(inst, :qwe)
71
+ assert_equal('use_config_example', inst.qwe.common.name)
72
+ end
73
+ end
74
+
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: use-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Slava Chernobai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-18 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hash-access
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.0
23
+ version:
24
+ description: UseConfig. Allows Ruby classes to use configuration, stored in YAML files.
25
+ email: chernobai@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ files:
34
+ - README
35
+ - LICENSE
36
+ - Rakefile
37
+ - config/asd.yml
38
+ - config/qwe.yml
39
+ - lib/use_config.rb
40
+ - lib/use_config
41
+ - test/test_020_use_config.rb
42
+ - test/test_010_configuration.rb
43
+ - lib/use_config/configuration.rb
44
+ has_rdoc: true
45
+ homepage: http://code.slava.pp.ru/use-config/
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main=README
49
+ - --line-numbers
50
+ - --inline-source
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.0.1
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: UseConfig. Allows Ruby classes to use configuration, stored in YAML files.
72
+ test_files: []
73
+