Created
November 30, 2017 15:01
-
-
Save siutin/5cf4366b2fff52f2580e661734a94e9f to your computer and use it in GitHub Desktop.
jruby bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| RSpec.describe "jruby bug", type: :request do | |
| # Creates a ruby module to namespace all the classes in if required | |
| def register_module(module_name) | |
| # dynamically create nested modules | |
| module_names = module_name.split("::").inject([]) { |n, c| n << (n.empty? ? [c] : [n.last] + [c]).flatten } | |
| puts "module_names: #{module_names}" | |
| module_names.map do |module_name_array| | |
| *prefix, parent, child = module_name_array | |
| if child | |
| full_path_child_module_name = module_name_array.join("::") | |
| unless Object.const_defined?(full_path_child_module_name) | |
| parent_module = find_or_register_module(parent, prefix) | |
| parent_module.const_set child, Module.new | |
| end | |
| else | |
| parent_module = find_or_register_module(parent, prefix) # register parent_module | |
| puts "parent_module: #{parent_module}" | |
| parent_module | |
| end | |
| end | |
| end | |
| def find_or_register_module(name, prefix) | |
| full_path_module_name = (prefix + [name]).join("::") | |
| if Object.const_defined? full_path_module_name | |
| Object.const_get full_path_module_name | |
| else | |
| if prefix.empty? | |
| Object.const_set name, Module.new | |
| else | |
| full_path_prefix = prefix.join("::") | |
| prefix_module = Object.const_get(full_path_prefix) | |
| prefix_module.const_set name, Module.new | |
| end | |
| end | |
| end | |
| context '1' do | |
| let(:module_name) { "Abc123::Foo" } | |
| before do | |
| register_module("Foo") | |
| register_module(module_name) | |
| end | |
| it { expect(Object.const_get(module_name).to_s).to match(module_name) } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment