including 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,13 +35,13 @@ That's basically it. You can pass an array of `only` the methods you'd
35
35
  like to mix in, or an array of exceptions, via `except`.
36
36
 
37
37
  class Quux
38
- including Foo, except: [:foo, :baz]
38
+ including Foo, only: [:foo, :baz]
39
39
  end
40
40
 
41
41
  quux = Quux.new
42
- quux.bar # "bar"
43
- quux.foo # NoMethodError
44
- quux.baz # NoMethodError
42
+ quux.foo # "foo"
43
+ quux.bar # NoMethodError
44
+ quux.baz # "baz"
45
45
 
46
46
  = Installation
47
47
  gem install including
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -4,19 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{including}
8
- s.version = "0.1.1"
7
+ s.name = "including"
8
+ s.version = "0.2.0"
9
9
 
10
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}
11
+ s.authors = ["Bryan Woods"]
12
+ s.date = "2012-04-05"
13
+ s.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 "
14
+ s.email = "bryanwoods4e@gmail.com"
20
15
  s.extra_rdoc_files = [
21
16
  "LICENSE.txt",
22
17
  "README.rdoc"
@@ -35,11 +30,11 @@ Gem::Specification.new do |s|
35
30
  "spec/including_spec.rb",
36
31
  "spec/spec_helper.rb"
37
32
  ]
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}
33
+ s.homepage = "http://github.com/bryanwoods/including"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.16"
37
+ s.summary = "Including mixes in only what you want"
43
38
 
44
39
  if s.respond_to? :specification_version then
45
40
  s.specification_version = 3
@@ -1,15 +1,25 @@
1
- class Object
1
+ class Module
2
2
  def including(mixin, options = {})
3
3
  include mixin and return unless options[:only] || options[:except]
4
4
  included_module = mixin.dup
5
5
 
6
- excluded_methods = (
7
- options[:except] ||
8
- included_module.instance_methods(false) - options[:only]
9
- ).map(&:to_sym)
6
+ if options[:except]
7
+ excluded_methods = options[:except]
10
8
 
9
+ undefine(included_module, excluded_methods) and return
10
+ else
11
+ included_methods = included_module.instance_methods(false)
12
+ excluded_methods = Array(options[:only]).map(&:to_sym)
13
+
14
+ undefine(included_module, included_methods - excluded_methods)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def undefine(included_module, excluded_methods)
11
21
  (include included_module).tap do
12
- excluded_methods.each do |meth|
22
+ Array(excluded_methods).flatten.each do |meth|
13
23
  included_module.send(:undef_method, meth)
14
24
  end
15
25
  end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "Object" do
3
+ describe Module do
4
4
  before do
5
5
  module Foo
6
6
  def foo; "foo"; end
@@ -12,15 +12,15 @@ describe "Object" do
12
12
  end
13
13
 
14
14
  describe "#including" do
15
- context "provided no options arguments" do
16
- context "provided a valid module" do
15
+ context "given no options arguments" do
16
+ context "given a valid module" do
17
17
  before do
18
18
  Bar.class_eval do
19
19
  including Foo
20
20
  end
21
21
  end
22
22
 
23
- it "mixes in the provided module" do
23
+ it "mixes in the given module" do
24
24
  bar = Bar.new
25
25
 
26
26
  bar.foo.should == "foo"
@@ -29,7 +29,7 @@ describe "Object" do
29
29
  end
30
30
  end
31
31
 
32
- context "provided an invalid module" do
32
+ context "given an invalid module" do
33
33
  it "raises an exception" do
34
34
  expect do
35
35
  Bar.class_eval { including NotAModule }
@@ -37,51 +37,111 @@ describe "Object" do
37
37
  end
38
38
  end
39
39
  end
40
- end
41
40
 
42
- context "provided an 'only' options argument" do
43
- before do
44
- Bar.class_eval { including Foo, only: [:foo, :bar] }
45
- end
41
+ context "given an 'only' options argument" do
42
+ context "given an array" do
43
+ before do
44
+ Bar.class_eval { including Foo, only: [:foo, :bar] }
45
+ end
46
46
 
47
- it "undefines all methods listed in the 'only' array" do
48
- bar = Bar.new
47
+ it "undefines all methods not listed in the 'only' array" do
48
+ bar = Bar.new
49
49
 
50
- bar.foo.should == "foo"
51
- bar.bar.should == "bar"
50
+ bar.foo.should == "foo"
51
+ bar.bar.should == "bar"
52
52
 
53
- expect { bar.baz }.to raise_error(NoMethodError)
54
- end
55
- end
53
+ expect { bar.baz }.to raise_error(NoMethodError)
54
+ end
55
+ end
56
+
57
+ context "given a symbol" do
58
+ before do
59
+ Bar.class_eval { including Foo, only: :bar }
60
+ end
61
+
62
+ it "undefines all other methods" do
63
+ bar = Bar.new
56
64
 
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] }
65
+ bar.bar.should == "bar"
66
+
67
+ expect { bar.foo }.to raise_error(NoMethodError)
68
+ end
61
69
  end
62
70
 
63
- it "undefines all methods not listed in the 'except' array" do
64
- bar = Bar.new
71
+ context "given a string" do
72
+ before do
73
+ Bar.class_eval { including Foo, only: "bar" }
74
+ end
75
+
76
+ it "undefines all other methods" do
77
+ bar = Bar.new
65
78
 
