-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kitaro-tn/dev
added as namespace module
- Loading branch information
Showing
5 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
require "as_namespace/version" | ||
|
||
module AsNamespace | ||
# Your code goes here... | ||
|
||
def self.included(base) | ||
base.extend(ClassMethod) | ||
end | ||
|
||
module ClassMethod | ||
|
||
## | ||
# @param [Module] module_obj | ||
# @param [Object] alias_val | ||
def as_namespace(module_obj:, alias_val:) | ||
@module_obj = module_obj | ||
@alias_val = alias_val | ||
if (alias_val =~ /^[[:upper:]]/).nil? | ||
self.class_eval <<-EOS, __FILE__, __LINE__ + 1 | ||
def #{alias_val} | ||
#{module_obj} | ||
end | ||
EOS | ||
else | ||
self.const_set(alias_val, module_obj) | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
require 'spec_helper' | ||
require 'support/sample_namespace' | ||
|
||
describe AsNamespace do | ||
it 'has a version number' do | ||
expect(AsNamespace::VERSION).not_to be nil | ||
end | ||
|
||
it 'is abbreviated name' do | ||
test_class = TestClass | ||
expect(test_class.instance_methods(false)).to include(:grand) | ||
expect(test_class.const_defined?(:CHILD)).to be true | ||
expect(test_class.new.grandson).to eq("Grandson") | ||
expect(test_class.new.child).to eq("Child") | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# coding: utf-8 | ||
|
||
require 'as_namespace' | ||
|
||
module Parent | ||
|
||
module Child | ||
|
||
def self.myself | ||
"Child" | ||
end | ||
|
||
module Grandson | ||
|
||
def self.myself | ||
"Grandson" | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
class TestClass | ||
|
||
include AsNamespace | ||
|
||
as_namespace module_obj: Parent::Child::Grandson, alias_val: :grand | ||
as_namespace module_obj: Parent::Child, alias_val: :CHILD | ||
|
||
attr_reader :grandson, :child | ||
|
||
def initialize | ||
@grandson = grand.myself | ||
@child = CHILD.myself | ||
end | ||
|
||
end |