Module: Expand

Defined in:
lib/expand.rb,
lib/expand/version.rb

Overview

Extend your classes or modules to use the Expand methods

Defined Under Namespace

Classes: Manager

Constant Summary collapse

VERSION =
"1.0.6"

Class Method Summary collapse

Class Method Details

.expand { ... } ⇒ Expand::Manager

Allows you to open a module namespace to add constants

Examples:

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

Parameters:

  • context (Module, String, or any object responding to to_s)

    representing a module namespace.

  • module:

    module name to create

  • class:

    class name to create

  • parent:

    optional parent class for the created class, defaults to Object

Yields:

  • the provided block is executed against an instance of Expand::Manager using instance_eval

Returns:

  • (Expand::Manager)

    instance which can allow you to create classes and modules in the given context.

See Also:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/expand.rb', line 117

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

.namespace(context, **class_or_module) { ... } ⇒ Expand::Manager

Allows you to open a module namespace to add constants

Examples:

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

Parameters:

  • context (Module, String, or any object responding to to_s)

    representing a module namespace.

  • module:

    module name to create

  • class:

    class name to create

  • parent:

    optional parent class for the created class, defaults to Object

Yields:

  • the provided block is executed against an instance of Expand::Manager using instance_eval

Returns:

  • (Expand::Manager)

    instance which can allow you to create classes and modules in the given context.

See Also:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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