use-config 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 Slava Chernobai <chernobai@gmail.com>
1
+ Copyright (c) 2008, 2011 Svetoslav Chernobay <slava@chernobay.info>
2
2
 
3
3
  This is free software. You may redistribute copies of it under the terms
4
4
  of the GNU General Public License.
@@ -0,0 +1,77 @@
1
+ # UseConfig
2
+
3
+ Easy configuration solution for any Ruby class.
4
+
5
+ [UseConfig](https://github.com/slavach/use-config) allows a Ruby class to use
6
+ configuration stored in a YAML file.
7
+
8
+ Configuration is loaded by a single line of code, like:
9
+
10
+ use_config :conf
11
+
12
+ Once been loaded, configuration values are accessible by calling to the
13
+ corresponding methods:
14
+
15
+ conf.project.name
16
+
17
+ ## Installation
18
+
19
+ UseConfig is available as the gem use-config.
20
+
21
+ gem ins use-config
22
+
23
+ ## Usage Example
24
+
25
+ First, create the configuration file conf.yaml, containing a hash
26
+ representation:
27
+
28
+ project:
29
+ name: use-config-demo
30
+ title: UseConfig Demo Project
31
+
32
+ messages:
33
+ welcome: Welcome to the UseConfig Demo
34
+ lorem: Lorem ipsum dolor sit amet, consectetur adipisicing elit
35
+
36
+ Next, create a ruby program that uses the above configuration:
37
+
38
+ require 'use_config'
39
+ include UseConfig
40
+
41
+ class UseConfigDemo
42
+ use_config :conf # Creates a hash accessible by the 'conf' method
43
+
44
+ def show_project
45
+ print 'Name: ', conf.project.name, "\n"
46
+ print 'Title: ', conf.project.title, "\n"
47
+ end
48
+ end
49
+
50
+ demo = UseConfigDemo.new
51
+
52
+ demo.show_project
53
+
54
+ print demo.conf.messages.welcome, "\n"
55
+ print demo.conf.messages.lorem, "\n"
56
+
57
+ ## Development
58
+
59
+ Clone the latest code from Github:
60
+
61
+ git clone git://github.com/slavach/use-config.git
62
+
63
+ Install bundler:
64
+
65
+ gem install bundler
66
+
67
+ Install required gems:
68
+
69
+ bundle install
70
+
71
+ To invoke tests - run `rake spec` or simply `rake`.
72
+
73
+ ## Author
74
+
75
+ UseConfig has been written by Svetoslav Chernobay <slava@chernobay.info>.
76
+ Feel free to contact him if you have any questions or comments.
77
+
data/Rakefile CHANGED
@@ -1,67 +1,37 @@
1
1
  require 'rubygems'
2
2
 
3
3
  require 'rake/clean'
4
- require 'rake/testtask'
5
4
  require 'rake/rdoctask'
6
- require 'rake/gempackagetask'
7
5
 
8
- $:.insert(0, File.dirname(__FILE__) + '/lib')
6
+ require 'yard'
7
+ require 'rspec'
8
+ require 'rspec/core/rake_task'
9
9
 
10
- require 'use_config'
10
+ require File.dirname(__FILE__) + "/lib/use_config/version.rb"
11
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 ]
12
+ task :default => [ :spec ]
30
13
 
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
14
+ CLEAN.include(%w[ pkg doc .yardoc use-config-*.gem ])
37
15
 
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')
16
+ desc 'Run RSpec examples'
17
+ RSpec::Core::RakeTask.new :spec do |t|
18
+ t.rspec_opts = "--colour --format documentation"
19
+ t.pattern = 'spec/*.rb'
47
20
  end
48
21
 
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/'
22
+ desc "Build the gem"
23
+ task :build do
24
+ system "gem build use-config.gemspec"
52
25
  end
53
26
 
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
27
+ desc "Push the gem to rubygems.org"
28
+ task :push do
29
+ system "gem push use-config-#{UseConfig::VERSION}.gem"
59
30
  end
60
31
 
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
32
+ desc "Generate documentation"
33
+ YARD::Rake::YardocTask.new :doc do |t|
34
+ t.files = %w[ README.md, LICENSE, lib/**/*.rb ]
35
+ t.options = %w[ --title UseConfig ]
66
36
  end
67
37
 
@@ -1,21 +1,22 @@
1
- require 'rubygems'
1
+ require 'use_config/version'
2
2
  require 'use_config/configuration'
3
3
 
4
- # Extends calling class with UseConfig::ClassMethods when included
4
+ # Extends the calling class with UseConfig::ClassMethods
5
5
  module UseConfig
6
- VERSION = '0.1.1'
7
-
8
6
  def self.included(base) # :nodoc:
9
7
  base.extend ClassMethods
10
8
  end
11
9
 
12
- # Extends Object class with UseConfig::ClassMethods when included
10
+ # Extends the Object class with UseConfig::ClassMethods
11
+ # When UseConfig::ObjectExtend included, all the UseConfig::ClassMethods
12
+ # methods been added to Object and all its derivatives.
13
13
  module ObjectExtend
14
14
  def self.included(base) # :nodoc:
15
15
  Object.extend ClassMethods
16
16
  end
17
17
  end
18
18
 
19
+ # This module contains class methods use_config and drop_config.
19
20
  module ClassMethods
20
21
  # Adds configuration hash readed from config/name.yaml file
21
22
  # to configuration class, and generates configuration access methods.
@@ -29,11 +30,13 @@ module UseConfig
29
30
  end
30
31
  end
31
32
 
33
+ # Generates class accessor.
32
34
  metaclass.instance_eval do
33
35
  attr_accessor name
34
36
  end
35
37
  self.send "#{name}=".to_sym, UseConfig::Configuration.add_conf(self, name, options, &block)
36
38
 
39
+ # Generates instance accessor.
37
40
  class_eval <<-EVAL
38
41
  def #{name}
39
42
  self.class.#{name}
@@ -56,6 +59,5 @@ module UseConfig
56
59
  end
57
60
  end
58
61
  end
59
-
60
62
  end
61
63
 
@@ -1,49 +1,110 @@
1
1
  require 'rubygems'
2
- require 'hash_access'
3
2
  require 'yaml'
4
3
  require 'thread'
5
4
 
6
- module UseConfig
7
- include HashAccess
5
+ require 'hash_access'
8
6
 
7
+ module UseConfig
8
+ # UseConfig::Configuration class plays two roles.
9
+ #
10
+ # An instance of the class is a hash, a configuration placeholder.
11
+ # It contents could be loaded from a YAML file or could be populated
12
+ # manually. Configuration values can be accessed using the hash notation
13
+ # +object['key']+ as well as using the method notation +object.key+.
14
+ #
15
+ # The class itself is a container. It containas above-mentioned configuration
16
+ # objects that are stored in the +conf+ hash attribute which is accessible
17
+ # by the +conf+ method.
18
+ #
19
+ # A particular instance should be created with the +add_conf+ method.
20
+ # It is not intended to use an instance as a standalone object.
21
+ #
22
+ # Usage example:
23
+ #
24
+ # UseConfig::Configuration.add_conf self, :qwerty, :empty => true
25
+ # UseConfig::Configuration.conf.qwerty.param_1 = 'Value 1'
26
+ # print UseConfig::Configuration.conf.qwerty.to_s, "\n"
27
+ #
28
+ # This class is designed as the UseConfig supplemental class. It is not very
29
+ # useful by its own.
9
30
  class Configuration < Hash
31
+ include HashAccess
32
+
10
33
  class << self
11
- attr_accessor :accessors
12
- attr_accessor :default_config_path
34
+ attr_accessor :path
13
35
  attr_accessor :extentions
36
+ attr_accessor :reload_when_add
37
+ attr_accessor :default_instance_properties
14
38
  attr_accessor :conf
15
- attr_accessor :settings
16
39
  attr_accessor :mutex
17
40
  end
18
41
 
19
- self.mutex = Mutex.new
42
+ self.path = %w[. config]
43
+ self.extentions = %w[yml yaml]
44
+ self.reload_when_add = false
45
+
46
+ self.default_instance_properties = {
47
+ :name => nil,
48
+ :file => nil,
49
+ :use_file => true,
50
+ :path => self.path,
51
+ :extentions => self.extentions,
52
+ :used_by => [],
53
+ }
20
54
 
21
55
  self.conf = {}
22
- self.settings = {}
23
56
 
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' ]
57
+ self.mutex = Mutex.new
27
58
 
