ruby-configurable 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,12 @@
1
+ === 1.0.2 / 2011-05-10
2
+
3
+ * 1 minor enhancement
4
+
5
+ * Default config is now accessible thru the generated Config module,
6
+ as Config.defaults. The default_config method added to your class
7
+ when you include Configurable is deprecated, but will continue to
8
+ work (on both your class and instances of it).
9
+
1
10
  === 1.0.1 / 2011-05-06
2
11
 
3
12
  * 2 bugfixes
data/README.txt CHANGED
@@ -53,13 +53,13 @@ Lets you make your Ruby class configurable with a simple mixin.
53
53
  Doodad::Config # => a ConfigStruct::Struct
54
54
  Doodad::Config::Bar # => another ConfigStruct::Struct
55
55
 
56
- Doodad.default_config # => #<struct Doodad::Config
56
+ Doodad::Config.defaults # => #<struct Doodad::Config
57
57
  # bar=#<struct Doodad::Config::Bar
58
58
  # quux=42, wibble=nil>,
59
59
  # baz=nil,
60
60
  # foo="default">
61
61
 
62
- Doodad.config # => a (deep) copy of default_config,
62
+ Doodad.config # => a (deep) copy of Doodad::Config.defaults,
63
63
  # ready for use
64
64
 
65
65
  Doodad.config.replace( # replaces config with passed values
@@ -14,7 +14,7 @@ require 'configurable/core_ext/inflections'
14
14
  #
15
15
  # This creates a ConfigStruct::Struct called Doodad::Config, along with
16
16
  # a frozen instance of it containing the default options accessible with
17
- # the Doodad.default_config method (see ConfigAccessors.default_config).
17
+ # the Doodad::Config.defaults method (see ConfigStructDefaults).
18
18
  #
19
19
  # You can then use Doodad.config (see ConfigAccessors.config) to store
20
20
  # the configuration for your class. See ConfigStruct::Struct for the
@@ -30,15 +30,14 @@ require 'configurable/core_ext/inflections'
30
30
  # Doodad.config.one # => 1
31
31
  #
32
32
  module Configurable
33
- VERSION = '1.0.1' #:nodoc:
33
+ VERSION = '1.0.2' #:nodoc:
34
34
 
35
35
  def self.included(klass) #:nodoc:
36
36
  klass.extend Macros
37
37
  klass.extend ConfigAccessors
38
38
  end
39
39
 
40
- # Returns the class' default configuration object.
41
- def default_config
40
+ def default_config #:nodoc:
42
41
  self.class.default_config
43
42
  end
44
43
 
@@ -64,10 +63,10 @@ module Configurable
64
63
  # configuration struct will be created as Doodad::Config.)
65
64
  #
66
65
  # You may also declare options with default settings, which will
67
- # then be available thru the default_config method on your class
68
- # (e.g. Doodad.default_config; see documentation on
69
- # ConfigAccessors.default_config). To declare defaults, pass them as
70
- # hash parameters:
66
+ # then be available thru the Config::defaults method on the
67
+ # generated struct (e.g. Doodad::Config.defaults; see documentation
68
+ # on ConfigStructDefaults). To declare defaults, pass them as hash
69
+ # parameters:
71
70
  #
72
71
  # configurable_options :one => 1, :two => 2
73
72
  #
@@ -86,6 +85,8 @@ module Configurable
86
85
  def configurable_options(*args)
87
86
  @_config_struct, @_default_config =
88
87
  create_struct(self, 'Config', *args)
88
+ @_config_struct.extend ConfigStructDefaults
89
+ @_config_struct.defaults = @_default_config
89
90
  end
90
91
 
91
92
 
@@ -110,10 +111,25 @@ module Configurable
110
111
  end
111
112
 
112
113
  def config_struct #:nodoc:
114
+ # the ||= nil quashes an 'instance variable not initialized' warning
113
115
  @_config_struct ||= nil
114
116
  end
115
117
  end
116
118
 
119
+ # Accessors for the class' default configuration to add to the
120
+ # generated Config module.
121
+ module ConfigStructDefaults
122
+ # The class' default configuration object. This object is a frozen
123
+ # instance of the Config struct (which is a ConfigStruct::Struct
124
+ # created by the Macros.configurable_options method).
125
+ attr_reader :defaults
126
+
127
+ def defaults=(new_defaults)
128
+ raise TypeError, 'defaults can only be assigned once' if defaults
129
+ @defaults = new_defaults
130
+ end
131
+ end
132
+
117
133
  # Methods to access the class' current configuration and default
118
134
  # configuration.
119
135
  module ConfigAccessors
@@ -125,17 +141,13 @@ module Configurable
125
141
  def config
126
142
  raise NameError, 'use configurable_options to define settings' unless
127
143
  config_struct
128
- @_config ||= @_default_config.dup # dup unfreezes
144
+ @_config ||= config_struct.defaults.dup # dup unfreezes
129
145
  end
130
146
 
131
- # Returns the class' default configuration object. This object is a
132
- # frozen instance of the class' Config struct (which is a
133
- # ConfigStruct::Struct created by the Macros.configurable_options
134
- # method).
135
- def default_config
147
+ def default_config #:nodoc:
136
148
  raise NameError, 'use configurable_options to define settings' unless
137
149
  config_struct
138
- @_default_config
150
+ config_struct.defaults
139
151
  end
140
152
  end
141
153
  end
@@ -119,6 +119,35 @@ describe Configurable do
119
119
  end
120
120
  end
121
121
 
122
+ describe 'generated Config class' do
123
+ before do
124
+ @struct = @test_class.const_get('Config')
125
+ refute_equal ::Config, @struct
126
+ end
127
+
128
+ it 'should return the default config' do
129
+ assert_respond_to @struct, :defaults
130
+ @defaults = @struct.defaults
131
+ assert_equal 1, @defaults.one
132
+ assert_nil @defaults.two
133
+ end
134
+
135
+ it 'should return the same instance as the class default_config' do
136
+ assert_equal @test_class.default_config, @struct.defaults
137
+ assert_equal @test_class.default_config.object_id,
138
+ @struct.defaults.object_id
139
+ end
140
+
141
+ it 'should raise an error if you try to reassign to defaults' do
142
+ begin
143
+ @struct.defaults = @struct.new
144
+ flunk 'expected TypeError'
145
+ rescue TypeError
146
+ # passed test if #defaults= raises TypeError
147
+ end
148
+ end
149
+ end
150
+
122
151
  describe 'an instance' do
123
152
  before do
124
153
  @test_inst = @test_class.new
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-configurable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin R. Bullock
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-06 00:00:00 -05:00
19
- default_executable:
18
+ date: 2011-05-11 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rubyforge
@@ -75,7 +74,6 @@ files:
75
74
  - lib/configurable/core_ext/inflections.rb
76
75
  - test/test_config_struct.rb
77
76
  - test/test_configurable.rb
78
- has_rdoc: true
79
77
  homepage: http://bitbucket.org/krbullock/configurable
80
78
  licenses: []
81
79
 
@@ -106,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
104
  requirements: []
107
105
 
108
106
  rubyforge_project: handtools
109
- rubygems_version: 1.6.1
107
+ rubygems_version: 1.7.2
110
108
  signing_key:
111
109
  specification_version: 3
112
110
  summary: Lets you make your Ruby class configurable with a simple mixin.