configurability 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,15 @@
1
+ 4[tip] 4d7044641f87 2010-07-12 13:38 -0700 ged
2
+ Documentation, added a test for classname normalization.
3
+
4
+ 3 0b395dbf657f 2010-07-12 08:13 -0700 ged
5
+ Added some stuff to the README, made String config keys work.
6
+
7
+ 2 9531380cc3f2 2010-07-12 07:52 -0700 ged
8
+ Adding an example.
9
+
10
+ 1 982ba9fe2e6e 2010-07-11 11:26 -0700 ged
11
+ Initial version.
12
+
13
+ 0 ac34a9d6c913 2010-07-10 17:18 -0700 ged
14
+ Adding rake subrepo
15
+
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2010, Michael Granger
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the author/s, nor the names of the project's
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Configurability
2
+
3
+ Configurability is a mixin that allows you to add configurability to one or
4
+ more objects or classes. You can assign them each a subsection of the
5
+ configuration, and then later, when the configuration is loaded, the
6
+ configuration is split up and sent to the objects that will use it.
7
+
8
+ ## Usage
9
+
10
+ To add configurability to a class, just require the library and extend
11
+ the class:
12
+
13
+ class User
14
+ extend Configurability
15
+ end
16
+
17
+ Or, add it to a instance:
18
+
19
+ user = User.new
20
+ user.extend( Configurability )
21
+
22
+ Later, when you've loaded the configuration, can can call
23
+
24
+ Configurability.configure_objects( config )
25
+
26
+ and the `config` will be spliced up and sent to all the objects that have
27
+ been extended with it. `Configurability` expects the configuration to be
28
+ broken up into a number of sections, each of which is accessible via either
29
+ a method with the _section name_ or the index operator (`#[]`) that takes the
30
+ _section name_ as a `Symbol` or a `String`:
31
+
32
+ config.section_name
33
+ config[:section_name]
34
+ config['section_name']
35
+
36
+ The section name is based on an object's _config key_, which is the name of
37
+ the object that is being extended with all non-word characters converted into
38
+ underscores (`_`) by default.
39
+
40
+ If the object responds to the `#name` method, then the return value of that
41
+ method is used to derive the name. If it doesn't have a `#name` method, the
42
+ name of its `Class` will be used instead. If its class is anonymous, then
43
+ the object's config key will be `:anonymous`.
44
+
45
+ When the configuration is loaded, an instance variable called `@config` is set
46
+ to the appropriate section of the config object for each object that has
47
+ been extended with Configurability.
48
+
49
+
50
+ ## Customization
51
+
52
+ The default behavior above is just provided as a reasonable default; it is
53
+ expected that you'll want to customize at least one or two things about
54
+ how configuration is handled in your objects.
55
+
56
+ ### Setting a Custom Config Key
57
+
58
+ The first thing you might want to do is change the config section that
59
+ corresponds to your object. You can do that by declaring a different
60
+ config key, either using a declarative method:
61
+
62
+ class OutputFormatter
63
+ extend Configurability
64
+ config_key :format
65
+ end
66
+
67
+ or by overriding the `#config_key` method and returning the desired value
68
+ as a Symbol:
69
+
70
+ class User
71
+ extend Configurability
72
+ def self::config_key
73
+ return :employees
74
+ end
75
+ end
76
+
77
+ ### Changing How an Object Is Configured
78
+
79
+ You can also change what happens when an object is configured by implementing
80
+ a `#configure` method that takes the config section as an argument:
81
+
82
+ class WebServer
83
+ extend Configurability
84
+
85
+ def self::configure( configsection )
86
+ @default_bind_addr = configsection[:host]
87
+ @default_port = configsection[:port]
88
+ end
89
+ end
90
+
91
+ If you still want the `config` variable to be set, just `super` from your implementation; don't if you don't want it to be set.
92
+
93
+
94
+ ## Development
95
+
96
+ You can submit bug reports, suggestions, and read more about future plans at
97
+ the project page:
98
+
99
+ > http://bitbucket.org/ged/configurability
100
+
101
+ or clone it with Mercurial from the same address.
102
+
103
+
104
+ ## License
105
+
106
+ Copyright (c) 2010, Michael Granger
107
+ All rights reserved.
108
+
109
+ Redistribution and use in source and binary forms, with or without
110
+ modification, are permitted provided that the following conditions are met:
111
+
112
+ * Redistributions of source code must retain the above copyright notice,
113
+ this list of conditions and the following disclaimer.
114
+
115
+ * Redistributions in binary form must reproduce the above copyright notice,
116
+ this list of conditions and the following disclaimer in the documentation
117
+ and/or other materials provided with the distribution.
118
+
119
+ * Neither the name of the author/s, nor the names of the project's
120
+ contributors may be used to endorse or promote products derived from this
121
+ software without specific prior written permission.
122
+
123
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
124
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
125
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
126
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
127
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
128
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
129
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
130
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
131
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
132
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
133
+
data/Rakefile ADDED
@@ -0,0 +1,348 @@
1
+ #!rake -*- ruby -*-
2
+ #
3
+ # configurability rakefile
4
+ #
5
+ # Based on various other Rakefiles, especially one by Ben Bleything
6
+ #
7
+ # Copyright (c) 2007-2010 The FaerieMUD Consortium
8
+ #
9
+ # Authors:
10
+ # * Michael Granger <ged@FaerieMUD.org>
11
+ #
12
+
13
+ BEGIN {
14
+ require 'rbconfig'
15
+ require 'pathname'
16
+ basedir = Pathname.new( __FILE__ ).dirname
17
+
18
+ libdir = basedir + "lib"
19
+ extdir = libdir + Config::CONFIG['sitearch']
20
+
21
+ $LOAD_PATH.unshift( libdir.to_s ) unless $LOAD_PATH.include?( libdir.to_s )
22
+ $LOAD_PATH.unshift( extdir.to_s ) unless $LOAD_PATH.include?( extdir.to_s )
23
+ }
24
+
25
+ begin
26
+ require 'readline'
27
+ include Readline
28
+ rescue LoadError
29
+ # Fall back to a plain prompt
30
+ def readline( text )
31
+ $stderr.print( text.chomp )
32
+ return $stdin.gets
33
+ end
34
+ end
35
+
36
+ begin
37
+ require 'rubygems'
38
+ rescue LoadError
39
+ module Gem
40
+ class Specification; end
41
+ end
42
+ end
43
+
44
+ require 'rbconfig'
45
+ require 'rake'
46
+ require 'rake/testtask'
47
+ require 'rake/packagetask'
48
+ require 'rake/clean'
49
+ # require 'rake/191_compat.rb'
50
+
51
+ $dryrun = false
52
+
53
+ ### Config constants
54
+ BASEDIR = Pathname.new( __FILE__ ).dirname.relative_path_from( Pathname.getwd )
55
+ BINDIR = BASEDIR + 'bin'
56
+ LIBDIR = BASEDIR + 'lib'
57
+ EXTDIR = BASEDIR + 'ext'
58
+ DOCSDIR = BASEDIR + 'docs'
59
+ PKGDIR = BASEDIR + 'pkg'
60
+ DATADIR = BASEDIR + 'data'
61
+
62
+ MANUALDIR = DOCSDIR + 'manual'
63
+
64
+ PROJECT_NAME = 'configurability'
65
+ PKG_NAME = PROJECT_NAME.downcase
66
+ PKG_SUMMARY = 'A configurability mixin for Ruby'
67
+
68
+ # Cruisecontrol stuff
69
+ CC_BUILD_LABEL = ENV['CC_BUILD_LABEL']
70
+ CC_BUILD_ARTIFACTS = ENV['CC_BUILD_ARTIFACTS'] || 'artifacts'
71
+
72
+ VERSION_FILE = LIBDIR + 'configurability.rb'
73
+ if VERSION_FILE.exist? && buildrev = ENV['CC_BUILD_LABEL']
74
+ PKG_VERSION = VERSION_FILE.read[ /VERSION\s*=\s*['"](\d+\.\d+\.\d+)['"]/, 1 ] + '.' + buildrev
75
+ elsif VERSION_FILE.exist?
76
+ PKG_VERSION = VERSION_FILE.read[ /VERSION\s*=\s*['"](\d+\.\d+\.\d+)['"]/, 1 ]
77
+ end
78
+
79
+ PKG_VERSION = '0.0.0' unless defined?( PKG_VERSION )
80
+
81
+ PKG_FILE_NAME = "#{PKG_NAME.downcase}-#{PKG_VERSION}"
82
+ GEM_FILE_NAME = "#{PKG_FILE_NAME}.gem"
83
+
84
+ # Universal VCS constants
85
+ DEFAULT_EDITOR = 'vi'
86
+ COMMIT_MSG_FILE = 'commit-msg.txt'
87
+ FILE_INDENT = " " * 12
88
+ LOG_INDENT = " " * 3
89
+
90
+ EXTCONF = EXTDIR + 'extconf.rb'
91
+
92
+ ARTIFACTS_DIR = Pathname.new( CC_BUILD_ARTIFACTS )
93
+
94
+ TEXT_FILES = Rake::FileList.new( %w[Rakefile ChangeLog README* LICENSE] )
95
+ BIN_FILES = Rake::FileList.new( "#{BINDIR}/*" )
96
+ LIB_FILES = Rake::FileList.new( "#{LIBDIR}/**/*.rb" )
97
+ EXT_FILES = Rake::FileList.new( "#{EXTDIR}/**/*.{c,h,rb}" )
98
+ DATA_FILES = Rake::FileList.new( "#{DATADIR}/**/*" )
99
+
100
+ SPECDIR = BASEDIR + 'spec'
101
+ SPECLIBDIR = SPECDIR + 'lib'
102
+ SPEC_FILES = Rake::FileList.new( "#{SPECDIR}/**/*_spec.rb", "#{SPECLIBDIR}/**/*.rb" )
103
+
104
+ TESTDIR = BASEDIR + 'tests'
105
+ TEST_FILES = Rake::FileList.new( "#{TESTDIR}/**/*.tests.rb" )
106
+
107
+ RAKE_TASKDIR = BASEDIR + 'rake'
108
+ RAKE_TASKLIBS = Rake::FileList.new( "#{RAKE_TASKDIR}/*.rb" )
109
+ PKG_TASKLIBS = Rake::FileList.new( "#{RAKE_TASKDIR}/{191_compat,helpers,packaging,rdoc,testing}.rb" )
110
+ PKG_TASKLIBS.include( "#{RAKE_TASKDIR}/manual.rb" ) if MANUALDIR.exist?
111
+
112
+ RAKE_TASKLIBS_URL = 'http://repo.deveiate.org/rake-tasklibs'
113
+
114
+ LOCAL_RAKEFILE = BASEDIR + 'Rakefile.local'
115
+
116
+ EXTRA_PKGFILES = Rake::FileList.new
117
+
118
+ RELEASE_FILES = TEXT_FILES +
119
+ SPEC_FILES +
120
+ TEST_FILES +
121
+ BIN_FILES +
122
+ LIB_FILES +
123
+ EXT_FILES +
124
+ DATA_FILES +
125
+ RAKE_TASKLIBS +
126
+ EXTRA_PKGFILES
127
+
128
+
129
+ RELEASE_FILES << LOCAL_RAKEFILE.to_s if LOCAL_RAKEFILE.exist?
130
+
131
+ RELEASE_ANNOUNCE_ADDRESSES = [
132
+ "Ruby-Talk List <ruby-talk@ruby-lang.org>",
133
+ ]
134
+
135
+ COVERAGE_MINIMUM = ENV['COVERAGE_MINIMUM'] ? Float( ENV['COVERAGE_MINIMUM'] ) : 85.0
136
+ RCOV_EXCLUDES = 'spec,tests,/Library/Ruby,/var/lib,/usr/local/lib'
137
+ RCOV_OPTS = [
138
+ '--exclude', RCOV_EXCLUDES,
139
+ '--xrefs',
140
+ '--save',
141
+ '--callsites',
142
+ #'--aggregate', 'coverage.data' # <- doesn't work as of 0.8.1.2.0
143
+ ]
144
+
145
+
146
+ ### Load some task libraries that need to be loaded early
147
+ if !RAKE_TASKDIR.exist?
148
+ $stderr.puts "It seems you don't have the build task directory. Shall I fetch it "
149
+ ans = readline( "for you? [y]" )
150
+ ans = 'y' if !ans.nil? && ans.empty?
151
+
152
+ if ans =~ /^y/i
153
+ $stderr.puts "Okay, fetching #{RAKE_TASKLIBS_URL} into #{RAKE_TASKDIR}..."
154
+ system 'hg', 'clone', RAKE_TASKLIBS_URL, "./#{RAKE_TASKDIR}"
155
+ if ! $?.success?
156
+ fail "Damn. That didn't work. Giving up; maybe try manually fetching?"
157
+ end
158
+ else
159
+ $stderr.puts "Then I'm afraid I can't continue. Best of luck."
160
+ fail "Rake tasklibs not present."
161
+ end
162
+
163
+ RAKE_TASKLIBS.include( "#{RAKE_TASKDIR}/*.rb" )
164
+ end
165
+
166
+ require RAKE_TASKDIR + 'helpers.rb'
167
+ include RakefileHelpers
168
+
169
+ # Set the build ID if the mercurial executable is available
170
+ if hg = which( 'hg' )
171
+ id = IO.read('|-') or exec hg.to_s, 'id', '-n'
172
+ PKG_BUILD = "pre%03d" % [(id.chomp[ /^[[:xdigit:]]+/ ] || '1')]
173
+ else
174
+ PKG_BUILD = 'pre000'
175
+ end
176
+ SNAPSHOT_PKG_NAME = "#{PKG_FILE_NAME}.#{PKG_BUILD}"
177
+ SNAPSHOT_GEM_NAME = "#{SNAPSHOT_PKG_NAME}.gem"
178
+
179
+ # Documentation constants
180
+ API_DOCSDIR = DOCSDIR + 'api'
181
+ README_FILE = TEXT_FILES.find {|path| path =~ /^README/ } || 'README'
182
+ RDOC_OPTIONS = [
183
+ '--tab-width=4',
184
+ '--show-hash',
185
+ '--include', BASEDIR.to_s,
186
+ "--main=#{README_FILE}",
187
+ "--title=#{PKG_NAME}",
188
+ ]
189
+ YARD_OPTIONS = [
190
+ '--use-cache',
191
+ '--no-private',
192
+ '--protected',
193
+ '-r', README_FILE,
194
+ '--exclude', 'extconf\\.rb',
195
+ '--files', 'ChangeLog,LICENSE',
196
+ '--output-dir', API_DOCSDIR.to_s,
197
+ '--title', "#{PKG_NAME} #{PKG_VERSION}",
198
+ ]
199
+
200
+ # Release constants
201
+ SMTP_HOST = "mail.faeriemud.org"
202
+ SMTP_PORT = 465 # SMTP + SSL
203
+
204
+ # Project constants
205
+ PROJECT_HOST = 'deveiate.org'
206
+ PROJECT_PUBDIR = '/usr/local/www/public/code'
207
+ PROJECT_DOCDIR = "#{PROJECT_PUBDIR}/#{PKG_NAME}"
208
+ PROJECT_SCPPUBURL = "#{PROJECT_HOST}:#{PROJECT_PUBDIR}"
209
+ PROJECT_SCPDOCURL = "#{PROJECT_HOST}:#{PROJECT_DOCDIR}"
210
+
211
+ # Gem dependencies: gemname => version
212
+ DEPENDENCIES = {
213
+ }
214
+
215
+ # Developer Gem dependencies: gemname => version
216
+ DEVELOPMENT_DEPENDENCIES = {
217
+ 'rake' => '>= 0.8.7',
218
+ 'rcodetools' => '>= 0.7.0.0',
219
+ 'rcov' => '>= 0.8.1.2.0',
220
+ 'rdoc' => '>= 2.4.3',
221
+ 'RedCloth' => '>= 4.0.3',
222
+ 'rspec' => '>= 1.2.6',
223
+ 'ruby-termios' => '>= 0.9.6',
224
+ 'text-format' => '>= 1.0.0',
225
+ 'tmail' => '>= 1.2.3.1',
226
+ 'diff-lcs' => '>= 1.1.2',
227
+ }
228
+
229
+ # Non-gem requirements: packagename => version
230
+ REQUIREMENTS = {
231
+ }
232
+
233
+ # RubyGem specification
234
+ GEMSPEC = Gem::Specification.new do |gem|
235
+ gem.name = PKG_NAME.downcase
236
+ gem.version = PKG_VERSION
237
+
238
+ gem.summary = PKG_SUMMARY
239
+ gem.description = [
240
+ "Configurability is mixin that allows you to add configurability to one or more classes, assign them each a section of the configuration, and then when the configuration is loaded, the class is given the section it requested.",
241
+ ].join( "\n" )
242
+
243
+ gem.authors = "Michael Granger"
244
+ gem.email = ["ged@FaerieMUD.org"]
245
+ gem.homepage = 'http://bitbucket.org/ged/configurability'
246
+
247
+ gem.has_rdoc = true
248
+ gem.rdoc_options = RDOC_OPTIONS
249
+ gem.extra_rdoc_files = TEXT_FILES - [ 'Rakefile' ]
250
+
251
+ gem.bindir = BINDIR.relative_path_from(BASEDIR).to_s
252
+ gem.executables = BIN_FILES.select {|pn| File.executable?(pn) }.
253
+ collect {|pn| File.basename(pn) }
254
+ gem.require_paths << EXTDIR.relative_path_from( BASEDIR ).to_s if EXTDIR.exist?
255
+
256
+ if EXTCONF.exist?
257
+ gem.extensions << EXTCONF.relative_path_from( BASEDIR ).to_s
258
+ end
259
+
260
+ gem.files = RELEASE_FILES
261
+ gem.test_files = SPEC_FILES
262
+
263
+ DEPENDENCIES.each do |name, version|
264
+ version = '>= 0' if version.length.zero?
265
+ gem.add_runtime_dependency( name, version )
266
+ end
267
+
268
+ REQUIREMENTS.each do |name, version|
269
+ gem.requirements << [ name, version ].compact.join(' ')
270
+ end
271
+ end
272
+
273
+ $trace = Rake.application.options.trace ? true : false
274
+ $dryrun = Rake.application.options.dryrun ? true : false
275
+ $include_dev_dependencies = false
276
+
277
+ # Load any remaining task libraries
278
+ RAKE_TASKLIBS.each do |tasklib|
279
+ next if tasklib.to_s =~ %r{/helpers\.rb$}
280
+ begin
281
+ trace " loading tasklib %s" % [ tasklib ]
282
+ import tasklib
283
+ rescue ScriptError => err
284
+ fail "Task library '%s' failed to load: %s: %s" %
285
+ [ tasklib, err.class.name, err.message ]
286
+ trace "Backtrace: \n " + err.backtrace.join( "\n " )
287
+ rescue => err
288
+ log "Task library '%s' failed to load: %s: %s. Some tasks may not be available." %
289
+ [ tasklib, err.class.name, err.message ]
290
+ trace "Backtrace: \n " + err.backtrace.join( "\n " )
291
+ end
292
+ end
293
+
294
+ # Load any project-specific rules defined in 'Rakefile.local' if it exists
295
+ import LOCAL_RAKEFILE if LOCAL_RAKEFILE.exist?
296
+
297
+
298
+ #####################################################################
299
+ ### T A S K S
300
+ #####################################################################
301
+
302
+ ### Default task
303
+ task :default => [:clean, :local, :spec, :apidocs, :package]
304
+
305
+ ### Task the local Rakefile can append to -- no-op by default
306
+ task :local
307
+
308
+ ### Task: clean
309
+ CLEAN.include 'coverage', '**/*.orig', '**/*.rej'
310
+ CLOBBER.include 'artifacts', 'coverage.info', 'ChangeLog', PKGDIR
311
+
312
+ ### Task: changelog
313
+ file 'ChangeLog' do |task|
314
+ log "Updating #{task.name}"
315
+
316
+ changelog = make_changelog()
317
+ File.open( task.name, 'w' ) do |fh|
318
+ fh.print( changelog )
319
+ end
320
+ end
321
+
322
+
323
+ ### Task: cruise (Cruisecontrol task)
324
+ desc "Cruisecontrol build"
325
+ task :cruise => [:clean, 'spec:quiet', :package] do |task|
326
+ raise "Artifacts dir not set." if ARTIFACTS_DIR.to_s.empty?
327
+ artifact_dir = ARTIFACTS_DIR.cleanpath + (CC_BUILD_LABEL || Time.now.strftime('%Y%m%d-%T'))
328
+ artifact_dir.mkpath
329
+
330
+ coverage = BASEDIR + 'coverage'
331
+ if coverage.exist? && coverage.directory?
332
+ $stderr.puts "Copying coverage stats..."
333
+ FileUtils.cp_r( 'coverage', artifact_dir )
334
+ end
335
+
336
+ $stderr.puts "Copying packages..."
337
+ FileUtils.cp_r( FileList['pkg/*'].to_a, artifact_dir )
338
+ end
339
+
340
+
341
+ desc "Update the build system to the latest version"
342
+ task :update_build do
343
+ log "Updating the build system"
344
+ run 'hg', '-R', RAKE_TASKDIR, 'pull', '-u'
345
+ log "Updating the Rakefile"
346
+ sh 'rake', '-f', RAKE_TASKDIR + 'Metarakefile'
347
+ end
348
+