simpleconfig 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
3
+ require 'rdoc/task'
4
4
  require "rubygems"
5
- require "rake/gempackagetask"
5
+ require "rubygems/package_task"
6
6
 
7
7
  desc 'Default: run unit tests.'
8
8
  task :default => :test
@@ -30,20 +30,18 @@ end
30
30
  #
31
31
  spec = Gem::Specification.new do |s|
32
32
  s.name = "simpleconfig"
33
- s.version = "1.1.2"
33
+ s.version = "1.1.3"
34
34
  s.date = "2010-09-13"
35
35
  s.summary = "Simple object-oriented application settings for Ruby applications"
36
36
  s.email = "luke@lukeredpath.co.uk"
37
37
  s.homepage = "http://github.com/lukeredpath/simpleconfig"
38
38
  s.description = "SimpleConfig is a plugin designed to make application-wide configuration settings (e.g. in a Rails app) easy to set and access in an object-oriented fashion."
39
- s.autorequire = "simpleconfig"
40
39
  s.has_rdoc = false
41
40
  s.authors = ["Luke Redpath"]
42
41
  s.files = [
43
42
  "lib/simple_config.rb",
44
43
  "lib/simple_config/controller_mixin.rb",
45
44
  "lib/simple_config/utilities.rb",
46
- "lib/rails_compatibility.rb",
47
45
  "lib/tasks/simple_config.rake",
48
46
  "init.rb",
49
47
  "Rakefile",
@@ -66,7 +64,7 @@ end
66
64
  #
67
65
  # To publish your gem online, install the 'gemcutter' gem; Read more
68
66
  # about that here: http://gemcutter.org/pages/gem_docs
69
- Rake::GemPackageTask.new(spec) do |pkg|
67
+ Gem::PackageTask.new(spec) do |pkg|
70
68
  pkg.gem_spec = spec
71
69
  end
72
70
 
data/init.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
2
2
 
3
- require 'rails_compatibility'
4
3
  require 'simple_config'
5
4
  require 'simple_config/controller_mixin'
@@ -1,14 +1,5 @@
1
1
  require 'yaml'
2
2
 
3
- unless Object.public_method_defined?(:tap)
4
- class Object
5
- def tap(&block)
6
- yield self
7
- self
8
- end
9
- end
10
- end
11
-
12
3
  module SimpleConfig
13
4
 
14
5
  class << self
@@ -27,9 +18,9 @@ module SimpleConfig
27
18
  end
28
19
 
29
20
  def for(config_name, &block)
30
- (@configs[config_name] ||= Config.new).tap do |config|
31
- config.configure(&block) if block_given?
32
- end
21
+ config = (@configs[config_name] ||= Config.new)
22
+ config.configure(&block) if block_given?
23
+ config
33
24
  end
34
25
  end
35
26
 
@@ -44,12 +35,15 @@ module SimpleConfig
44
35
  end
45
36
 
46
37
  def group(name, &block)
47
- (@groups[name] ||= Config.new).tap do |group|
48
- group.configure(&block) if block_given?
49
- end
38
+ group = (@groups[name] ||= Config.new)
39
+ group.configure(&block) if block_given?
40
+ define_accessor(name) { group }
41
+ group
50
42
  end
51
43
 
52
44
  def set(key, value)
45
+ unset(key) if set?(key)
46
+ define_accessor(key) { value }
53
47
  @settings[key] = value
54
48
  end
55
49
 
@@ -71,6 +65,7 @@ module SimpleConfig
71
65
  # exists? :bar # => false
72
66
  #
73
67
  def unset(key)
68
+ singleton_class.send(:remove_method, key)
74
69
  @settings.delete(key)
75
70
  end
76
71
 
@@ -123,15 +118,18 @@ module SimpleConfig
123
118
  end
124
119
  end
125
120
 
121
+ def set?(key)
122
+ @settings.key?(key)
123
+ end
124
+
126
125
  private
127
- def method_missing(method_name, *args)
128
- case true
129
- when @groups.key?(method_name)
130
- return @groups[method_name]
131
- when @settings.key?(method_name)
132
- return get(method_name)
133
- else
134
- super(method_name, *args)
126
+ def define_accessor(name, &block)
127
+ singleton_class.class_eval { define_method(name, &block) } unless respond_to?(name)
128
+ end
129
+
130
+ def singleton_class
131
+ class << self
132
+ self
135
133
  end
136
134
  end
137
135
  end
@@ -3,41 +3,41 @@ require File.join(File.dirname(__FILE__), *%w[test_helper])
3
3
  require 'simple_config'
4
4
 
5
5
  class SimpleConfigConfigTest < Test::Unit::TestCase
6
-
6
+
7
7
  def setup
8
8
  @config = SimpleConfig::Config.new
9
9
  end
10
-
10
+
11
11
  def test_should_be_able_to_set_config_values
12
12
  @config.set(:var_one, 'hello world')
13
13
  assert_equal 'hello world', @config.get(:var_one)
14
14
  end
15
-
15
+
16
16
  def test_should_be_able_to_access_settings_using_method_access
17
17
  @config.set(:foo, 'bar')
18
18
  assert_equal 'bar', @config.foo
19
19
  end
20
-
20
+
21
21
  def test_should_raise_NoMethodError_if_setting_does_not_exist_when_using_method_access
22
22
  assert_raises(NoMethodError) { @config.some_non_existent_variable }
23
23
  end
24
-
24
+
25
25
  def test_should_return_nil_if_setting_does_not_exist_when_using_get
26
26
  assert_nil @config.get(:some_non_existent_variable)
27
27
  end
28
-
28
+
29
29
  def test_unset_should_delete_config_values
30
30
  @config.set(:foo, 'bar')
31
31
  assert_equal('bar', @config.foo)
32
32
  @config.unset(:foo)
33
33
  assert_raises(NoMethodError) { @config.foo }
34
34
  end
35
-
35
+
36
36
  def test_unset_should_return_deleted_value
37
37
  @config.set(:foo, 'bar')
38
38
  assert_equal('bar', @config.unset(:foo))
39
39
  end
40
-
40
+
41
41
  def test_exists_should_return_whether_variable_isset
42
42
  assert(!@config.exists?(:foo))
43
43
  @config.set(:foo, 'bar')
@@ -45,7 +45,7 @@ class SimpleConfigConfigTest < Test::Unit::TestCase
45
45
  @config.unset(:foo)
46
46
  assert(!@config.exists?(:foo))
47
47
  end
48
-
48
+
49
49
  def test_exists_should_consider_empty_values_as_set
50
50
  [nil, 0, ''].each do |empty_value|
51
51
  @config.set(:foo, empty_value)
@@ -53,52 +53,64 @@ class SimpleConfigConfigTest < Test::Unit::TestCase
53
53
  assert(@config.exists?(:foo))
54
54
  end
55
55
  end
56
-
56
+
57
57
  def test_should_return_a_new_group_as_a_separate_config
58
58
  group = @config.group(:test)
59
59
  assert_instance_of(SimpleConfig::Config, group)
60
60
  assert_not_equal @config, group
61
61
  end
62
-
62
+
63
63
  def test_should_return_an_existing_group
64
64
  group = @config.group(:test)
65
65
  assert_equal group, @config.group(:test)
66
66
  end
67
-
67
+
68
68
  def test_should_configure_group_with_supplied_block_when_given
69
69
  group = @config.group(:test) do
70
70
  set :group_var, 'value'
71
71
  end
72
72
  assert_equal 'value', group.group_var
73
73
  end
74
-
74
+
75
+ def test_should_respond_to_methods_for_settings_and_groups
76
+ @config.group(:grp) { set :foo, 'bar' }
77
+ assert(@config.respond_to?(:grp))
78
+ assert(@config.grp.respond_to?(:foo))
79
+ end
80
+
81
+ def test_should_not_overwrite_existing_methods
82
+ object_id = @config.object_id
83
+ @config.set :object_id, 'object_id'
84
+ assert_equal object_id, @config.object_id
85
+ end
86
+
75
87
  def test_should_load_and_parse_external_config_as_ruby_in_context_of_config_instance
76
88
  File.stubs(:read).with('external_config.rb').returns(ruby_code = stub('ruby'))
77
89
  @config.expects(:instance_eval).with(ruby_code)
78
90
  @config.load('external_config.rb')
79
91
  end
80
-
92
+
81
93
  def test_should_laod_and_parse_external_config_as_yaml_in_context_of_config_instance
82
94
  parser = stub('YAMLParser')
83
95
  SimpleConfig::YAMLParser.stubs(:parse_contents_of_file).with('external_config.yml').returns(parser)
84
96
  parser.expects(:parse_into).with(@config)
85
97
  @config.load('external_config.yml')
86
98
  end
87
-
99
+
88
100
  def test_should_load_and_parse_external_config_as_yaml_if_config_file_has_full_yaml_extension
89
101
  parser = stub('YAMLParser')
90
102
  SimpleConfig::YAMLParser.expects(:parse_contents_of_file).with('external_config.yaml').returns(parser)
91
103
  parser.stubs(:parse_into)
92
104
  @config.load('external_config.yaml')
93
105
  end
94
-
106
+
95
107
  def test_should_load_and_parse_external_config_if_file_exists_when_if_exists_is_true
96
108
  File.stubs(:read).with('external_config.rb').returns(ruby_code = stub('ruby'))
97
109
  @config.expects(:instance_eval).with(ruby_code)
98
110
  File.stubs(:exist?).with('external_config.rb').returns(true)
99
111
  @config.load('external_config.rb', :if_exists? => true)
100
112
  end
101
-
113
+
102
114
  def test_should_not_load_and_parse_external_config_if_file_does_not_exist_when_if_exists_is_true
103
115
  @config.expects(:instance_eval).never
104
116
  File.stubs(:exist?).with('external_config.rb').returns(false)
@@ -136,7 +148,14 @@ class SimpleConfigConfigTest < Test::Unit::TestCase
136
148
  assert_equal hash, @config.test_group.to_hash
137
149
  end
138
150
 
151
+ def test_should_be_able_to_redefine_settings
152
+ @config = cascaded_test_config
153
+ @config.set(:test, "new-test-setting")
154
+ assert_equal "new-test-setting", @config.test
155
+ end
156
+
139
157
  private
158
+
140
159
  def cascaded_test_config
141
160
  SimpleConfig.for :simple_config_test do
142
161
  set :test, "test-setting"
@@ -148,5 +167,5 @@ class SimpleConfigConfigTest < Test::Unit::TestCase
148
167
  end
149
168
  end
150
169
  end
151
-
170
+
152
171
  end
metadata CHANGED
@@ -1,22 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simpleconfig
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
4
+ hash: 21
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 2
10
- version: 1.1.2
9
+ - 3
10
+ version: 1.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Luke Redpath
14
- autorequire: simpleconfig
14
+ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-13 00:00:00 +01:00
19
- default_executable:
18
+ date: 2010-09-13 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: SimpleConfig is a plugin designed to make application-wide configuration settings (e.g. in a Rails app) easy to set and access in an object-oriented fashion.
@@ -31,7 +30,6 @@ files:
31
30
  - lib/simple_config.rb
32
31
  - lib/simple_config/controller_mixin.rb
33
32
  - lib/simple_config/utilities.rb
34
- - lib/rails_compatibility.rb
35
33
  - lib/tasks/simple_config.rake
36
34
  - init.rb
37
35
  - Rakefile
@@ -42,7 +40,6 @@ files:
42
40
  - test/simple_config_functional_test.rb
43
41
  - test/simple_config_test.rb
44
42
  - test/yaml_parser_test.rb
45
- has_rdoc: true
46
43
  homepage: http://github.com/lukeredpath/simpleconfig
47
44
  licenses: []
48
45
 
@@ -72,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
69
  requirements: []
73
70
 
74
71
  rubyforge_project:
75
- rubygems_version: 1.3.7
72
+ rubygems_version: 1.8.5
76
73
  signing_key:
77
74
  specification_version: 3
78
75
  summary: Simple object-oriented application settings for Ruby applications
@@ -1,7 +0,0 @@
1
- unless Rails.respond_to?(:env)
2
- Rails.instance_eval do
3
- def env
4
- RAILS_ENV
5
- end
6
- end
7
- end