Sha256: dcd86861f82ce5dfa0fff19f856321ec4cee9f409b08b69dd8ca469835137324
Contents?: true
Size: 1.82 KB
Versions: 2
Compression:
Stored size: 1.82 KB
Contents
# SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base). # # Author:: Andy Maleh # Copyright:: Copyright (c) 2014-2015 Andy Maleh # License:: MIT License # This module allows defining class methods and method invocations the same way a super class does without using def included(base). # Avoiding require_relative for backwards compatibility with Ruby 1.8.7 module SuperModule class << self V1_LIBRARY = File.expand_path(File.join(File.dirname(__FILE__), 'super_module', 'v1')) attr_accessor :super_module_body def define(&super_module_body) clone.tap { |super_module| super_module.super_module_body = super_module_body } end def included(original_base) if super_module_body original_base.class_eval(&super_module_body) else require V1_LIBRARY original_base.send(:include, SuperModule::V1) end end def __super_module_parent(name, ancestor) name_tokens = name.to_s.split('::') name_token_count = name_tokens.size if name_token_count == 1 ancestor else top_ancestor = ancestor.const_get(name_tokens.first) sub_module_name = name_tokens[1, name_token_count].join('::') __super_module_parent(sub_module_name, top_ancestor) end end end end def super_module(name=nil, &super_module_body) initial_ancestor = self.class == Object ? Object : self SuperModule.define(&super_module_body).tap do |new_super_module| if name parent = SuperModule.__super_module_parent(name, initial_ancestor) module_name = name.to_s.split('::').last parent.const_set(module_name, new_super_module) super_module_body = new_super_module.super_module_body new_super_module.class_eval(&super_module_body) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
super_module-1.2.2 | lib/super_module.rb |
super_module-1.2.1 | lib/super_module.rb |