module Expand

Extend your classes or modules to use the Expand methods

Constants

VERSION

Public Class Methods

expand(context, **class_or_module, &block)
Alias for: namespace
namespace(context, **class_or_module, &block) click to toggle source

Allows you to open a module namespace to add constants @example

require 'expand'
extend Expand
namespace SomeGem::Thing do
  create_class 'Other' do
    # add your class code here
  end
  create_module 'Another' do
    # add your module code here
  end
end

namespace SomeGem::Thing, class: :Other do
  # add methods here
end
namespace SomeGem::Thing, module: :Another do
  # add methods here
end

@param context [Module, String, or any object responding to to_s] representing a module namespace. @param module: module name to create @param class: class name to create @param parent: optional parent class for the created class, defaults to Object @yield the provided block is executed against an instance of Expand::Manager using instance_eval

@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

# File lib/expand.rb, line 97
def namespace(context, **class_or_module, &block)
  manager = Manager.for(context)

  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)
  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.apply(&block)
  end
end
Also aliased as: expand

Private Instance Methods

expand(context, **class_or_module, &block)
Alias for: namespace
namespace(context, **class_or_module, &block) click to toggle source

Allows you to open a module namespace to add constants @example

require 'expand'
extend Expand
namespace SomeGem::Thing do
  create_class 'Other' do
    # add your class code here
  end
  create_module 'Another' do
    # add your module code here
  end
end

namespace SomeGem::Thing, class: :Other do
  # add methods here
end
namespace SomeGem::Thing, module: :Another do
  # add methods here
end

@param context [Module, String, or any object responding to to_s] representing a module namespace. @param module: module name to create @param class: class name to create @param parent: optional parent class for the created class, defaults to Object @yield the provided block is executed against an instance of Expand::Manager using instance_eval

@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

# File lib/expand.rb, line 97
def namespace(context, **class_or_module, &block)
  manager = Manager.for(context)

  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)
  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.apply(&block)
  end
end
Also aliased as: expand