Skip to content

Commit

Permalink
Merge pull request #4 from kitaro-tn/dev
Browse files Browse the repository at this point in the history
added as namespace module
  • Loading branch information
tanish-kr committed Jul 17, 2016
2 parents 42c7ac2 + 2ed5157 commit c811c1d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/as_namespace`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Expand All @@ -26,7 +25,18 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
```ruby
require 'as_namespace'

class TestClass

include AsNamespace

as_namespace module_obj: Parent::Child::Grandson, alias_val: :grand
as_namespace module_obj: Parent::Child, alias_val: :CHILD

end
```

## Development

Expand Down
8 changes: 0 additions & 8 deletions as_namespace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/kitaro-tn/as_namespace"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
Expand Down
27 changes: 26 additions & 1 deletion lib/as_namespace.rb
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
9 changes: 9 additions & 0 deletions spec/as_namespace_spec.rb
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
39 changes: 39 additions & 0 deletions spec/support/sample_namespace.rb
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

0 comments on commit c811c1d

Please sign in to comment.