28
- self.accessors.each do |a|
29
- attr_accessor a
59
+ # Passes self to calling block.
60
+ #
61
+ # Example:
62
+ #
63
+ # UseConfig::Configuration.configure do |c|
64
+ # c.path << APP_ROOT + 'config'
65
+ # end
66
+ #
67
+ def self.configure
68
+ yield self if block_given?
30
69
  end
31
70
 
71
+ # Creates a new config (an instance of +self+) (calls +self.new+)
72
+ # and inserts the result object it into +self.conf+ hash as
73
+ # +self.conf[name]+ element. If the config already exists and the
74
+ # +:reload_when_add+ class setting is set - reloads config contents.
75
+ #
76
+ # Returns the created config object.
77
+ #
78
+ # Parameters
79
+ #
80
+ # * +used_by+ - The class that uses the config. After the config created
81
+ # created, this class is assotiated with it. This prevents the config to
82
+ # be deleted if any class uses it.
83
+ # * +name+ - Config name. It is used as the +conf+ hash key as well as the
84
+ # attribute of the config itself.
85
+ # * +options+ - The options hash to be passed to +new+.
86
+ #
87
+ # Options
88
+ #
89
+ # * +:empty+ - Create an empty config. Don't load config from file.
90
+ # * +:file+ - Use the mentioned file instead of the default.
91
+ # files' search path.
32
92
  def self.add_conf(used_by, name, options = {}, &block)
33
93
  sname = name.to_s
34
94
  self.mutex.synchronize do
35
95
  if self.conf[sname]
36
- self.conf[sname].reload! if self.settings[:reload_when_add]
96
+ self.conf[sname].reload! if self.reload_when_add
37
97
  self.conf[sname].used_by_add(used_by)
38
98
  yield self.conf[sname] if block_given?
39
99
  else
40
- self.conf[sname] = UseConfig::Configuration.new(sname, options, &block)
100
+ self.conf[sname] = self.new(sname, options, &block)
41
101
  self.conf[sname].used_by_add(used_by)
42
102
  end
43
103
  self.conf[sname]
44
104
  end # self.mutex.synchronize do
45
105
  end
46
106
 
107
+ # Deletes the config if it is not in use.
47
108
  def self.drop_conf(used_by, name)
48
109
  sname = name.to_s
49
110
  self.mutex.synchronize do
@@ -56,96 +117,137 @@ module UseConfig
56
117
  end # self.mutex.synchronize do
57
118
  end
58
119
 
59
- # Instance methods
120
+ # Deletes all configs.
121
+ def self.reset!
122
+ self.mutex.synchronize do
123
+ self.conf = {}
124
+ end
125
+ end
126
+
127
+ # Instance behavior
60
128
 
129
+ attr_accessor :instance_properties
130
+
131
+ # What's this?
61
132
  def configuration?
62
133
  true
63
134
  end
64
135
 
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
136
+ # Passes self to a calling block.
137
+ def configure(&block)
138
+ yield self if block_given?
139
+ end
140
+
141
+ # Initializes a new instance. Load configuration from file if given.
142
+ #
143
+ # Parameters:
144
+ #
145
+ # * +name+ - The object's name.
146
+ # * +options+ - Options' hash.
147
+ #
148
+ # Options:
149
+ #
150
+ # * +:empty: - Create empty object, don't read configuration from file.
151
+ # * No other options yet.
152
+ #
153
+ # Notes:
154
+ #
155
+ # Doesn't add any +use_by+ properties, even being passed in +options+.
156
+ # +use_by+ should be added later, by the caller, using +add_use_by+
157
+ # method.
158
+ def initialize(name, options, &block)
159
+ self.instance_properties = {}.access_by_methods
160
+ self.class.default_instance_properties.keys.each do |key|
161
+ if options[key].nil?
162
+ instance_properties[key] = self.class.default_instance_properties[key]
80
163
  else
81
- self.config_path.unshift(options[:path_insert])
164
+ instance_properties[key] = options[key]
82
165
  end
83
166
  end
167
+ instance_properties.name = name.to_s
168
+ instance_properties.used_by = []
84
169
  if options[:empty]
85
- self.use_config_file = false
170
+ instance_properties.use_file = false
86
171
  end
87
- if use_config_file and config_file.nil?
172
+ if instance_properties.use_file and instance_properties.file.nil?
88
173
  config_file_find
89
- if config_file.nil?
174
+ if instance_properties.file.nil?
90
175
  raise "Configuration file not found"
