configurable 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.4.1 / 2009-03-23
2
+
3
+ * Simplified internal API for nesting (removed initialize_<key>)
4
+ * Added :hidden option type to prevent declaration of an option
5
+ * Refactored :map_default attribute to :set_default
6
+ * Made OrderedHashPatch preserve order when serialized as YAML
7
+
1
8
  == 0.4.0 / 2009-03-05
2
9
 
3
10
  Reworked nesting. Changes are not backward compatible.
data/README CHANGED
@@ -131,5 +131,5 @@ Configurable is available as a gem on RubyForge[http://rubyforge.org/projects/ta
131
131
  Copyright (c) 2008-2009, Regents of the University of Colorado.
132
132
  Developer:: {Simon Chiang}[http://bahuvrihi.wordpress.com], {Biomolecular Structure Program}[http://biomol.uchsc.edu/], {Hansen Lab}[http://hsc-proteomics.uchsc.edu/hansenlab/]
133
133
  Support:: CU Denver School of Medicine Deans Academic Enrichment Fund
134
- Licence:: {MIT-Style}[link:files/MIT-LICENSE.html]
134
+ License:: {MIT-Style}[link:files/MIT-LICENSE.html]
135
135
 
data/lib/config_parser.rb CHANGED
@@ -331,7 +331,8 @@ class ConfigParser
331
331
  # psr.parse("--opt value")
332
332
  # psr.config # => {:opt => 'eulav'}
333
333
  #
334
- # Define raises an error if key is already set by a different option.
334
+ # The :hidden type causes no configuration to be defined. Raises an error if
335
+ # key is already set by a different option.
335
336
  def define(key, default_value=nil, attributes={})
336
337
  # check for conflicts and register
337
338
  if default_config.has_key?(key)
@@ -346,6 +347,7 @@ class ConfigParser
346
347
  when :switch then setup_switch(key, default_value, attributes)
347
348
  when :flag then setup_flag(key, default_value, attributes)
348
349
  when :list then setup_list(key, attributes)
350
+ when :hidden then return nil
349
351
  when nil then setup_option(key, attributes)
350
352
  else
351
353
  if respond_to?("setup_#{attributes[:type]}")
data/lib/configurable.rb CHANGED
@@ -122,11 +122,19 @@ require 'configurable/class_methods'
122
122
  #
123
123
  # ==== Non-reader/writer attributes
124
124
  #
125
- # Metadata for a config may be specified in attributes as well. Attributes like
126
- # :desc, and :type are used by ConfigParser, for instance, to determine how to
127
- # represent the configuration on the command line. Attributes are unstructured
128
- # so they can accomodate metadata for multiple contexts (ex a web or desktop
129
- # interface), as needed.
125
+ # Attributes provide metadata for how to use configurations in various contexts.
126
+ # In general, attributes can be used to set any metadata an application
127
+ # needs. A few attributes are used internally by Configurable.
128
+ #
129
+ # Attribute:: Use::
130
+ # set_default:: When set to false, the delegate will not map a default value
131
+ # during bind. Specify when you manually initialize a config
132
+ # variable.
133
+ # type:: Specifies the type of option ConfigParser generates for this
134
+ # Delegate (ex: :switch, :flag, :list, :hidden)
135
+ # desc:: The description string used in the ConfigParser help
136
+ # long:: The long option (default: key)
137
+ # short:: The short option.
130
138
  #
131
139
  module Configurable
132
140
  autoload(:Utils, 'configurable/utils')
@@ -255,12 +255,10 @@ module Configurable
255
255
  # (default: key.to_s.capitalize)
256
256
  # instance_reader:: The method accessing the nested instance. (default: key)
257
257
  # instance_writer:: The method to set the nested instance. (default: "#{key}=")
258
- # instance_initializer:: The method that initializes the instance.
259
- # (default: "initialize_#{key}")
260
- # reader:: The method used to read the instance configuration.
258
+ # reader:: The method used to read the instance config.
261
259
  # (default: "#{key}_config_reader")
262
- # writer:: The method used to initialize or reconfigure the
263
- # instance. (default: "#{key}_config_writer")
260
+ # writer:: The method used to reconfigure the instance.
261
+ # (default: "#{key}_config_writer")
264
262
  #
265
263
  # Except for const_name, these attributes are used to define methods
266
264
  # required for nesting to work properly. None of the method attributes may
@@ -270,14 +268,13 @@ module Configurable
270
268
  # functionality:
271
269
  #
272
270
  # Attribute:: Function
273
- # instance_reader:: Returns the instance of the configurable class
271
+ # instance_reader:: Returns the instance of the configurable class
272
+ # (initializing if necessary, by default nest initializes
273
+ # using configurable_class.new)
274
274
  # instance_writer:: Inputs and sets the instance of the configurable class
275
- # instance_initializer:: Receives the initial config and return an instance of
276
- # configurable class
277
275
  # reader:: Returns instance.config
278
- # writer:: Reconfigures instance using the input overrides,
279
- # or uses instance_initializer and instance_writer to
280
- # initialize and set the instance.
276
+ # writer:: Reconfigures instance using the input overrides, or
277
+ # sets instance if provided.
281
278
  #
282
279
  # Methods can be public or otherwise. Specifying true uses and defines the
283
280
  # default methods. Specifying false uses the default method name, but does
@@ -289,7 +286,6 @@ module Configurable
289
286
  attributes = {
290
287
  :instance_reader => true,
291
288
  :instance_writer => true,
292
- :initializer => true
293
289
  }.merge(attributes)
294
290
 
295
291
  # define the nested configurable
@@ -310,7 +306,17 @@ module Configurable
310
306
 
311
307
  # define instance reader
312
308
  instance_reader = define_attribute_method(:instance_reader, attributes, key) do |attribute|
313
- attr_reader(key)
309
+ instance_variable = "@#{key}".to_sym
310
+
311
+ # gets or initializes the instance
312
+ define_method(attribute) do
313
+ if instance_variable_defined?(instance_variable)
314
+ instance_variable_get(instance_variable)
315
+ else
316
+ instance_variable_set(instance_variable, configurable_class.new)
317
+ end
318
+ end
319
+
314
320
  public(key)
315
321
  end
316
322
 
@@ -320,25 +326,21 @@ module Configurable
320
326
  public(attribute)
321
327
  end
322
328
 
323
- # define initializer
324
- initializer = define_attribute_method(:initializer, attributes, "initialize_#{key}") do |attribute|
325
- define_method(attribute) {|config| configurable_class.new.reconfigure(config) }
326
- private(attribute)
327
- end
328
-
329
329
  # define the reader
330
330
  reader = define_attribute_method(:reader, attributes, "#{key}_config_reader") do |attribute|
331
- define_method(attribute) { send(instance_reader).config }
331
+ define_method(attribute) do
332
+ send(instance_reader).config
333
+ end
332
334
  private(attribute)
333
335
  end
334
336
 
335
337
  # define the writer
336
338
  writer = define_attribute_method(:writer, attributes, "#{key}_config_writer") do |attribute|
337
339
  define_method(attribute) do |value|
338
- if instance = send(instance_reader)
339
- instance.reconfigure(value)
340
+ if value.kind_of?(configurable_class)
341
+ send(instance_writer, value)
340
342
  else
341
- send(instance_writer, send(initializer, value))
343
+ send(instance_reader).reconfigure(value)
342
344
  end
343
345
  end
344
346
  private(attribute)
@@ -458,6 +460,33 @@ module Configurable
458
460
  super
459
461
  @insertion_order = orig.instance_variable_get(:@insertion_order).dup
460
462
  end
463
+
464
+ # Overridden to load an array of [key, value] pairs in order (see to_yaml).
465
+ # The default behavior for loading from a hash of key-value pairs is
466
+ # preserved, but the insertion order will not be preserved.
467
+ def yaml_initialize( tag, val )
468
+ @insertion_order ||= []
469
+
470
+ if Array === val
471
+ val.each do |k, v|
472
+ self[k] = v
473
+ end
474
+ else
475
+ super
476
+ end
477
+ end
478
+
479
+ # Overridden to preserve insertion order by serializing self as an array
480
+ # of [key, value] pairs.
481
+ def to_yaml( opts = {} )
482
+ YAML::quick_emit( object_id, opts ) do |out|
483
+ out.seq( taguri, to_yaml_style ) do |seq|
484
+ each_pair do |key, value|
485
+ seq.add( [key, value] )
486
+ end
487
+ end
488
+ end
489
+ end
461
490
  end
462
491
 
463
492
  module ClassMethods
@@ -185,7 +185,7 @@ module Configurable
185
185
  # UNLESS map_default is set (indicating manual initialization)
186
186
  value = case
187
187
  when source.has_key?(key) then source.delete(key)
188
- when delegate[:map_default, true] then delegate.default
188
+ when delegate[:set_default, true] then delegate.default
189
189
  else next
190
190
  end
191
191
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configurable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Chiang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-05 00:00:00 -07:00
12
+ date: 2009-03-23 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency