use 1.3.1 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +8 -0
  3. data/README +0 -1
  4. data/Rakefile +33 -0
  5. data/lib/use.rb +161 -162
  6. data/test/test_use.rb +1 -1
  7. data/use.gemspec +26 -0
  8. metadata +59 -42
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5cc4ebf5e672d9172d6f5d8186cc103db14df3b8
4
+ data.tar.gz: 56617771d511b621577cbf9636a80c39356c79ee
5
+ SHA512:
6
+ metadata.gz: 90fae6e4aa251b1df17116f960c9f69bbc6d89bcd7882ed3e9c04ed258a3cc87072c7b659038defc9527ec3832742cf1f30377b0dbdfc504195f258c969f28b4
7
+ data.tar.gz: 57595675316a3979ad8e8940dc9404061250e433f89f732cc03abbb59991f4d964927715871620dbf3cc80a57c38a3dc8f3ea1e33a87f773b149f478982ed49a
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ == 1.3.3 - 12-Oct-2014
2
+ * Rakefile, gemspec and README updates.
3
+ * Fixed an unused variable warning.
4
+
5
+ == 1.3.2 - 8-Oct-2009
6
+ * Updated and fixed gemspec.
7
+ * Added the :gem rake task.
8
+
1
9
  == 1.3.1 - 31-Jul-2009
2
10
  * Fixed a bug where identical methods from different modules were being
3
11
  inadvertently undefined.
data/README CHANGED
@@ -91,4 +91,3 @@ USE_VERSION
91
91
 
92
92
  == Author
93
93
  Daniel J. Berger
94
- djberg96 at nospam at gmail dot com
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+
5
+ CLEAN.include("**/*.gem", "**/*.rbc", "**/*.rbx")
6
+
7
+ namespace :gem do
8
+ desc "Create the use gem"
9
+ task :create => [:clean] do
10
+ spec = eval(IO.read('use.gemspec'))
11
+ if Gem::VERSION < "2.0"
12
+ Gem::Builder.new(spec).build
13
+ else
14
+ require 'rubygems/package'
15
+ Gem::Package.build(spec)
16
+ end
17
+ end
18
+
19
+ desc "Install the use gem"
20
+ task :install => [:create] do
21
+ file = Dir["*.gem"].first
22
+ sh "gem install -l #{file}"
23
+ end
24
+ end
25
+
26
+ Rake::TestTask.new do |t|
27
+ task :test => :clean
28
+ t.libs << 'test'
29
+ t.verbose = true
30
+ t.warning = true
31
+ end
32
+
33
+ task :default => :test
data/lib/use.rb CHANGED
@@ -1,178 +1,177 @@
1
1
  require 'structured_warnings'
2
2
 
3
3
  unless defined? MethodRedefinedWarning
4
- # Warning raised in $VERBOSE mode if a method is shadowed.
5
- class MethodRedefinedWarning < Warning
6
- end
4
+ # Warning raised in $VERBOSE mode if a method is shadowed.
5
+ class MethodRedefinedWarning < Warning
6
+ end
7
7
  end
8
8
 
9
9
  class Class