91
176
  end
92
177
  load_configuration
93
178
  end
94
- access_by_methods
179
+ self.access_by_methods
95
180
  yield self if block_given?
96
181
  self
97
- end # def initialize(name, options = {}, &block)
182
+ end # def initialize
183
+
184
+ private
98
185
 
186
+ # Sets +name+ property.
187
+ def set_name(name)
188
+ if name
189
+ self.instance_properties.name = name.to_s
190
+ end
191
+ end
192
+
193
+ # Adds the class to usage list.
99
194
  def used_by_add(used_by)
100
- self.used_by.push(used_by) unless self.used_by.include?(used_by)
101
- self
195
+ unless self.instance_properties.used_by.include?(used_by)
196
+ self.instance_properties.used_by.push(used_by)
197
+ end
102
198
  end
103
199
 
200
+ # Removes the class from usage list.
104
201
  def used_by_drop(used_by)
105
- if self.used_by.include?(used_by)
106
- self.used_by.delete(used_by)
202
+ if self.instance_properties.used_by.include?(used_by)
203
+ self.instance_properties.used_by.delete(used_by)
107
204
  end
108
- self
109
205
  end
110
206
 
207
+ # Returns +used_by+ property
208
+ def used_by
209
+ self.instance_properties.used_by
210
+ end
211
+
212
+ # Serches for configuration file.
111
213
  def config_file_find
112
- self.config_path.each do |dir|
113
- self.extentions.each do |ext|
114
- file = "#{dir}/#{name}.#{ext}"
214
+ instance_properties.path.each do |dir|
215
+ instance_properties.extentions.each do |ext|
216
+ file = "#{dir}/#{self.instance_properties.name}.#{ext}"
115
217
  if File.exist?(file)
116
- self.config_file = file
117
- return self
218
+ self.instance_properties.use_file = true
219
+ self.instance_properties.file = file
118
220
  end
119
221
  end
120
222
  end
121
- self
122
223
  end # def config_file_find
123
224
 
225
+ # Loads configuration from the +config_file+.
226
+ # TODO: rename this function to +load+
124
227
  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
228
+ begin
229
+ cfg = YAML.load_file(instance_properties.file)
230
+ cfg.each do |key, value|
231
+ self[key] = value
232
+ end
233
+ true
234
+ rescue
235
+ false
129
236
  end
130
- self
131
237
  end
132
238
 
239
+ # Removes all the keys from itself.
133
240
  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
241
+ self.clear
140
242
  end
141
243
 
244
+ # Reloads configuration from file.
142
245
  def reload!
143
- clear!
144
- load_configuration if use_config_file
145
- yield self if block_given?
146
- self
246
+ self.clear!
247
+ if (instance_properties.file && instance_properties.use_file)
248
+ load_configuration
249
+ end
147
250
  end
148
-
149
251
  end # class Configuration
150
252
  end # module UseConfig
151
253
 
