lib/indexes/dsl/api.rb in indexes-4.0.0.0 vs lib/indexes/dsl/api.rb in indexes-4.0.0.1
- old
+ new
@@ -1,21 +1,19 @@
module Indexes
module Dsl
class Api
def initialize(args=[], parent={}, &block)
- @args = args
@parent = parent
instance_exec *args, &block
end
def method_missing(name, *args, &block)
options = args.extract_options!
name = name.to_sym
if block_given?
- child = add_block(name, args, options)
- continue child, &block
+ add_block name, args, options, &block
elsif args.size > 0
add_argument name, args, options
elsif options.any?
add_options name, options
else
@@ -27,32 +25,43 @@
@parent
end
private
- def add_block(name, args, options)
+ def add_block(name, args, options, &block)
+ case args.first
+ when String,Symbol
+ child = {}
+ node = { args.first.to_sym => child }
+ when Enumerable
+ child = node = []
+ else
+ child = node = {}
+ end
case @parent
when Array
- item = options.merge(name => {})
- @parent << item
- child = item[name]
+ @parent << options.merge(name => node)
when Hash
- if @parent.has_key?(name)
- child = @parent[name].merge!(options)
- else
- child = @parent[name] = {}
- end
+ @parent[name] = node
end
- if args.any?
- child[args.first.to_sym] = {}
+ case args.first
+ when Enumerable
+ args.first.each do |arg|
+ self.class.new [arg], child, &block
+ end
else
- child
+ self.class.new [], child, &block
end
end
def add_argument(name, args, options)
- @parent[name] = args.first
+ case @parent
+ when Array
+ @parent << { name => args.first }
+ when Hash
+ @parent[name] = args.first
+ end
end
def add_options(name, options)
options.symbolize_keys!
case @parent
@@ -72,13 +81,9 @@
when Array
@parent << { name => {} }
when Hash
@parent[name] = {}
end
- end
-
- def continue(child, &block)
- self.class.new @args, child, &block
end
end
end
end