10
- # The version of the 'use' library
11
- USE_VERSION = '1.3.1'
12
-
13
- # Allows you to include mixins in a fine grained manner. Instead of
14
- # including all methods from a given module, you can can instead mixin
15
- # only those methods you want through a combination of the :include,
16
- # :exclude, and :alias options.
17
- #
18
- # Examples:
19
- #
20
- # # Defines a 'bar' and 'baz' method
21
- # module Alpha
22
- # def bar
23
- # puts 'hello'
24
- # end
25
- # def baz
26
- # puts 'world'
27
- # end
28
- # end
29
- #
30
- # # Defines a 'bar', 'blah', and 'zap' methods
31
- # module Beta
32
- # def bar
33
- # puts 'goodbye'
34
- # end
35
- # def blah
36
- # puts 'new york'
37
- # end
38
- # def zap
39
- # puts 'zap'
40
- # end
41
- # end
42
- #
43
- # # From the Alpha module, only mixin the 'bar' method. From the Beta
44
- # # module exclude the 'bar' and 'zap' methods.
45
- # class Zap
46
- # use Alpha, :bar
47
- # use Beta, :exclude => [:bar, :zap]
48
- # end
49
- #
50
- # z = Zap.new
51
- #
52
- # z.bar # => "hello"
53
- # z.baz # => NoMethodError - wasn't mixed in
54
- # z.zap # => NoMethodError - wasn't mixed in
55
- # z.blah # => "new york"
56
- #
57
- # # Alias a method on the fly
58
- # class MyKlass
59
- # use Alpha, :alias => {:bar, :test}
60
- # end
61
- #
62
- # m = MyKlass.new
63
- # m.test # => 'hello'
64
- # m.bar # => NoMethodError - was aliased to 'test'
65
- #
66
- # If no options follow the module name this method is identical
67
- # to a standard include.
68
- #--
69
- # Designer's note: Most of the explicit .to_s calls on method lists are
70
- # here to deal with the fact that Ruby 1.8 returns strings for method
71
- # lists while Ruby 1.9 returns symbols. To ensure compatibility, and
72
- # preserve my sanity, all method names are converted to strings.
73
- #
74
- def use(*args)
75
- valid_keys = %w/include exclude alias/
76
- excluded_methods = []
77
- included_methods = []
78
- aliased_methods = []
79
-
80
- mod = args.shift.clone
81
-
82
- # If no arguments follow the module name, treat it as a standard include
83
- if args.empty?
84
- included_methods.concat(mod.instance_methods)
85
- end
86
-
87
- m = Module.new
88
-
89
- args.each{ |arg|
90
- if arg.kind_of?(Hash)
91
- arg.each{ |key, val|
92
- case key.to_s
93
- when 'include'
94
- if val.respond_to?(:each)
95
- val.each{ |element| included_methods << element.to_s }
96
- else
97
- included_methods << val.to_s
98
- end
99
- when 'exclude'
100
- if val.respond_to?(:each)
101
- val.each{ |element| excluded_methods << element.to_s }
102
- else
103
- excluded_methods << val.to_s
104
- end
105
- when 'alias'
106
- aliased_methods.push(val)
107
- else
108
- raise "invalid key '#{key}'"
109
- end
110
- }
111
- else
112
- included_methods.push(arg.to_s)
113
- end
114
- }
115
-
116
- unless included_methods.empty? || excluded_methods.empty?
117
- err = 'you cannot include and exclude in the same statement'
118
- raise ArgumentError, err
10
+ # The version of the 'use' library
11
+ USE_VERSION = '1.3.3'
12
+
13
+ # Allows you to include mixins in a fine grained manner. Instead of
14
+ # including all methods from a given module, you can can instead mixin
15
+ # only those methods you want through a combination of the :include,
16
+ # :exclude, and :alias options.
17
+ #
18
+ # Examples:
19
+ #
20
+ # # Defines a 'bar' and 'baz' method
21
+ # module Alpha
22
+ # def bar
23
+ # puts 'hello'
24
+ # end
25
+ # def baz
26
+ # puts 'world'
27
+ # end
28
+ # end
29
+ #
30
+ # # Defines a 'bar', 'blah', and 'zap' methods
31
+ # module Beta
32
+ # def bar
33
+ # puts 'goodbye'
34
+ # end
35
+ # def blah
36
+ # puts 'new york'
37
+ # end
38
+ # def zap
39
+ # puts 'zap'
40
+ # end
41
+ # end
42
+ #
43
+ # # From the Alpha module, only mixin the 'bar' method. From the Beta
44
+ # # module exclude the 'bar' and 'zap' methods.
45
+ # class Zap
46
+ # use Alpha, :bar
47
+ # use Beta, :exclude => [:bar, :zap]
48
+ # end
49
+ #
50
+ # z = Zap.new
51
+ #
52
+ # z.bar # => "hello"
53
+ # z.baz # => NoMethodError - wasn't mixed in
54
+ # z.zap # => NoMethodError - wasn't mixed in
55
+ # z.blah # => "new york"
56
+ #
57
+ # # Alias a method on the fly
58
+ # class MyKlass
59
+ # use Alpha, :alias => {:bar, :test}
60
+ # end
61
+ #
62
+ # m = MyKlass.new
63
+ # m.test # => 'hello'
64
+ # m.bar # => NoMethodError - was aliased to 'test'
65
+ #
66
+ # If no options follow the module name this method is identical
67
+ # to a standard include.
68
+ #--
69
+ # Designer's note: Most of the explicit .to_s calls on method lists are
70
+ # here to deal with the fact that Ruby 1.8 returns strings for method
71
+ # lists while Ruby 1.9 returns symbols. To ensure compatibility, and
72
+ # preserve my sanity, all method names are converted to strings.
73
+ #
74
+ def use(*args)
75
+ excluded_methods = []
76
+ included_methods = []
77
+ aliased_methods = []
78
+
79
+ mod = args.shift.clone
80
+
81
+ # If no arguments follow the module name, treat it as a standard include
82
+ if args.empty?
83
+ included_methods.concat(mod.instance_methods)
84
+ end
85
+
86
+ m = Module.new
87
+
88
+ args.each{ |arg|
89
+ if arg.kind_of?(Hash)
90
+ arg.each{ |key, val|
91
+ case key.to_s
92
+ when 'include'
93
+ if val.respond_to?(:each)
94
+ val.each{ |element| included_methods << element.to_s }
95
+ else
96
+ included_methods << val.to_s
97
+ end
98
+ when 'exclude'
99
+ if val.respond_to?(:each)
100
+ val.each{ |element| excluded_methods << element.to_s }
101
+ else
102
+ excluded_methods << val.to_s
103
+ end
104
+ when 'alias'
105
+ aliased_methods.push(val)
106
+ else
107
+ raise "invalid key '#{key}'"
108
+ end
109
+ }
110
+ else
111
+ included_methods.push(arg.to_s)
119
112
  end
113
+ }
120
114
 