@@ -0,0 +1,4 @@
1
+ module UseConfig
2
+ VERSION = '0.2.0'
3
+ end
4
+
@@ -0,0 +1,294 @@
1
+ # vim foldmethod=syntax
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+
6
+ require File.dirname(__FILE__) + "/../lib/use_config/configuration.rb"
7
+
8
+ describe UseConfig::Configuration, "instance public interface" do
9
+ before :each do
10
+ UseConfig::Configuration.reset!
11
+ UseConfig::Configuration.configure do |c|
12
+ c.path << "#{File.dirname(__FILE__)}/config"
13
+ end
14
+ end
15
+
16
+ describe "#initialize" do
17
+ describe "correct usage" do
18
+ context "when option :empty given" do
19
+ before :each do
20
+ @conf = UseConfig::Configuration.new :empty_conf,
21
+ :empty => true
22
+ end
23
+
24
+ it "is empty after creation" do
25
+ @conf.should == {}
26
+ end
27
+
28
+ it "assigns values by methods" do
29
+ @conf.core.name = 'conf'
30
+ @conf.core.name.should == 'conf'
31
+ end
32
+
33
+ it "has instance_properties" do
34
+ @conf.instance_properties.should_not == nil
35
+ end
36
+
37
+ it "has instance_properties.name" do
38
+ @conf.instance_properties.name.should == 'empty_conf'
39
+ end
40
+ end
41
+
42
+ context "when option :file given" do
43
+ it "loads configuration from file" do
44
+ c = UseConfig::Configuration.new :the_first_conf,
45
+ :file => 'first_conf.yaml'
46
+ c.core.name.should = 'first_conf'
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "incorrect usage" do
52
+ context "when config file missing" do
53
+ it "raises an error" do
54
+ expect {
55
+ UseConfig::Configuration.new :none_conf
56
+ }.to raise_error
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ describe UseConfig::Configuration, "private instance methods" do
64
+ before :each do
65
+ module ::UseConfig
66
+ class Configuration
67
+ public :set_name, :used_by_add, :used_by_drop, :used_by,
68
+ :config_file_find, :load_configuration,
69
+ :clear!, :reload!
70
+ end
71
+ end
72
+
73
+ @conf = UseConfig::Configuration.new :empty_conf,
74
+ :empty => true
75
+ end
76
+
77
+ describe "#set_name" do
78
+ it "changes name property" do
79
+ @conf.set_name :empty_conf_changed
80
+ @conf.instance_properties.name.should == 'empty_conf_changed'
81
+ end
82
+
83
+ it "doesn't change name when no parameter given" do
84
+ expect {
85
+ @conf.set_name
86
+ }.should raise_error
87
+ end
88
+ end
89
+
90
+ describe "#used_by_add" do
91
+ it "adds the element" do
92
+ @conf.used_by_add :class_one
93
+ @conf.used_by.include?(:class_one).should == true
94
+ @conf.used_by.count.should == 1
95
+ end
96
+
97
+ it "doesn't add the same element twice" do
98
+ @conf.used_by_add :class_two
99
+ @conf.used_by_add :class_two
100
+ @conf.used_by.count.should == 1
101
+ end
102
+
103
+ it "adds two different elements" do
104
+ @conf.used_by_add :class_three
105
+ @conf.used_by_add :class_four
106
+ @conf.used_by.count.should == 2
107
+ end
108
+ end
109
+
110
+ describe "#used_by_drop" do
111
+ it "removes the element" do
112
+ @conf.used_by_add :class_five
113
+ @conf.used_by_drop :class_five
114
+ @conf.used_by.count.should == 0
115
+ end
116
+
117
+ it "doesn't remove anything if the key is unknown" do
118
+ @conf.used_by_add :class_six
119
+ @conf.used_by_drop :non_existing_class
120
+ @conf.used_by.count.should == 1
121
+ end
122
+ end
123
+
124
+ describe "#used_by" do
125
+ it "returns used_by array" do
126
+ @conf.used_by_add :class_seven
127
+ @conf.used_by.should == [ :class_seven ]
128
+ end
129
+ end
130
+
131
+ describe "#config_file_find" do
132
+ it "finds the existing file" do
133
+ @conf.set_name :first_conf
134
+ @conf.config_file_find
135
+ @conf.instance_properties.file.should_not == nil
136
+ end
137
+
138
+ it "doesn't find any non-existent file" do
139
+ @conf.set_name :first_conf_missing
140
+ @conf.config_file_find
141
+ @conf.instance_properties.file.should == nil
142
+ end
143
+ end
144
+
145
+ describe "#load_configuration" do
146
+ it "loads configuration from the existing file" do
147
+ @conf.set_name :first_conf
148
+ @conf.config_file_find
149
+ @conf.load_configuration
150
+ @conf.core.name.should == 'first_conf'
151
+ end
152
+
153
+ it "doesn't load configuration if the file is missing" do
154
+ @conf.name = :first_conf
155
+ @conf.config_file = 'missing_file'
156
+ @conf.load_configuration.should == false
157
+ end
158
+ end
159
+
160
+ describe "#clear!" do
161
+ it "clears itself" do
162
+ @conf.qwerty = 'qwerty'
163
+ @conf.asdfgh = 'asdfgh'
164
+ @conf.clear!
165
+ @conf.keys.should == []
166
+ end
167
+ end
168
+
169
+ describe "#reload!" do
170
+ context "when uses config file" do
171
+ it "clears itself and loads configuration" do
172
+ @conf.set_name :first_conf
173
+ @conf.config_file_find
174
+ @conf.load_configuration
175
+ @conf.core.name = 'changed'
176
+ @conf.reload!
177
+ @conf.core.name.should == 'first_conf'
178
+ end
179
+ end
180
+
181
+ context "when doesn't use config file" do
182
+ it "clears itself" do
183
+ @conf.core.name = 'changed'
184
+ @conf.reload!
185
+ @conf.keys.should == []
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ describe UseConfig::Configuration, "class" do
192
+ before :each do
193
+ UseConfig::Configuration.reset!
194
+ UseConfig::Configuration.configure do |c|
195
+ c.path << "#{File.dirname(__FILE__)}/config"
196
+ end
197
+ end
198
+
199
+ describe "general class behaivior" do
200
+ end
201
+
202
+ describe ".add_conf" do
203
+ context "config file is not used" do
204
+ before :each do
205
+ UseConfig::Configuration.add_conf :some_class, :cfg,
206
+ :empty => true
207
+ end
208
+
209
+ it "creates a new empty config hash" do
210
+ UseConfig::Configuration.conf['cfg'].should == {}
211
+ end
212
+
213
+ it "hash values are accessible by methods" do
214
+ UseConfig::Configuration.conf['cfg'].a_key = 'a_value'
215
+ UseConfig::Configuration.conf['cfg'].a_key.should == 'a_value'
216
+ end
217
+
218
+ it "child hash values are accessinle by methods" do
219
+ UseConfig::Configuration.conf['cfg'].one.two.three = 'a_value'
220
+ UseConfig::Configuration.conf['cfg'].one.two.three == 'a_value'
221
+ end
222
+
223
+ it "keeps configuration when called the next time" do
224
+ UseConfig::Configuration.conf['cfg'].a_key = 'a_value'
225
+ UseConfig::Configuration.add_conf :another_class, :cfg,
226
+ :empty => true
227
+ UseConfig::Configuration.conf['cfg'].a_key.should == 'a_value'
228
+ end
229
+ end
230
+
231
+ context "config file present" do
232
+ before :each do
233
+ UseConfig::Configuration.add_conf :some_class, :first_conf
234
+ UseConfig::Configuration.add_conf :another_class, :second_conf
235
+ end
236
+
237
+ it "loads first configuration" do
238
+ UseConfig::Configuration.conf['first_conf'].should_not == nil
239
+ end
240
+
241
+ it "loads second configuration" do
242
+ UseConfig::Configuration.conf['second_conf'].should_not == nil
243
+ end
244
+
245
+ it "first configuration is correct" do
246
+ UseConfig::Configuration.conf['first_conf'].core.name.should == 'first_conf'
247
+ end
248
+
249
+ it "second configuration is correct" do
250
+ UseConfig::Configuration.conf['second_conf'].name.should == 'second_conf'
251
+ end
252
+ end
253
+
254
+ context "config file missing" do
255
+ it "raises error" do
256
+ expect {
257
+ UseConfig::Configuration.add_conf :some_class, :missing_conf
258
+ }.to raise_error
259
+ end
260
+ end
261
+ end
262
+
263
+ describe ".drop_conf" do
264
+ before :each do
265
+ UseConfig::Configuration.add_conf :some_class, :cfg,
266
+ :empty => true
267
+ end
268
+
269
+ context "configuration is used by a single class" do
270
+ it "destroys configuration" do
271
+ UseConfig::Configuration.drop_conf :some_class, :cfg
272
+ UseConfig::Configuration.conf['cfg'].should == nil
273
+ end
274
+ end
275
+
276
+ context "configuration is used by two classes" do
277
+ it "keeps configuration when called once" do
278
+ UseConfig::Configuration.add_conf :another_class, :cfg,
279
+ :empty => true
280
+ UseConfig::Configuration.drop_conf :some_class, :cfg
281
+ UseConfig::Configuration.conf['cfg'].should_not == nil
282
+ end
283
+
284
+ it "destroys configuration when called a second time" do
285
+ UseConfig::Configuration.add_conf :another_class, :cfg,
286
+ :empty => true
287
+ UseConfig::Configuration.drop_conf :some_class, :cfg
288
+ UseConfig::Configuration.drop_conf :another_class, :cfg
289
+ UseConfig::Configuration.conf['cfg'].should == nil
290
+ end
291
+ end
292
+ end
293
+ end
294
+
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ require File.dirname(__FILE__) + "/../lib/use_config.rb"
5
+
6
+ class Sample; include UseConfig; end
7
+
8
+ describe UseConfig do
9
+ before :each do
10
+ UseConfig::Configuration.reset!
11
+ UseConfig::Configuration.configure do |c|
12
+ c.path << "#{File.dirname(__FILE__)}/config"
13
+ end
14
+ end
15
+
16
+ context "Sample class includes UseConfig" do
17
+ it "has method use_config" do
18
+ Sample.respond_to?(:use_config).should == true
19
+ end
20
+
21
+ it "has method drop_config" do
22
+ Sample.respond_to?(:drop_config).should == true
23
+ end
24
+
25
+ it "uses configuration" do
26
+ Sample.use_config :first_conf
27
+ Sample.respond_to?(:first_conf).should == true
28
+ end
29
+
30
+ it "loads configuration" do
31
+ Sample.use_config :first_conf
32
+ Sample.first_conf.core.name.should == 'first_conf'
33
+ end
34
+ end
35
+
36
+ context "Samle class instance" do
37
+ before :each do
38
+ Sample.use_config :first_conf
39
+ @sample = Sample.new
40
+ end
41
+
42
+ it "has configuration accessor" do
43
+ @sample.respond_to? :first_conf
44
+ end
45
+
46
+ it "has the actual configuration" do
47
+ @sample.first_conf.core.name.should == 'first_conf'
48
+ end
49
+ end
50
+ end
51
+
metadata CHANGED
@@ -1,48 +1,54 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: use-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ prerelease:
5
+ version: 0.2.0
5
6
  platform: ruby
