lib/expand.rb in expand-1.0.2 vs lib/expand.rb in expand-1.0.3

- old
+ new

@@ -6,11 +6,11 @@ # The primary way to access Manager objects is to use the Expand namespace method. # @see Expand#namespace # class Manager # @see Expand#namespace - def initialize(namespace) + def initialize(namespace, **class_or_module, &block) @managed = namespace end # Create a module under the namespace for this object # @example @@ -69,15 +69,31 @@ # @return [Expand::Manager] instance which can allow you to create classes and modules in the given context. # # @see Expand::Manager#create_class # @see Expand::Manager#create_module # - def namespace(context, &block) + def namespace(context, **class_or_module, &block) unless context.is_a?(Module) context = context.to_s.split('::').inject(Object) do |base, mod| base.const_get(mod) end end - Manager.new(context).instance_eval(&block) + manager = Manager.new(context) + + creating_class, creating_module = class_or_module[:class], class_or_module[:module] + raise ArgumentError, "You must choose either class: or module: but not both." if creating_class && creating_module + + case + when creating_class + parent = class_or_module[:parent] || Object + manager.create_class(creating_class, parent: parent, &block) + when creating_module + if class_or_module[:parent] + warn "An option for :parent was provided as `#{class_or_module[:parent]}' but was ignored when creating the module: #{class_or_module[:module]}" + end + manager.create_module(creating_module, &block) + else + manager.instance_eval(&block) + end end alias expand namespace end