121
- local_instance_methods = mod.instance_methods.map{ |e| e.to_s }
115
+ unless included_methods.empty? || excluded_methods.empty?
116
+ err = 'you cannot include and exclude in the same statement'
117
+ raise ArgumentError, err
118
+ end
122
119
 
123
- excluded_methods.map!{ |e| e.to_s }
120
+ local_instance_methods = mod.instance_methods.map{ |e| e.to_s }
124
121
 
125
- # Remove excluded_methods methods
126
- unless excluded_methods.empty?
127
- (local_instance_methods & excluded_methods).each{ |meth|
128
- mod.module_eval{ remove_method(meth) }
129
- }
130
- end
122
+ excluded_methods.map!{ |e| e.to_s }
131
123
 
132
- # Alias methods. All aliased methods are automatically included.
133
- aliased_methods.each{ |pair|
134
- pair.each{ |old_method, new_method|
135
- included_methods << new_method
136
- mod.module_eval{
137
- alias_method(new_method, old_method)
138
- remove_method(old_method) rescue nil
139
- }
140
- }
124
+ # Remove excluded_methods methods
125
+ unless excluded_methods.empty?
126
+ (local_instance_methods & excluded_methods).each{ |meth|
127
+ mod.module_eval{ remove_method(meth) }
141
128
  }
129
+ end
130
+
131
+ # Alias methods. All aliased methods are automatically included.
132
+ aliased_methods.each{ |pair|
133
+ pair.each{ |old_method, new_method|
134
+ included_methods << new_method
135
+ mod.module_eval{
136
+ alias_method(new_method, old_method)
137
+ remove_method(old_method) rescue nil
138
+ }
139
+ }
140
+ }
141
+
142
+ included_methods.map!{ |e| e.to_s }
143
+
144
+ # Remove all methods not specifically included. The rescue was needed
145
+ # for those cases where a module included another module. Also, don't
146
+ # remove methods from classes that already exist unless specifically
147
+ # included.
148
+ unless included_methods.empty?
149
+ self_instance_methods = self.instance_methods.map{ |e| e.to_s }
150
+ (local_instance_methods - included_methods).each{ |meth|
151
+ if self_instance_methods.include?(meth)
152
+ if included_methods.include?(meth)
153
+ mod.module_eval{ undef_method(meth) rescue nil }
154
+ else
155
+ mod.module_eval{ remove_method(meth) rescue nil }
156
+ end
157
+ else
158
+ mod.module_eval{ undef_method(meth) rescue nil }
159
+ end
160
+ }
161
+ end
142
162
 
143
- included_methods.map!{ |e| e.to_s }
144
-
145
- # Remove all methods not specifically included. The rescue was needed
146
- # for those cases where a module included another module. Also, don't
147
- # remove methods from classes that already exist unless specifically
148
- # included.
149
- unless included_methods.empty?
150
- self_instance_methods = self.instance_methods.map{ |e| e.to_s }
151
- (local_instance_methods - included_methods).each{ |meth|
152
- if self_instance_methods.include?(meth)
153
- if included_methods.include?(meth)
154
- mod.module_eval{ undef_method(meth) rescue nil }
155
- else
156
- mod.module_eval{ remove_method(meth) rescue nil }
157
- end
158
- else
159
- mod.module_eval{ undef_method(meth) rescue nil }
160
- end
161
- }
162
- end
163
-
164
- m.module_eval{ include mod }
163
+ m.module_eval{ include mod }
165
164
 
166
- # Raise a warning if methods are shadowed (in $VERBOSE mode)
167
- if $VERBOSE
168
- local_instance_methods = instance_methods(true)
169
- m.instance_methods.each{ |meth|
170
- next unless local_instance_methods.include?(meth)
171
- msg = "method '#{meth}' aliased, shadows old '#{meth}'"
172
- warn MethodRedefinedWarning, msg
173
- }
174
- end
165
+ # Raise a warning if methods are shadowed (in $VERBOSE mode)
166
+ if $VERBOSE
167
+ local_instance_methods = instance_methods(true)
168
+ m.instance_methods.each{ |meth|
169
+ next unless local_instance_methods.include?(meth)
170
+ msg = "method '#{meth}' aliased, shadows old '#{meth}'"
171
+ warn MethodRedefinedWarning, msg
172
+ }
173
+ end
175
174
 
176
- include m
177
- end
175
+ include m
176
+ end
178
177
  end
data/test/test_use.rb CHANGED
@@ -25,7 +25,7 @@ class TC_Use < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_version
28
- assert_equal('1.3.1', Class::USE_VERSION)
28
+ assert_equal('1.3.3', Class::USE_VERSION)
29
29
  end
30
30
 
31
31
  def test_mod_a_methods
data/use.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'use'
5
+ gem.version = '1.3.3'
6
+ gem.author = 'Daniel J. Berger'
7
+ gem.license = 'Artistic 2.0'
8
+ gem.email = 'djberg96@gmail.com'
9
+ gem.homepage = 'https://github.com/djberg96/use'
10
+ gem.summary = 'Selectively mixin methods from a given module'
11
+ gem.test_file = 'test/test_use.rb'
12
+ gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
13
+
14
+ gem.extra_rdoc_files = ['MANIFEST', 'README', 'CHANGES']
15
+
16
+ gem.add_dependency('structured_warnings', '>= 0.1.1')
17
+ gem.add_development_dependency('rake')
18
+
19
+ gem.description = <<-EOF
20
+ The use library solves the multi-mixin problem by allowing you to
21
+ selectively mixin specific methods from a module rather than mixing
22
+ in all of them. In addition, you can alias methods on the fly as they
23
+ are mixed in, effectively allowing you to change the name of the mixin
24
+ method.
25
+ EOF
26
+ end
metadata CHANGED
@@ -1,72 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: use
3
- version: !ruby/object:Gem::Version
4
- version: 1.3.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.3
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Daniel J. Berger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2009-07-31 00:00:00 -06:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: structured_warnings
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
17
20
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
21
24
  - - ">="
22
- - !ruby/object:Gem::Version
25
+ - !ruby/object:Gem::Version
23
26
  version: 0.1.1
24
- version:
25
- description: " The use library solves the multi-mixin problem by allowing you to\n selectively mixin specific methods from a module rather than mixing\n in all of them. In addition, you can alias methods on the fly as they\n are mixed in, effectively allowing you to change the name of the mixin\n method.\n"
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
+ description: |2
42
+ The use library solves the multi-mixin problem by allowing you to
43
+ selectively mixin specific methods from a module rather than mixing
44
+ in all of them. In addition, you can alias methods on the fly as they
45
+ are mixed in, effectively allowing you to change the name of the mixin
46
+ method.
26
47
  email: djberg96@gmail.com
27
48
  executables: []
28
-
29
49
  extensions: []
30
-
31
- extra_rdoc_files:
50
+ extra_rdoc_files:
32
51
  - MANIFEST
33
52
  - README
34
53
  - CHANGES
35
- files:
54
+ files:
55
+ - CHANGES
56
+ - MANIFEST
57
+ - README
58
+ - Rakefile
36
59
  - examples/example_use.rb
37
60
  - lib/use.rb
38
61
  - test/sample_data.rb
39
62
  - test/test_use.rb
40
- - MANIFEST
41
- - README
42
- - CHANGES
43
- has_rdoc: true
44
- homepage: http://www.rubyforge.org/projects/shards
45
- licenses:
63
+ - use.gemspec
64
+ homepage: https://github.com/djberg96/use
65
+ licenses:
46
66
  - Artistic 2.0
67
+ metadata: {}
47
68
  post_install_message:
48
69
  rdoc_options: []
49
-
50
- require_paths:
70
+ require_paths:
51
71
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
53
- requirements:
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
54
74
  - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
57
- version:
58
- required_rubygems_version: !ruby/object:Gem::Requirement
59
- requirements:
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
60
79
  - - ">="
61
- - !ruby/object:Gem::Version
62
- version: "0"
63
- version:
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
64
82
  requirements: []
65
-
66
- rubyforge_project: shards
67
- rubygems_version: 1.3.5
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.2
68
85
  signing_key:
69
- specification_version: 3
86
+ specification_version: 4
70
87
  summary: Selectively mixin methods from a given module
71
- test_files:
88
+ test_files:
72
89
  - test/test_use.rb