Skip to content

Commit

Permalink
fixed bugs in conversion to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerbot committed Nov 21, 2010
1 parent 9b9b6bc commit be8785b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 46 deletions.
2 changes: 1 addition & 1 deletion example/variations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def set_time(value)
exit
end
end.parse(ARGV) do |args, config|
errors = ConfigClass.configs.validate(config)
errors = []#ConfigClass.configs.validate(config)

if errors.empty?
pp args
Expand Down
12 changes: 6 additions & 6 deletions lib/configurable/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def check_infinite_nest(klass) # :nodoc:

klass.configs.each_value do |config|
if config.kind_of?(Nest)
check_infinite_nest(config.configurable_class)
check_infinite_nest(config.configurable.class)
end
end
end
Expand All @@ -320,10 +320,10 @@ def each_registry # :nodoc:
ancestors.each do |ancestor|
case
when ancestor.kind_of?(ClassMethods)
yield ancestor.config_type_registry
yield ancestor, ancestor.config_type_registry

when ancestor == Configurable
yield Configurable::DEFAULT_CONFIG_TYPES
yield ancestor, Configurable::DEFAULT_CONFIG_TYPES
break

else next
Expand All @@ -334,7 +334,7 @@ def each_registry # :nodoc:
def guess_config_type_by_name(name) # :nodoc:
return name if name.nil?

each_registry do |registry|
each_registry do |ancestor, registry|
if registry.has_key?(name)
return registry[name]
end
Expand All @@ -344,13 +344,13 @@ def guess_config_type_by_name(name) # :nodoc:
end

def guess_config_type_by_value(value) # :nodoc:
each_registry do |registry|
each_registry do |ancestor, registry|
guesses = registry.values.select {|config_type| config_type.matches?(value) }

case guesses.length
when 0 then next
when 1 then return guesses.at(0)
else raise "multiple guesses for config type: #{guesses.inspect} (default: #{default.inspect})"
else raise "multiple guesses for config type: #{guesses.inspect} (in: #{ancestor} default: #{default.inspect})"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/configurable/config_classes/nest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def configurable
end

def default
configurable.config.to_hash
uncast configurable.config.to_hash
end

# Calls the reader on the reciever to retreive an instance of the
Expand Down
3 changes: 1 addition & 2 deletions lib/configurable/config_types/nest_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def cast(input)
end

def uncast(value)
configs = value.config.to_hash
configurable.class.configs.export(configs)
configurable.class.configs.export(value)
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions lib/configurable/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ def to_parser(*args, &block)

nest_keys = nesting.collect {|nest| nest.key }
nest_names = nesting.collect {|nest| nest.name }.push(config.name)
hint = guess_hint(config)

guess_attrs = {
:long => nest_names.join(':'),
:hint => guess_hint(config)
}

attrs = {
config_attrs = {
:key => config.key,
:nest_keys => nest_keys,
:default => config.default,
:long => nest_names.join(':'),
:hint => hint,
:callback => lambda {|value| config.type.cast(value) }
}

parser.on(attrs.merge(config.desc))
attrs = guess_attrs.merge(config.desc).merge(config_attrs)
parser.on(attrs)
end

parser.sort_opts!
Expand Down Expand Up @@ -72,9 +75,9 @@ def export(source, target={})
# self to nesting.
def traverse(nesting=[], &block)
each_value do |config|
if config.respond_to?(:configs)
if config.respond_to?(:configurable)
nesting.push config
config.configs.traverse(nesting, &block)
config.configurable.class.configs.traverse(nesting, &block)
nesting.pop
else
yield(nesting, config)
Expand Down
29 changes: 0 additions & 29 deletions test/configurable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,35 +538,6 @@ def test_config_generates_a_nest_config_and_configurable_class_for_block
assert_equal({:inner => 1}, config.cast({'inner' => '1'}))
end

#
# config combinations test
#

class ScalarNestClass
include Configurable

class Outer
include Configurable
config :inner, 1
end

config :outer, Outer.new, :class => Configurable::ConfigClasses::Config
end

def test_nest_config_type_works_as_a_scalar_config
config = ScalarNestClass.configs[:outer]
assert_equal Config, config.class
assert_equal NestType, config.type.class
assert_equal ScalarNestClass::Outer, config.type.configurable.class
assert_equal({:inner => 1}, config.cast({'inner' => '1'}))

configurable = ScalarNestClass.new
assert_equal 1, configurable.config[:outer].config[:inner]

configs = ScalarNestClass.configs.export(configurable.config)
assert_equal({'outer' => {'inner' => '1'}}, configs)
end

#
# documentation test
#
Expand Down

0 comments on commit be8785b

Please sign in to comment.