6
7
  authors:
7
- - Slava Chernobai
8
+ - Svetoslav Chernobay
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-01-18 00:00:00 +03:00
13
+ date: 2011-03-08 00:00:00 +03:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: hash-access
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
19
21
  requirements:
20
22
  - - ">="
21
23
  - !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
24
+ version: "0.2"
25
+ - - <=
26
+ - !ruby/object:Gem::Version
27
+ version: "0.3"
28
+ type: :runtime
29
+ version_requirements: *id001
30
+ description: UseConfig library allows a Ruby class to use configuration stored in a YAML file
31
+ email: slava@chernobay.info
26
32
  executables: []
27
33
 
28
34
  extensions: []
29
35
 
30
36
  extra_rdoc_files:
31
- - README
37
+ - README.md
32
38
  - LICENSE
33
39
  files:
34
- - README
40
+ - README.md
35
41
  - LICENSE
36
42
  - Rakefile
37
- - config/asd.yml
38
- - config/qwe.yml
39
43
  - lib/use_config.rb
40
- - lib/use_config
41
- - test/test_020_use_config.rb
42
- - test/test_010_configuration.rb
43
44
  - lib/use_config/configuration.rb
45
+ - lib/use_config/version.rb
46
+ - spec/use_config_spec.rb
47
+ - spec/use_config_configuration_spec.rb
44
48
  has_rdoc: true
