lib/configurable/class_methods.rb in configurable-0.4.2 vs lib/configurable/class_methods.rb in configurable-0.5.0
- old
+ new
@@ -13,19 +13,10 @@
include Lazydoc::Attributes
# A hash of (key, Delegate) pairs defining the class configurations.
attr_reader :configurations
- def self.extended(base) # :nodoc:
- unless base.instance_variable_defined?(:@source_file)
- caller[2] =~ Lazydoc::CALLER_REGEXP
- base.instance_variable_set(:@source_file, File.expand_path($1))
- end
-
- base.send(:initialize_configurations).extend(IndifferentAccess)
- end
-
def inherited(child) # :nodoc:
unless child.instance_variable_defined?(:@source_file)
caller[0] =~ Lazydoc::CALLER_REGEXP
child.instance_variable_set(:@source_file, File.expand_path($1))
end
@@ -38,27 +29,27 @@
end
super
end
# Parses configurations from argv in a non-destructive manner by generating
- # a ConfigParser using the configurations for self. Parsed configs are
- # added to config (note that you must keep a separate reference to
- # config as it is not returned by parse). The parser will is yielded to the
- # block, if given, to register additonal options. Returns an array of the
- # arguments that remain after parsing.
+ # a ConfigParser using the configurations for self. Returns an array like
+ # [args, config] where the args are the arguments that remain after parsing,
+ # and config is a hash of the parsed configs. The parser will is yielded to
+ # the block, if given, to register additonal options.
#
# See ConfigParser#parse for more information.
- def parse(argv=ARGV, config={})
- ConfigParser.new do |parser|
- parser.add(configurations)
- yield(parser) if block_given?
- end.parse(argv, config)
+ def parse(argv=ARGV, options={}) # :yields: parser
+ parse!(argv.dup, options)
end
# Same as parse, but removes parsed args from argv.
- def parse!(argv=ARGV, config={})
- argv.replace(parse(argv, config))
+ def parse!(argv=ARGV, options={})
+ parser = ConfigParser.new
+ parser.add(configurations)
+
+ args = parser.parse!(argv, options)
+ [args, parser.config]
end
protected
# Sets configurations to symbolize keys for AGET ([]) and ASET([]=)
@@ -300,10 +291,17 @@
const_name = if attributes.has_key?(:const_name)
attributes.delete(:const_name)
else
key.to_s.capitalize
end
- const_set(const_name, configurable_class) if const_name
+
+ if const_name
+ # this prevents a warning in cases where the nesting
+ # class defines the configurable_class
+ unless const_defined?(const_name) && const_get(const_name) == configurable_class
+ const_set(const_name, configurable_class)
+ end
+ end
# define instance reader
instance_reader = define_attribute_method(:instance_reader, attributes, key) do |attribute|
instance_variable = "@#{key}".to_sym
\ No newline at end of file