including 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +49 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/including.gemspec +62 -0
- data/lib/including.rb +17 -0
- data/spec/including_spec.rb +88 -0
- data/spec/spec_helper.rb +11 -0
- metadata +101 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.8.3)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rdoc
|
11
|
+
json (1.6.5)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
rdoc (3.12)
|
14
|
+
json (~> 1.4)
|
15
|
+
rspec (2.8.0)
|
16
|
+
rspec-core (~> 2.8.0)
|
17
|
+
rspec-expectations (~> 2.8.0)
|
18
|
+
rspec-mocks (~> 2.8.0)
|
19
|
+
rspec-core (2.8.0)
|
20
|
+
rspec-expectations (2.8.0)
|
21
|
+
diff-lcs (~> 1.1.2)
|
22
|
+
rspec-mocks (2.8.0)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 1.0.0)
|
29
|
+
jeweler (~> 1.8.3)
|
30
|
+
rspec (~> 2.8.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Bryan Woods
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= including
|
2
|
+
|
3
|
+
* An observation: Other languages allow for more granular loading of only desired methods and functions from module code.
|
4
|
+
* An experiment: How might this be done in Ruby?
|
5
|
+
* A warning: Running this code in production might not be wise.
|
6
|
+
|
7
|
+
= How does it work?
|
8
|
+
|
9
|
+
Let's say you've got a module:
|
10
|
+
module Foo
|
11
|
+
def foo
|
12
|
+
"foo"
|
13
|
+
end
|
14
|
+
|
15
|
+
def bar
|
16
|
+
"bar"
|
17
|
+
end
|
18
|
+
|
19
|
+
def baz
|
20
|
+
"baz"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
And you want to mix it in to a class. But wait! the `foo` method might
|
25
|
+
conflict with some other `foo`!
|
26
|
+
class Bar
|
27
|
+
including Foo, only: [:bar]
|
28
|
+
end
|
29
|
+
|
30
|
+
bar = Bar.new
|
31
|
+
bar.bar # "bar"
|
32
|
+
bar.foo # NoMethodError
|
33
|
+
|
34
|
+
That's basically it. You can pass an array of `only` the methods you'd
|
35
|
+
like to mix in, or an array of exceptions, via `except`.
|
36
|
+
|
37
|
+
class Quux
|
38
|
+
including Foo, except: [:foo, :baz]
|
39
|
+
end
|
40
|
+
|
41
|
+
quux = Quux.new
|
42
|
+
quux.bar # "bar"
|
43
|
+
quux.foo # NoMethodError
|
44
|
+
quux.baz # NoMethodError
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2012 Bryan Woods. See LICENSE.txt for
|
49
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
gem.name = "including"
|
17
|
+
gem.homepage = "http://github.com/bryanwoods/including"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{Including mixes in only what you want}
|
20
|
+
gem.description = %Q{
|
21
|
+
An observation: Other languages allow for more granular loading of only
|
22
|
+
desired methods and functions from module code.
|
23
|
+
|
24
|
+
An experiment: How might this be done in Ruby?
|
25
|
+
}
|
26
|
+
gem.email = "bryanwoods4e@gmail.com"
|
27
|
+
gem.authors = ["Bryan Woods"]
|
28
|
+
end
|
29
|
+
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
31
|
+
|
32
|
+
require 'rspec/core'
|
33
|
+
require 'rspec/core/rake_task'
|
34
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
39
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
40
|
+
spec.rcov = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :spec
|
44
|
+
|
45
|
+
require 'rdoc/task'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "including #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/including.gemspec
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{including}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Bryan Woods}]
|
12
|
+
s.date = %q{2012-03-07}
|
13
|
+
s.description = %q{
|
14
|
+
An observation: Other languages allow for more granular loading of only
|
15
|
+
desired methods and functions from module code.
|
16
|
+
|
17
|
+
An experiment: How might this be done in Ruby?
|
18
|
+
}
|
19
|
+
s.email = %q{bryanwoods4e@gmail.com}
|
20
|
+
s.extra_rdoc_files = [
|
21
|
+
"LICENSE.txt",
|
22
|
+
"README.rdoc"
|
23
|
+
]
|
24
|
+
s.files = [
|
25
|
+
".document",
|
26
|
+
".rspec",
|
27
|
+
"Gemfile",
|
28
|
+
"Gemfile.lock",
|
29
|
+
"LICENSE.txt",
|
30
|
+
"README.rdoc",
|
31
|
+
"Rakefile",
|
32
|
+
"VERSION",
|
33
|
+
"including.gemspec",
|
34
|
+
"lib/including.rb",
|
35
|
+
"spec/including_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/bryanwoods/including}
|
39
|
+
s.licenses = [%q{MIT}]
|
40
|
+
s.require_paths = [%q{lib}]
|
41
|
+
s.rubygems_version = %q{1.8.5}
|
42
|
+
s.summary = %q{Including mixes in only what you want}
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
49
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
53
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
data/lib/including.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Object
|
2
|
+
def including(mixin, options = {})
|
3
|
+
include mixin and return unless options[:only] || options[:except]
|
4
|
+
included_module = mixin.dup
|
5
|
+
|
6
|
+
excluded_methods = (
|
7
|
+
options[:except] ||
|
8
|
+
included_module.instance_methods(false) - options[:only]
|
9
|
+
).map(&:to_sym)
|
10
|
+
|
11
|
+
(include included_module).tap do
|
12
|
+
excluded_methods.each do |meth|
|
13
|
+
included_module.send(:undef_method, :"#{meth}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Object" do
|
4
|
+
before do
|
5
|
+
module Foo
|
6
|
+
def foo; "foo"; end
|
7
|
+
def bar; "bar"; end
|
8
|
+
def baz; "baz"; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Bar; end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#including" do
|
15
|
+
context "provided no options arguments" do
|
16
|
+
context "provided a valid module" do
|
17
|
+
before do
|
18
|
+
Bar.class_eval do
|
19
|
+
including Foo
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "mixes in the provided module" do
|
24
|
+
bar = Bar.new
|
25
|
+
|
26
|
+
bar.foo.should == "foo"
|
27
|
+
bar.bar.should == "bar"
|
28
|
+
bar.baz.should == "baz"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "provided an invalid module" do
|
33
|
+
it "raises an exception" do
|
34
|
+
expect do
|
35
|
+
Bar.class_eval { including NotAModule }
|
36
|
+
end.to raise_error(NameError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "provided an 'only' options argument" do
|
43
|
+
before do
|
44
|
+
Bar.class_eval { including Foo, only: [:foo, :bar] }
|
45
|
+
end
|
46
|
+
|
47
|
+
it "undefines all methods listed in the 'only' array" do
|
48
|
+
bar = Bar.new
|
49
|
+
|
50
|
+
bar.foo.should == "foo"
|
51
|
+
bar.bar.should == "bar"
|
52
|
+
|
53
|
+
expect { bar.baz }.to raise_error(NoMethodError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "provided an 'except' options argumenmt" do
|
58
|
+
context "given an array of symbols" do
|
59
|
+
before do
|
60
|
+
Bar.class_eval { including Foo, except: [:foo] }
|
61
|
+
end
|
62
|
+
|
63
|
+
it "undefines all methods not listed in the 'except' array" do
|
64
|
+
bar = Bar.new
|
65
|
+
|
66
|
+
expect { bar.foo }.to raise_error(NoMethodError)
|
67
|
+
|
68
|
+
bar.bar.should == "bar"
|
69
|
+
bar.baz.should == "baz"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "given an array with at least one string" do
|
74
|
+
before do
|
75
|
+
Bar.class_eval { including Foo, except: ["foo"] }
|
76
|
+
end
|
77
|
+
|
78
|
+
it "undefines all methods not listed in the 'except' array" do
|
79
|
+
bar = Bar.new
|
80
|
+
|
81
|
+
expect { bar.foo }.to raise_error(NoMethodError)
|
82
|
+
|
83
|
+
bar.bar.should == "bar"
|
84
|
+
bar.baz.should == "baz"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'including'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: including
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bryan Woods
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-03-07 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.8.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.8.3
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
description: "\n An observation: Other languages allow for more granular loading of only \n desired methods and functions from module code.\n\n An experiment: How might this be done in Ruby?\n "
|
49
|
+
email: bryanwoods4e@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .rspec
|
60
|
+
- Gemfile
|
61
|
+
- Gemfile.lock
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.rdoc
|
64
|
+
- Rakefile
|
65
|
+
- VERSION
|
66
|
+
- including.gemspec
|
67
|
+
- lib/including.rb
|
68
|
+
- spec/including_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: http://github.com/bryanwoods/including
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 716551475364656808
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.8.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Including mixes in only what you want
|
100
|
+
test_files: []
|
101
|
+
|