inherit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5bc59b9072b4aa6720bd5a5ca9e624f4244d55aa
4
+ data.tar.gz: cd42224ed2714c3a0ea54caecff47cbdaf97a3b2
5
+ SHA512:
6
+ metadata.gz: 589c2d2452de02e532ee54f67d04846910ffe4d171b77ae53078a732769ffabc8fec6ec8d3167f6aa878c9167cfd687ebee6ec6cc94457dea4e5f52469be0cf8
7
+ data.tar.gz: 29f5d84d936ae0ceba5f5151d2a4cb5466f4cb225cee47a159f3f2c6d584754b6156942192c77fadcfb37297468cdd87d45a3459033943e0eb3ad444590ab41a
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2014, Stefan Rusterholz <stefan.rusterholz@gmail.com>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,83 @@
1
+ README
2
+ ======
3
+
4
+
5
+
6
+ Summary
7
+ -------
8
+
9
+ Avoid the anti-pattern of `def self.included(base); base.extend …; end`, without
10
+ getting lost in subclassing.
11
+
12
+
13
+
14
+ Installation
15
+ ------------
16
+
17
+ `gem install inherit`
18
+
19
+
20
+
21
+ Usage
22
+ -----
23
+
24
+ require 'inherit'
25
+
26
+ module Inheritable
27
+ module Constants
28
+ Foo = "Nice constant!"
29
+ end
30
+ module ClassMethods
31
+ def funky
32
+ "Funky class method!"
33
+ end
34
+ end
35
+ module InstanceMethods
36
+ def rad
37
+ "Rad instance method!"
38
+ end
39
+ end
40
+ def self.inherited(subclass)
41
+ puts "#{self} got inherited by #{subclass}"
42
+ end
43
+ def self.extended(object)
44
+ puts "#{self} extended #{object}"
45
+ end
46
+ end
47
+
48
+ class Example
49
+ inherit Inheritable # -> "Inheritable got inherited by Example"
50
+ end
51
+ Example::Foo # => "Nice constant!"
52
+ Example.funky # => "Funky class method!"
53
+ Example.new.rad # => "Rad instance method!"
54
+ a_string = "a string"
55
+ a_string.inherit Inheritable # -> "Inheritable extended a string"
56
+ a_string.singleton_class::Foo # => "Nice constant!"
57
+ a_string.rad # => "Rad instance method!"
58
+
59
+
60
+
61
+ Description
62
+ -----------
63
+
64
+ Avoid the anti-pattern of `def self.included(base); base.extend …; end`, without
65
+ getting lost in subclassing.
66
+
67
+
68
+
69
+ Links
70
+ -----
71
+
72
+ * [Online API Documentation](http://rdoc.info/github/apeiros/inherit/master/frames)
73
+ * [Public Repository](https://github.com/apeiros/inherit)
74
+ * [Bug Reporting](https://github.com/apeiros/inherit/issues)
75
+ * [RubyGems Site](https://rubygems.org/gems/inherit)
76
+
77
+
78
+
79
+ License
80
+ -------
81
+
82
+ You can use this code under the {file:LICENSE.txt BSD-2-Clause License}, free of charge.
83
+ If you need a different license, please ask the author.
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../rake/lib', __FILE__))
2
+ Dir.glob(File.expand_path('../rake/tasks/**/*.{rake,task,rb}', __FILE__)) do |task_file|
3
+ begin
4
+ import task_file
5
+ rescue LoadError => e
6
+ warn "Failed to load task file #{task_file}"
7
+ warn " #{e.class} #{e.message}"
8
+ warn " #{e.backtrace.first}"
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "inherit"
5
+ s.version = "0.0.1"
6
+ s.authors = "Stefan Rusterholz"
7
+ s.email = "stefan.rusterholz@gmail.com"
8
+ s.homepage = "https://github.com/apeiros/inherit"
9
+ s.license = 'BSD 2-Clause'
10
+
11
+ s.description = <<-DESCRIPTION.gsub(/^ /, '').chomp
12
+ Avoid the anti-pattern of `def self.included(base); base.extend …; end`, without getting lost in subclassing.
13
+ DESCRIPTION
14
+ s.summary = <<-SUMMARY.gsub(/^ /, '').chomp
15
+ Avoid the anti-pattern of `def self.included(base); base.extend …; end`, without getting lost in subclassing.
16
+ SUMMARY
17
+
18
+ s.files =
19
+ Dir['bin/**/*'] +
20
+ Dir['lib/**/*'] +
21
+ Dir['rake/**/*'] +
22
+ Dir['test/**/*'] +
23
+ Dir['*.gemspec'] +
24
+ %w[
25
+ LICENSE.txt
26
+ Rakefile
27
+ README.markdown
28
+ ]
29
+
30
+ if File.directory?('bin') then
31
+ s.executables = Dir.chdir('bin') { Dir.glob('**/*').select { |f| File.executable?(f) } }
32
+ end
33
+
34
+ s.add_development_dependency 'minitest', '>= 5.3.0'
35
+
36
+ s.required_ruby_version = ">= 1.9.2"
37
+ s.rubygems_version = "1.3.1"
38
+ s.specification_version = 3
39
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1")
40
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ require 'inherit/version'
4
+
5
+ # The `inherit` gem lets you avoid the anti-pattern of
6
+ # `def self.included(base); base.extend …; end`, without getting lost in
7
+ # subclassing.
8
+ #
9
+ # @see Module#inherit
10
+ # @see Object#inherit
11
+ #
12
+ # @example Usage
13
+ # require 'inherit'
14
+ #
15
+ # module Inheritable
16
+ # module Constants
17
+ # Foo = "Nice constant!"
18
+ # end
19
+ # module ClassMethods
20
+ # def funky
21
+ # "Funky class method!"
22
+ # end
23
+ # end
24
+ # module InstanceMethods
25
+ # def rad
26
+ # "Rad instance method!"
27
+ # end
28
+ # end
29
+ # def self.inherited(subclass)
30
+ # puts "#{self} got inherited by #{subclass}"
31
+ # end
32
+ # def self.extended(object)
33
+ # puts "#{self} extended #{object}"
34
+ # end
35
+ # end
36
+ #
37
+ # class Example
38
+ # inherit Inheritable # -> "Inheritable got inherited by Example"
39
+ # end
40
+ # Example::Foo # => "Nice constant!"
41
+ # Example.funky # => "Funky class method!"
42
+ # Example.new.rad # => "Rad instance method!"
43
+ # a_string = "a string"
44
+ # a_string.inherit Inheritable # -> "Inheritable extended a string"
45
+ # a_string.singleton_class::Foo # => "Nice constant!"
46
+ # a_string.rad # => "Rad instance method!"
47
+ module Inherit
48
+ end
49
+
50
+ class Module
51
+
52
+ # Add constants, class methods and instance methods to a module/class.
53
+ #
54
+ # Adds the following things (if present) from `ancestor` to the module/class:
55
+ #
56
+ # * includes ancestor::Constants
57
+ # * extends ancestor::ClassMethods
58
+ # * includes ancestor::InstanceMethods
59
+ #
60
+ # Additionally it triggers `ancestor.inherited` with the module/class as argument.
61
+ #
62
+ # @param [Module] ancestor
63
+ # The module to add constants, class methods and instance methods from, and
64
+ # to trigger `.inherited` on.
65
+ #
66
+ # @see Inherit Usage example
67
+ #
68
+ def inherit(ancestor)
69
+ include ancestor::Constants if ancestor.const_defined?(:Constants)
70
+ extend ancestor::ClassMethods if ancestor.const_defined?(:ClassMethods)
71
+ include ancestor::InstanceMethods if ancestor.const_defined?(:InstanceMethods)
72
+ ancestor.inherited(self) if ancestor.respond_to?(:inherited)
73
+ end
74
+ end
75
+
76
+ class Object
77
+
78
+ # Add constants and instance methods to an object.
79
+ #
80
+ # Adds the following things (if present) from `ancestor` to the object:
81
+ #
82
+ # * extends ancestor::Constants
83
+ # * extends ancestor::InstanceMethods
84
+ #
85
+ # Additionally it triggers `ancestor.extended` with the object as argument.
86
+ #
87
+ # @param [Module] ancestor
88
+ # The module to add constants and instance methods from, and to trigger
89
+ # `.extended` on.
90
+ #
91
+ # @see Inherit Usage example
92
+ #
93
+ def inherit(ancestor)
94
+ extend ancestor::Constants if ancestor.const_defined?(:Constants)
95
+ extend ancestor::InstanceMethods if ancestor.const_defined?(:InstanceMethods)
96
+ ancestor.extended(self) if ancestor.respond_to?(:extended)
97
+ end
98
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rubygems/version' # newer rubygems use this
5
+ rescue LoadError
6
+ require 'gem/version' # older rubygems use this
7
+ end
8
+
9
+ module Inherit
10
+
11
+ # The version of the inherit gem.
12
+ Version = Gem::Version.new("1.0.0")
13
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+
3
+ require 'stringio'
4
+ require 'minitest/assertions'
5
+ require 'minitest/test'
6
+ require 'inherit'
7
+
8
+ module TestSuite
9
+ attr_accessor :name
10
+ end
11
+
12
+ module Kernel
13
+ def suite(name, &block)
14
+ klass = Class.new(Minitest::Test)
15
+ klass.extend TestSuite
16
+ klass.name = "Suite #{name}"
17
+ klass.class_eval(&block)
18
+
19
+ klass
20
+ end
21
+ module_function :suite
22
+ end
23
+
24
+ class Minitest::Test
25
+ def self.inherited(by)
26
+ by.init
27
+ super
28
+ end
29
+
30
+ def self.init
31
+ @setups = []
32
+ end
33
+
34
+ def self.setup(&block)
35
+ @setups ||= []
36
+ @setups << block
37
+ end
38
+
39
+ class << self
40
+ attr_reader :setups
41
+ end
42
+
43
+ def setup
44
+ self.class.setups.each do |setup|
45
+ instance_eval(&setup)
46
+ end
47
+ super
48
+ end
49
+
50
+ def self.suite(name, &block)
51
+ klass = Class.new(Minitest::Test)
52
+ klass.extend TestSuite
53
+ klass.name = "Suite #{name}"
54
+ klass.name = "#{self.name} #{name}"
55
+ klass.class_eval(&block)
56
+
57
+ klass
58
+ end
59
+
60
+ def self.test(desc, &impl)
61
+ define_method("test_ #{desc}", &impl)
62
+ end
63
+
64
+ def capture_stdout
65
+ captured = StringIO.new
66
+ $stdout = captured
67
+ yield
68
+ captured.string
69
+ ensure
70
+ $stdout = STDOUT
71
+ end
72
+ end
@@ -0,0 +1,21 @@
1
+ # run with `ruby test/runner.rb`
2
+ # if you only want to run a single test-file: `ruby test/runner.rb testfile.rb`
3
+
4
+ if ENV['COVERAGE']
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+ end
8
+
9
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
10
+ $LOAD_PATH << File.expand_path('../../test/lib', __FILE__)
11
+ TEST_DIR = File.expand_path('../../test', __FILE__)
12
+
13
+ require 'minitest/autorun'
14
+ require 'helper'
15
+
16
+ units = ARGV.empty? ? Dir["#{TEST_DIR}/unit/**/*.rb"] : ARGV
17
+
18
+ units.each do |unit|
19
+ p load: unit
20
+ load unit
21
+ end
@@ -0,0 +1,117 @@
1
+ module Inheritable
2
+ module Constants
3
+ Foo = "Nice constant!"
4
+ end
5
+ module ClassMethods
6
+ def funky
7
+ "Funky class method!"
8
+ end
9
+ end
10
+ module InstanceMethods
11
+ def rad
12
+ "Rad instance method!"
13
+ end
14
+ end
15
+ def self.inherited(subclass)
16
+ subclass.inherited_me
17
+ end
18
+ def self.extended(object)
19
+ object.was_extended
20
+ end
21
+ end
22
+
23
+
24
+
25
+ suite "Module#inherit" do
26
+ test 'callback' do
27
+ test_class = Class.new do
28
+ @inherited = false
29
+ def self.inherited_me
30
+ @inherited = true
31
+ end
32
+ def self.did_inherit
33
+ @inherited
34
+ end
35
+ end
36
+
37
+ assert !test_class.did_inherit
38
+ test_class.inherit Inheritable
39
+ assert test_class.did_inherit
40
+ end
41
+
42
+ test 'constants' do
43
+ test_class = Class.new do
44
+ def self.inherited_me
45
+ end
46
+ end
47
+
48
+ assert !test_class.const_defined?(:Foo)
49
+ test_class.inherit Inheritable
50
+ assert test_class.const_defined?(:Foo)
51
+ end
52
+
53
+ test 'class methods' do
54
+ test_class = Class.new do
55
+ def self.inherited_me
56
+ end
57
+ end
58
+
59
+ assert !test_class.respond_to?(:funky)
60
+ test_class.inherit Inheritable
61
+ assert test_class.respond_to?(:funky)
62
+ end
63
+
64
+ test 'instance methods' do
65
+ test_class = Class.new do
66
+ def self.inherited_me
67
+ end
68
+ end
69
+
70
+ assert !test_class.method_defined?(:rad)
71
+ test_class.inherit Inheritable
72
+ assert test_class.method_defined?(:rad)
73
+ end
74
+ end
75
+
76
+ suite "Object#inherit" do
77
+ test 'callback' do
78
+ test_object = Object.new.instance_eval do
79
+ @inherited = false
80
+ def was_extended
81
+ @inherited = true
82
+ end
83
+ def did_inherit
84
+ @inherited
85
+ end
86
+ self
87
+ end
88
+
89
+ assert !test_object.did_inherit
90
+ test_object.inherit Inheritable
91
+ assert test_object.did_inherit
92
+ end
93
+
94
+ test 'constants' do
95
+ test_object = Object.new.instance_eval do
96
+ def was_extended
97
+ end
98
+ self
99
+ end
100
+
101
+ assert !test_object.singleton_class.const_defined?(:Foo)
102
+ test_object.inherit Inheritable
103
+ assert test_object.singleton_class.const_defined?(:Foo)
104
+ end
105
+
106
+ test 'instance methods' do
107
+ test_object = Object.new.instance_eval do
108
+ def was_extended
109
+ end
110
+ self
111
+ end
112
+
113
+ assert !test_object.respond_to?(:rad)
114
+ test_object.inherit Inheritable
115
+ assert test_object.respond_to?(:rad)
116
+ end
117
+ end
@@ -0,0 +1,2 @@
1
+ suite "TableData::Tables" do
2
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inherit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Rusterholz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.3.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.3.0
27
+ description: Avoid the anti-pattern of `def self.included(base); base.extend …; end`,
28
+ without getting lost in subclassing.
29
+ email: stefan.rusterholz@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.markdown
36
+ - Rakefile
37
+ - inherit.gemspec
38
+ - lib/inherit.rb
39
+ - lib/inherit/version.rb
40
+ - test/lib/helper.rb
41
+ - test/runner.rb
42
+ - test/unit/lib/inherit.rb
43
+ - test/unit/lib/inherit/version.rb
44
+ homepage: https://github.com/apeiros/inherit
45
+ licenses:
46
+ - BSD 2-Clause
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 1.9.2
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.1
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Avoid the anti-pattern of `def self.included(base); base.extend …; end`,
68
+ without getting lost in subclassing.
69
+ test_files: []
70
+ has_rdoc: