Sha256: 7e28cd4587ce6f074ce57e6a84393c66a89eec74295efdeffac5fafe067407da

Contents?: true

Size: 612 Bytes

Versions: 1

Compression:

Stored size: 612 Bytes

Contents

def define_module(name, &block)
  name = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  parts = name.split("::")
  parts.each_with_index do |part, index|
    sub_name = parts[0..index].join("::")
    eval("module #{sub_name}; end")
  end
  clazz = eval(name)
  clazz.unloadable if clazz.respond_to?(:unloadable)
  clazz.class_eval(&block) if block_given?
  clazz
end

=begin
def add_module(name, &block)
    define_module(name).class_eval do
        class << self; self; end.send(:define_method, :included) { |base|
            base.class_eval(&block)
        }
    end
end
=end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
defined-by-0.0.6 lib/defined-by/define_module.rb