45
- homepage: http://code.slava.pp.ru/use-config/
49
+ homepage: http://github.com/slavach/use-config
50
+ licenses: []
51
+
46
52
  post_install_message:
47
53
  rdoc_options:
48
54
  - --main=README
@@ -51,23 +57,23 @@ rdoc_options:
51
57
  require_paths:
52
58
  - lib
53
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
54
61
  requirements:
55
62
  - - ">="
56
63
  - !ruby/object:Gem::Version
57
64
  version: "0"
58
- version:
59
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
60
67
  requirements:
61
68
  - - ">="
62
69
  - !ruby/object:Gem::Version
63
- version: "0"
64
- version:
70
+ version: 1.3.6
65
71
  requirements: []
66
72
 
67
- rubyforge_project:
68
- rubygems_version: 1.0.1
73
+ rubyforge_project: use-config
74
+ rubygems_version: 1.5.2
69
75
  signing_key:
70
- specification_version: 2
71
- summary: UseConfig. Allows Ruby classes to use configuration, stored in YAML files.
76
+ specification_version: 3
77
+ summary: Easy configuration solution for any Ruby class
72
78
  test_files: []
73
79
 
data/README DELETED
@@ -1,34 +0,0 @@
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
-
@@ -1,7 +0,0 @@
1
- common:
2
- name: use_config_example
3
- title: UseConfig Example
4
-
5
- uses_use_config:
6
- title: Class That Uses UseConfig
7
-
@@ -1,10 +0,0 @@
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
-
@@ -1,90 +0,0 @@
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
-
@@ -1,74 +0,0 @@
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
-