subclass_and_replace 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +27 -0
  4. data/lib/subclass_and_replace.rb +67 -0
  5. metadata +76 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a6ac4148f38ab2e0f1c6e16ba82b1b3e666e543
4
+ data.tar.gz: f4d275e7a2e64d062133507156d0927488f1a75d
5
+ SHA512:
6
+ metadata.gz: 295a59fd19499a0ec4a971e4045a3e3d3485a7f95a0805cb7b58bfedb9ac1e2a6eed57f132a6dee2fca7d55a9039cdb1f331a0bf3fee06c559ce2bef9a819dbf
7
+ data.tar.gz: eb745ba6fc7c9c84e69f42424f88104f73730b27b50810f695348c7336eb794ec962df366e93b3753c8cbf3b6e469695a4f298333f4f4d5e1bda9b22f5196ae7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Carlos Reyna
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # Subclass and Replace
2
+
3
+ Create a new class which inherits from a class it will replace.
4
+
5
+ # Description
6
+
7
+ Use this class when you need to replace a class method and still call the superclass method.
8
+
9
+ ## Documentation
10
+
11
+ ### Install
12
+
13
+ gem install subclass_and_replace
14
+
15
+ ### Example
16
+
17
+ Set defaults to Rails cookies without writing custom methods or changing core code.
18
+
19
+ ```ruby
20
+ subclass_and_replace ActionDispatch::Cookies::CookieJar do
21
+
22
+ def handle_options(options)
23
+ options[:httponly] = true if options[:httponly].nil?
24
+ end
25
+
26
+ end
27
+ ```
@@ -0,0 +1,67 @@
1
+ module SubclassAndReplace
2
+
3
+ class << self
4
+
5
+ def replaced_classes
6
+ @replaced_classes ||= {}
7
+ end
8
+
9
+ def subclass_and_replace(klass, &block)
10
+ klass_path = klass.name
11
+
12
+ raise "'#{klass_path}' has already been replaced" unless SubclassAndReplace.replaced_classes[klass_path].nil?
13
+
14
+
15
+ namespace, klass_name = get_namespace_and_klass_name(klass_path)
16
+
17
+ new_klass = Class.new(klass, &block)
18
+
19
+ SubclassAndReplace.replaced_classes[klass_path] = replace_constant(namespace, klass_name, new_klass)
20
+
21
+ new_klass
22
+ end
23
+
24
+ def revert(klass)
25
+ klass_path = klass.name
26
+
27
+ return false if SubclassAndReplace.replaced_classes[klass_path].nil?
28
+
29
+ namespace, klass_name = get_namespace_and_klass_name(klass_path)
30
+
31
+ replace_constant(namespace, klass_name, SubclassAndReplace.replaced_classes.delete(klass_path))
32
+
33
+ true
34
+ end
35
+
36
+ protected
37
+
38
+ def get_namespace_and_klass_name(klass_path)
39
+ path_array = klass_path.split('::')
40
+ klass_name = path_array.pop
41
+
42
+ namespace = path_array.size > 0 ? Kernel.const_get( path_array.join('::') ) : Object
43
+
44
+ [namespace, klass_name]
45
+ end
46
+
47
+ def replace_constant(namespace, klass_name, new_klass)
48
+ old_klass = namespace.send(:remove_const, klass_name)
49
+ namespace.const_set(klass_name, new_klass)
50
+ old_klass
51
+ end
52
+
53
+ end
54
+
55
+ module Methods
56
+
57
+ protected
58
+
59
+ def subclass_and_replace(klass, &block)
60
+ SubclassAndReplace.subclass_and_replace(klass, &block)
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ include SubclassAndReplace::Methods
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subclass_and_replace
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Reyna
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
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: Create new class inherited from a class you wish to replace and the ability
42
+ to revert.
43
+ email:
44
+ - creyna@ingen10.biz
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/subclass_and_replace.rb
50
+ - LICENSE
51
+ - README.md
52
+ homepage: http://github.com/codercr/subclass_and_replace
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.6
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.3
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Create new class inherited from a class you wish to replace and the ability
75
+ to revert.
76
+ test_files: []