namespacing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7fe235b4e378650645bd9f237c7b98d9f49f587
4
+ data.tar.gz: ce83bba403ce30343d484033bdfe904323c8c650
5
+ SHA512:
6
+ metadata.gz: 45095800f498c99c8714fd9e4b5e659c5233b943049f5e7d8e9516b66e37423110aa0affd47905d9a2ee8f5408b2719a3565d08b84367e74c6eeac3b29e6c598
7
+ data.tar.gz: b855f011398687a38bbbe28505367a3186ca5b33e297dadb6c254ea859f6a56cc2f02749dc8b556ca9919b641288d6c89f17663023ae977a685cfabe17cec63b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in namespacing.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Justin Herrick
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Namespacing [![Build Status](https://travis-ci.org/jah2488/namespacing.png?branch=master)](https://travis-ci.org/jah2488/namespacing) [![Code Climate](https://codeclimate.com/github/jah2488/namespacing.png)](https://codeclimate.com/github/jah2488/namespacing) [![Dependency Status](https://gemnasium.com/jah2488/namespacing.png)](https://gemnasium.com/jah2488/namespacing)
2
+
3
+
4
+ Namespacing adds namespaces to Ruby by taking inspiration from how Clojure handles its namespaces.
5
+ It is primarly a simplification of the existing module syntax. Great for deeply nested modules or for attempting a more functinoal approach to writing Ruby code.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'namespacing'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install namespacing
20
+
21
+ ## Usage
22
+
23
+ Simply require the gem and decide the scope you'd like to have it at. To use it in the global scope, you'll want to extend `Object`
24
+
25
+ ```rb
26
+ require 'namespacing'
27
+
28
+ class Object
29
+ include Namespacing
30
+ end
31
+
32
+ ns 'my_app.dojo.util.options' do
33
+ def names
34
+ %w(on off maybe 7 42 tuesday)
35
+ end
36
+ end
37
+ ```
38
+ Then this code can be called with:
39
+ ```rb
40
+ MyApp::Dojo::Util::Options.names
41
+ #=> ['on', 'off', 'maybe', '7', '42', 'tuesday']
42
+ ```
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( http://github.com/<my-github-username>/namespacing/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,33 @@
1
+ require "namespacing/version"
2
+
3
+ module Namespacing
4
+ def ns(namespace, delim = '.', &block)
5
+ nest_mod(namespace.split(delim), block)
6
+ end
7
+
8
+ private
9
+ def nest_mod(mod = Kernel, module_names, block)
10
+ return mod.module_exec(&block) if module_names.empty?
11
+ find_or_create_constant_in_module(mod, to_const(module_names.first)).tap do |this|
12
+ make_module_methods_accessible(this)
13
+ this.module_exec do
14
+ nest_mod(this, module_names.drop(1), block)
15
+ end
16
+ end
17
+ end
18
+
19
+ def make_module_methods_accessible(mod)
20
+ mod.module_exec { extend self }
21
+ end
22
+
23
+ def find_or_create_constant_in_module(mod, str)
24
+ return mod.const_get(str) if mod.const_defined?(str)
25
+ return mod.const_set(str, Module.new)
26
+ end
27
+
28
+ def to_const(str)
29
+ str.split('_')
30
+ .map(&:capitalize)
31
+ .join
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Namespacing
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'namespacing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "namespacing"
8
+ spec.version = Namespacing::VERSION
9
+ spec.authors = ["Justin Herrick"]
10
+ spec.email = ["justin@justinherrick.com"]
11
+ spec.summary = %q{Adds Clojure like Namespacing to Ruby.}
12
+ spec.description = %q{Allows you to succintly define deeply nested modules.}
13
+ spec.homepage = "https://github.com/jah2488/namespacing"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
+ end
@@ -0,0 +1,67 @@
1
+ require_relative '../lib/namespacing'
2
+
3
+ class Object
4
+ include Namespacing
5
+ end
6
+
7
+ describe Namespacing do
8
+
9
+ context 'ns' do
10
+
11
+ it 'creates all methods in the nested namespace/module' do
12
+ ns 'my_app.dojo.util.options' do
13
+ def names
14
+ %w(john jill steve carol)
15
+ end
16
+ end
17
+ expect(MyApp::Dojo::Util::Options.names).to eq(%w(john jill steve carol))
18
+ end
19
+
20
+ it 'custom deliminators can optionally be specified' do
21
+ ns 'my_app|dojo|util|options', '|' do
22
+ def names
23
+ %w(john jill steve carol)
24
+ end
25
+ end
26
+ expect(MyApp::Dojo::Util::Options.names).to eq(%w(john jill steve carol))
27
+ end
28
+ end
29
+
30
+ context 'nest_mod' do
31
+
32
+ it 'creates a module given a string and block' do
33
+ nest_mod(['greeter'], Proc.new {
34
+ @hello = 'hello'
35
+ def greet
36
+ @hello
37
+ end
38
+ })
39
+ expect(Greeter.greet).to eq('hello')
40
+ end
41
+
42
+ it 'creates nested modules' do
43
+ nest_mod(['dojo', 'util', 'options'], Proc.new {
44
+ def names
45
+ %w(john jill steve carol)
46
+ end
47
+ })
48
+ expect(Dojo::Util::Options.names).to eq(%w(john jill steve carol))
49
+ end
50
+ end
51
+
52
+ context 'find_or_create_constant_in_module' do
53
+ it { expect(find_or_create_constant_in_module(Kernel, 'Hello')).to eq(Hello) }
54
+ it { expect(find_or_create_constant_in_module(Kernel, 'Hello')).to be_a(Module) }
55
+ end
56
+
57
+ context 'to_const' do
58
+
59
+ it 'hello -> Hello' do
60
+ expect(to_const('hello')).to eq('Hello')
61
+ end
62
+
63
+ it 'hello_world -> HelloWorld' do
64
+ expect(to_const('hello_world')).to eq('HelloWorld')
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namespacing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin Herrick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description: Allows you to succintly define deeply nested modules.
56
+ email:
57
+ - justin@justinherrick.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/namespacing.rb
70
+ - lib/namespacing/version.rb
71
+ - namespacing.gemspec
72
+ - spec/namespacing_spec.rb
73
+ homepage: https://github.com/jah2488/namespacing
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Adds Clojure like Namespacing to Ruby.
97
+ test_files:
98
+ - spec/namespacing_spec.rb