66
- expect { bar.foo }.to raise_error(NoMethodError)
79
+ bar.bar.should == "bar"
67
80
 
68
- bar.bar.should == "bar"
69
- bar.baz.should == "baz"
81
+ expect { bar.foo }.to raise_error(NoMethodError)
82
+ end
70
83
  end
71
84
  end
72
85
 
73
- context "given an array with at least one string" do
74
- before do
75
- Bar.class_eval { including Foo, except: ["foo"] }
86
+ context "given an 'except' options argumenmt" do
87
+ context "given an array of symbols" do
88
+ before do
89
+ Bar.class_eval { including Foo, except: [:foo] }
90
+ end
91
+
92
+ it "undefines all methods listed in the 'except' array" do
93
+ bar = Bar.new
94
+
95
+ expect { bar.foo }.to raise_error(NoMethodError)
96
+
97
+ bar.bar.should == "bar"
98
+ bar.baz.should == "baz"
99
+ end
100
+ end
101
+
102
+ context "given an array with at least one string" do
103
+ before do
104
+ Bar.class_eval { including Foo, except: ["foo"] }
105
+ end
106
+
107
+ it "undefines all methods listed in the 'except' array" do
108
+ bar = Bar.new
109
+
110
+ expect { bar.foo }.to raise_error(NoMethodError)
111
+
112
+ bar.bar.should == "bar"
113
+ bar.baz.should == "baz"
114
+ end
76
115
  end
77
116
 
78
- it "undefines all methods not listed in the 'except' array" do
79
- bar = Bar.new
117
+ context "given a symbol" do
118
+ before do
119
+ Bar.class_eval { including Foo, except: :foo }
120
+ end
80
121
 
81
- expect { bar.foo }.to raise_error(NoMethodError)
122
+ it "undefines the given method" do
123
+ bar = Bar.new
82
124
 
83
- bar.bar.should == "bar"
84
- bar.baz.should == "baz"
125
+ expect { bar.foo }.to raise_error(NoMethodError)
126
+
127
+ bar.bar.should == "bar"
128
+ bar.baz.should == "baz"
129
+ end
130
+ end
131
+
132
+ context "given a string" do
133
+ before do
134
+ Bar.class_eval { including Foo, except: "foo" }
135
+ end
136
+
137
+ it "undefines the given method" do
138
+ bar = Bar.new
139
+
140
+ expect { bar.foo }.to raise_error(NoMethodError)
141
+
142
+ bar.bar.should == "bar"
143
+ bar.baz.should == "baz"
144
+ end
85
145
  end
86
146
  end
87
147
  end
metadata CHANGED
@@ -1,60 +1,59 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: including
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
4
5
  prerelease:
5
- version: 0.1.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Bryan Woods
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-03-07 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-04-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70158534189880 !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
18
+ requirements:
20
19
  - - ~>
21
- - !ruby/object:Gem::Version
20
+ - !ruby/object:Gem::Version
22
21
  version: 2.8.0
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *70158534189880
25
+ - !ruby/object:Gem::Dependency
27
26
  name: bundler
28
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70158534189280 !ruby/object:Gem::Requirement
29
28
  none: false
30
- requirements:
29
+ requirements:
31
30
  - - ~>
32
- - !ruby/object:Gem::Version
31
+ - !ruby/object:Gem::Version
33
32
  version: 1.0.0
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *70158534189280
36
+ - !ruby/object:Gem::Dependency
38
37
  name: jeweler
39
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &70158534188360 !ruby/object:Gem::Requirement
40
39
  none: false
41
- requirements:
40
+ requirements:
42
41
  - - ~>
43
- - !ruby/object:Gem::Version
42
+ - !ruby/object:Gem::Version
44
43
  version: 1.8.3
45
44
  type: :development
46
45
  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 "
46
+ version_requirements: *70158534188360
47
+ description: ! "\n An observation: Other languages allow for more granular loading
48
+ of only \n desired methods and functions from module code.\n\n An experiment:
49
+ How might this be done in Ruby?\n "
49
50
  email: bryanwoods4e@gmail.com
50
51
  executables: []
51
-
52
52
  extensions: []
53
-
54
- extra_rdoc_files:
53
+ extra_rdoc_files:
55
54
  - LICENSE.txt
56
55
  - README.rdoc
57
- files:
56
+ files:
58
57
  - .document
59
58
  - .rspec
60
59
  - Gemfile
@@ -68,34 +67,31 @@ files:
68
67
  - spec/including_spec.rb
69
68
  - spec/spec_helper.rb
70
69
  homepage: http://github.com/bryanwoods/including
71
- licenses:
70
+ licenses:
72
71
  - MIT
73
72
  post_install_message:
74
73
  rdoc_options: []
75
-
76
- require_paths:
74
+ require_paths:
77
75
  - lib
78
- required_ruby_version: !ruby/object:Gem::Requirement
76
+ required_ruby_version: !ruby/object:Gem::Requirement
79
77
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- hash: 4074772481834400452
84
- segments:
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ segments:
85
83
  - 0
86
- version: "0"
87
- required_rubygems_version: !ruby/object:Gem::Requirement
84
+ hash: 260768192517672204
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  none: false
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- version: "0"
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
93
91
  requirements: []
94
-
95
92
  rubyforge_project:
96
- rubygems_version: 1.8.5
93
+ rubygems_version: 1.8.16
97
94
  signing_key:
98
95
  specification_version: 3
99
96
  summary: Including mixes in only what you want
100
97
  test_files: []
101
-