lib/expand.rb in expand-1.0.4 vs lib/expand.rb in expand-1.0.5

- old
+ new

@@ -1,10 +1,9 @@ require "expand/version" # Extend your classes or modules to use the Expand methods module Expand - # The primary way to access Manager objects is to use the Expand namespace method. # @see Expand#namespace # class Manager # @see Expand#namespace @@ -46,10 +45,24 @@ def create_class(name, parent: Object, &block) klass = Class.new(parent, &block) @managed.const_set(name, klass) klass end + + def apply(&block) + instance_eval(&block) + @managed + end + + def self.for(context) + unless context.is_a?(Module) + context = context.to_s.split("::").inject(Object) do |base, mod| + base.const_get(mod) + end + end + new(context) + end end # Allows you to open a module namespace to add constants # @example # require 'expand' @@ -80,31 +93,27 @@ # # @see Expand::Manager#create_class # @see Expand::Manager#create_module # 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 = Manager.new(context) + manager = Manager.for(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 + case class_or_module + in { module: Symbol => _creating_module, class: Symbol => _creating_class } + raise ArgumentError, "You must choose either class: or module: but not both." + in { class: Symbol => 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]}" + in { module: Symbol => creating_module, ** } + if parent = class_or_module[:parent] + warn "An option for :parent was provided as `#{parent}' but was ignored when creating the module: #{creating_module}" end + manager.create_module(creating_module, &block) else - manager.instance_eval(&block) + manager.apply(&block) end end - alias expand namespace + alias_method :expand, :namespace module_function :expand, :namespace end