class_inheritable_attributes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/class_inheritable_attributes.gemspec +17 -0
- data/lib/class_inheritable_attributes/version.rb +3 -0
- data/lib/class_inheritable_attributes.rb +180 -0
- data/test/abstract_unit.rb +64 -0
- data/test/class_inheritable_attributes_test.rb +230 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Burke Libbey
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ClassInheritableAttributes
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'class_inheritable_attributes'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install class_inheritable_attributes
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/class_inheritable_attributes/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Burke Libbey"]
|
6
|
+
gem.email = ["burke@burkelibbey.org"]
|
7
|
+
gem.description = %q{Forward-port of deprectaed ClassInheritableAttributes functionality removed in rails 3.2}
|
8
|
+
gem.summary = %q{Forward-port of deprectaed ClassInheritableAttributes functionality removed in rails 3.2.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "class_inheritable_attributes"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ClassInheritableAttributes::VERSION
|
17
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require "class_inheritable_attributes/version"
|
2
|
+
|
3
|
+
require 'active_support/core_ext/object/duplicable'
|
4
|
+
require 'active_support/core_ext/array/extract_options'
|
5
|
+
require 'active_support/deprecation'
|
6
|
+
|
7
|
+
# Retained for backward compatibility. Methods are now included in Class.
|
8
|
+
module ClassInheritableAttributes # :nodoc:
|
9
|
+
DEPRECATION_WARNING_MESSAGE = "class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first"
|
10
|
+
end
|
11
|
+
|
12
|
+
# It is recommended to use <tt>class_attribute</tt> over methods defined in this file. Please
|
13
|
+
# refer to documentation for <tt>class_attribute</tt> for more information. Officially it is not
|
14
|
+
# deprecated but <tt>class_attribute</tt> is faster.
|
15
|
+
#
|
16
|
+
# Allows attributes to be shared within an inheritance hierarchy. Each descendant gets a copy of
|
17
|
+
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
|
18
|
+
# to, for example, an array without those additions being shared with either their parent, siblings, or
|
19
|
+
# children. This is unlike the regular class-level attributes that are shared across the entire hierarchy.
|
20
|
+
#
|
21
|
+
# The copies of inheritable parent attributes are added to subclasses when they are created, via the
|
22
|
+
# +inherited+ hook.
|
23
|
+
#
|
24
|
+
# class Person
|
25
|
+
# class_inheritable_accessor :hair_colors
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# Person.hair_colors = [:brown, :black, :blonde, :red]
|
29
|
+
# Person.hair_colors # => [:brown, :black, :blonde, :red]
|
30
|
+
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
|
31
|
+
#
|
32
|
+
# To opt out of the instance writer method, pass :instance_writer => false.
|
33
|
+
# To opt out of the instance reader method, pass :instance_reader => false.
|
34
|
+
#
|
35
|
+
# class Person
|
36
|
+
# class_inheritable_accessor :hair_colors :instance_writer => false, :instance_reader => false
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# Person.new.hair_colors = [:brown] # => NoMethodError
|
40
|
+
# Person.new.hair_colors # => NoMethodError
|
41
|
+
class Class # :nodoc:
|
42
|
+
def class_inheritable_reader(*syms)
|
43
|
+
ActiveSupport::Deprecation.warn ClassInheritableAttributes::DEPRECATION_WARNING_MESSAGE
|
44
|
+
options = syms.extract_options!
|
45
|
+
syms.each do |sym|
|
46
|
+
next if sym.is_a?(Hash)
|
47
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
48
|
+
def self.#{sym} # def self.after_add
|
49
|
+
read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add)
|
50
|
+
end # end
|
51
|
+
#
|
52
|
+
#{" #
|
53
|
+
def #{sym} # def after_add
|
54
|
+
self.class.#{sym} # self.class.after_add
|
55
|
+
end # end
|
56
|
+
" unless options[:instance_reader] == false } # # the reader above is generated unless options[:instance_reader] == false
|
57
|
+
EOS
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def class_inheritable_writer(*syms)
|
62
|
+
ActiveSupport::Deprecation.warn ClassInheritableAttributes::DEPRECATION_WARNING_MESSAGE
|
63
|
+
options = syms.extract_options!
|
64
|
+
syms.each do |sym|
|
65
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
66
|
+
def self.#{sym}=(obj) # def self.color=(obj)
|
67
|
+
write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:color, obj)
|
68
|
+
end # end
|
69
|
+
#
|
70
|
+
#{" #
|
71
|
+
def #{sym}=(obj) # def color=(obj)
|
72
|
+
self.class.#{sym} = obj # self.class.color = obj
|
73
|
+
end # end
|
74
|
+
" unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
|
75
|
+
EOS
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def class_inheritable_array_writer(*syms)
|
80
|
+
ActiveSupport::Deprecation.warn ClassInheritableAttributes::DEPRECATION_WARNING_MESSAGE
|
81
|
+
options = syms.extract_options!
|
82
|
+
syms.each do |sym|
|
83
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
84
|
+
def self.#{sym}=(obj) # def self.levels=(obj)
|
85
|
+
write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:levels, obj)
|
86
|
+
end # end
|
87
|
+
#
|
88
|
+
#{" #
|
89
|
+
def #{sym}=(obj) # def levels=(obj)
|
90
|
+
self.class.#{sym} = obj # self.class.levels = obj
|
91
|
+
end # end
|
92
|
+
" unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
|
93
|
+
EOS
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def class_inheritable_hash_writer(*syms)
|
98
|
+
ActiveSupport::Deprecation.warn ClassInheritableAttributes::DEPRECATION_WARNING_MESSAGE
|
99
|
+
options = syms.extract_options!
|
100
|
+
syms.each do |sym|
|
101
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
102
|
+
def self.#{sym}=(obj) # def self.nicknames=(obj)
|
103
|
+
write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:nicknames, obj)
|
104
|
+
end # end
|
105
|
+
#
|
106
|
+
#{" #
|
107
|
+
def #{sym}=(obj) # def nicknames=(obj)
|
108
|
+
self.class.#{sym} = obj # self.class.nicknames = obj
|
109
|
+
end # end
|
110
|
+
" unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
|
111
|
+
EOS
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def class_inheritable_accessor(*syms)
|
116
|
+
class_inheritable_reader(*syms)
|
117
|
+
class_inheritable_writer(*syms)
|
118
|
+
end
|
119
|
+
|
120
|
+
def class_inheritable_array(*syms)
|
121
|
+
class_inheritable_reader(*syms)
|
122
|
+
class_inheritable_array_writer(*syms)
|
123
|
+
end
|
124
|
+
|
125
|
+
def class_inheritable_hash(*syms)
|
126
|
+
class_inheritable_reader(*syms)
|
127
|
+
class_inheritable_hash_writer(*syms)
|
128
|
+
end
|
129
|
+
|
130
|
+
def inheritable_attributes
|
131
|
+
@inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES
|
132
|
+
end
|
133
|
+
|
134
|
+
def write_inheritable_attribute(key, value)
|
135
|
+
if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
|
136
|
+
@inheritable_attributes = {}
|
137
|
+
end
|
138
|
+
inheritable_attributes[key] = value
|
139
|
+
end
|
140
|
+
|
141
|
+
def write_inheritable_array(key, elements)
|
142
|
+
write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
|
143
|
+
write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
|
144
|
+
end
|
145
|
+
|
146
|
+
def write_inheritable_hash(key, hash)
|
147
|
+
write_inheritable_attribute(key, {}) if read_inheritable_attribute(key).nil?
|
148
|
+
write_inheritable_attribute(key, read_inheritable_attribute(key).merge(hash))
|
149
|
+
end
|
150
|
+
|
151
|
+
def read_inheritable_attribute(key)
|
152
|
+
inheritable_attributes[key]
|
153
|
+
end
|
154
|
+
|
155
|
+
def reset_inheritable_attributes
|
156
|
+
ActiveSupport::Deprecation.warn ClassInheritableAttributes::DEPRECATION_WARNING_MESSAGE
|
157
|
+
@inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
# Prevent this constant from being created multiple times
|
162
|
+
EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze
|
163
|
+
|
164
|
+
def inherited_with_inheritable_attributes(child)
|
165
|
+
inherited_without_inheritable_attributes(child) if respond_to?(:inherited_without_inheritable_attributes)
|
166
|
+
|
167
|
+
if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
|
168
|
+
new_inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES
|
169
|
+
else
|
170
|
+
new_inheritable_attributes = Hash[inheritable_attributes.map do |(key, value)|
|
171
|
+
[key, value.duplicable? ? value.dup : value]
|
172
|
+
end]
|
173
|
+
end
|
174
|
+
|
175
|
+
child.instance_variable_set('@inheritable_attributes', new_inheritable_attributes)
|
176
|
+
end
|
177
|
+
|
178
|
+
alias inherited_without_inheritable_attributes inherited
|
179
|
+
alias inherited inherited_with_inheritable_attributes
|
180
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
ORIG_ARGV = ARGV.dup
|
2
|
+
|
3
|
+
begin
|
4
|
+
old, $VERBOSE = $VERBOSE, nil
|
5
|
+
# require File.expand_path('../../../load_paths', __FILE__)
|
6
|
+
ensure
|
7
|
+
$VERBOSE = old
|
8
|
+
end
|
9
|
+
|
10
|
+
lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
|
11
|
+
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
|
12
|
+
|
13
|
+
require 'active_support/core_ext/kernel/reporting'
|
14
|
+
|
15
|
+
require 'active_support/core_ext/string/encoding'
|
16
|
+
if "ruby".encoding_aware?
|
17
|
+
# These are the normal settings that will be set up by Railties
|
18
|
+
# TODO: Have these tests support other combinations of these values
|
19
|
+
silence_warnings do
|
20
|
+
Encoding.default_internal = "UTF-8"
|
21
|
+
Encoding.default_external = "UTF-8"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'test/unit'
|
26
|
+
# require 'empty_bool'
|
27
|
+
|
28
|
+
silence_warnings { require 'mocha' }
|
29
|
+
|
30
|
+
ENV['NO_RELOAD'] = '1'
|
31
|
+
require 'active_support'
|
32
|
+
|
33
|
+
# Include shims until we get off 1.8.6
|
34
|
+
require 'active_support/ruby/shim' if RUBY_VERSION < '1.8.7'
|
35
|
+
|
36
|
+
def uses_memcached(test_name)
|
37
|
+
require 'memcache'
|
38
|
+
begin
|
39
|
+
MemCache.new('localhost:11211').stats
|
40
|
+
yield
|
41
|
+
rescue MemCache::MemCacheError
|
42
|
+
$stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def with_kcode(code)
|
47
|
+
if RUBY_VERSION < '1.9'
|
48
|
+
begin
|
49
|
+
old_kcode, $KCODE = $KCODE, code
|
50
|
+
yield
|
51
|
+
ensure
|
52
|
+
$KCODE = old_kcode
|
53
|
+
end
|
54
|
+
else
|
55
|
+
yield
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Show backtraces for deprecated behavior for quicker cleanup.
|
60
|
+
ActiveSupport::Deprecation.debug = true
|
61
|
+
|
62
|
+
if RUBY_VERSION < '1.9'
|
63
|
+
$KCODE = 'UTF8'
|
64
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
require './test/abstract_unit'
|
2
|
+
require './lib/class_inheritable_attributes'
|
3
|
+
|
4
|
+
class ClassInheritableAttributesTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
ActiveSupport::Deprecation.silenced = true
|
7
|
+
@klass = Class.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
ActiveSupport::Deprecation.silenced = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_reader_declaration
|
15
|
+
assert_nothing_raised do
|
16
|
+
@klass.class_inheritable_reader :a
|
17
|
+
assert_respond_to @klass, :a
|
18
|
+
assert_respond_to @klass.new, :a
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_writer_declaration
|
23
|
+
assert_nothing_raised do
|
24
|
+
@klass.class_inheritable_writer :a
|
25
|
+
assert_respond_to @klass, :a=
|
26
|
+
assert_respond_to @klass.new, :a=
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_writer_declaration_without_instance_writer
|
31
|
+
assert_nothing_raised do
|
32
|
+
@klass.class_inheritable_writer :a, :instance_writer => false
|
33
|
+
assert_respond_to @klass, :a=
|
34
|
+
assert !@klass.new.respond_to?(:a=)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_accessor_declaration
|
39
|
+
assert_nothing_raised do
|
40
|
+
@klass.class_inheritable_accessor :a
|
41
|
+
assert_respond_to @klass, :a
|
42
|
+
assert_respond_to @klass.new, :a
|
43
|
+
assert_respond_to @klass, :a=
|
44
|
+
assert_respond_to @klass.new, :a=
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_accessor_declaration_without_instance_writer
|
49
|
+
assert_nothing_raised do
|
50
|
+
@klass.class_inheritable_accessor :a, :instance_writer => false
|
51
|
+
assert_respond_to @klass, :a
|
52
|
+
assert_respond_to @klass.new, :a
|
53
|
+
assert_respond_to @klass, :a=
|
54
|
+
assert !@klass.new.respond_to?(:a=)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_array_declaration
|
59
|
+
assert_nothing_raised do
|
60
|
+
@klass.class_inheritable_array :a
|
61
|
+
assert_respond_to @klass, :a
|
62
|
+
assert_respond_to @klass.new, :a
|
63
|
+
assert_respond_to @klass, :a=
|
64
|
+
assert_respond_to @klass.new, :a=
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_array_declaration_without_instance_writer
|
69
|
+
assert_nothing_raised do
|
70
|
+
@klass.class_inheritable_array :a, :instance_writer => false
|
71
|
+
assert_respond_to @klass, :a
|
72
|
+
assert_respond_to @klass.new, :a
|
73
|
+
assert_respond_to @klass, :a=
|
74
|
+
assert !@klass.new.respond_to?(:a=)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_hash_declaration
|
79
|
+
assert_nothing_raised do
|
80
|
+
@klass.class_inheritable_hash :a
|
81
|
+
assert_respond_to @klass, :a
|
82
|
+
assert_respond_to @klass.new, :a
|
83
|
+
assert_respond_to @klass, :a=
|
84
|
+
assert_respond_to @klass.new, :a=
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_hash_declaration_without_instance_writer
|
89
|
+
assert_nothing_raised do
|
90
|
+
@klass.class_inheritable_hash :a, :instance_writer => false
|
91
|
+
assert_respond_to @klass, :a
|
92
|
+
assert_respond_to @klass.new, :a
|
93
|
+
assert_respond_to @klass, :a=
|
94
|
+
assert !@klass.new.respond_to?(:a=)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_reader
|
99
|
+
@klass.class_inheritable_reader :a
|
100
|
+
assert_nil @klass.a
|
101
|
+
assert_nil @klass.new.a
|
102
|
+
|
103
|
+
@klass.send(:write_inheritable_attribute, :a, 'a')
|
104
|
+
|
105
|
+
assert_equal 'a', @klass.a
|
106
|
+
assert_equal 'a', @klass.new.a
|
107
|
+
assert_equal @klass.a, @klass.new.a
|
108
|
+
assert_equal @klass.a.object_id, @klass.new.a.object_id
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_writer
|
112
|
+
@klass.class_inheritable_reader :a
|
113
|
+
@klass.class_inheritable_writer :a
|
114
|
+
|
115
|
+
assert_nil @klass.a
|
116
|
+
assert_nil @klass.new.a
|
117
|
+
|
118
|
+
@klass.a = 'a'
|
119
|
+
assert_equal 'a', @klass.a
|
120
|
+
@klass.new.a = 'A'
|
121
|
+
assert_equal 'A', @klass.a
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_array
|
125
|
+
@klass.class_inheritable_array :a
|
126
|
+
|
127
|
+
assert_nil @klass.a
|
128
|
+
assert_nil @klass.new.a
|
129
|
+
|
130
|
+
@klass.a = %w(a b c)
|
131
|
+
assert_equal %w(a b c), @klass.a
|
132
|
+
assert_equal %w(a b c), @klass.new.a
|
133
|
+
|
134
|
+
@klass.new.a = %w(A B C)
|
135
|
+
assert_equal %w(a b c A B C), @klass.a
|
136
|
+
assert_equal %w(a b c A B C), @klass.new.a
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_hash
|
140
|
+
@klass.class_inheritable_hash :a
|
141
|
+
|
142
|
+
assert_nil @klass.a
|
143
|
+
assert_nil @klass.new.a
|
144
|
+
|
145
|
+
@klass.a = { :a => 'a' }
|
146
|
+
assert_equal({ :a => 'a' }, @klass.a)
|
147
|
+
assert_equal({ :a => 'a' }, @klass.new.a)
|
148
|
+
|
149
|
+
@klass.new.a = { :b => 'b' }
|
150
|
+
assert_equal({ :a => 'a', :b => 'b' }, @klass.a)
|
151
|
+
assert_equal({ :a => 'a', :b => 'b' }, @klass.new.a)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_inheritance
|
155
|
+
@klass.class_inheritable_accessor :a
|
156
|
+
@klass.a = 'a'
|
157
|
+
|
158
|
+
@sub = eval("class FlogMe < @klass; end; FlogMe")
|
159
|
+
|
160
|
+
@klass.class_inheritable_accessor :b
|
161
|
+
|
162
|
+
assert_respond_to @sub, :a
|
163
|
+
assert_respond_to @sub, :b
|
164
|
+
assert_equal @klass.a, @sub.a
|
165
|
+
assert_equal @klass.b, @sub.b
|
166
|
+
assert_equal 'a', @sub.a
|
167
|
+
assert_nil @sub.b
|
168
|
+
|
169
|
+
@klass.b = 'b'
|
170
|
+
assert_not_equal @klass.b, @sub.b
|
171
|
+
assert_equal 'b', @klass.b
|
172
|
+
assert_nil @sub.b
|
173
|
+
|
174
|
+
@sub.a = 'A'
|
175
|
+
assert_not_equal @klass.a, @sub.a
|
176
|
+
assert_equal 'a', @klass.a
|
177
|
+
assert_equal 'A', @sub.a
|
178
|
+
|
179
|
+
@sub.b = 'B'
|
180
|
+
assert_not_equal @klass.b, @sub.b
|
181
|
+
assert_equal 'b', @klass.b
|
182
|
+
assert_equal 'B', @sub.b
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_array_inheritance
|
186
|
+
@klass.class_inheritable_accessor :a
|
187
|
+
@klass.a = []
|
188
|
+
|
189
|
+
@sub = eval("class SubbyArray < @klass; end; SubbyArray")
|
190
|
+
|
191
|
+
assert_equal [], @klass.a
|
192
|
+
assert_equal [], @sub.a
|
193
|
+
|
194
|
+
@sub.a << :first
|
195
|
+
|
196
|
+
assert_equal [:first], @sub.a
|
197
|
+
assert_equal [], @klass.a
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_array_inheritance_
|
201
|
+
@klass.class_inheritable_accessor :a
|
202
|
+
@klass.a = {}
|
203
|
+
|
204
|
+
@sub = eval("class SubbyHash < @klass; end; SubbyHash")
|
205
|
+
|
206
|
+
assert_equal Hash.new, @klass.a
|
207
|
+
assert_equal Hash.new, @sub.a
|
208
|
+
|
209
|
+
@sub.a[:first] = :first
|
210
|
+
|
211
|
+
assert_equal 1, @sub.a.keys.size
|
212
|
+
assert_equal 0, @klass.a.keys.size
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_reset_inheritable_attributes
|
216
|
+
@klass.class_inheritable_accessor :a
|
217
|
+
@klass.a = 'a'
|
218
|
+
|
219
|
+
@sub = eval("class Inheriting < @klass; end; Inheriting")
|
220
|
+
|
221
|
+
assert_equal 'a', @klass.a
|
222
|
+
assert_equal 'a', @sub.a
|
223
|
+
|
224
|
+
@klass.reset_inheritable_attributes
|
225
|
+
@sub = eval("class NotInheriting < @klass; end; NotInheriting")
|
226
|
+
|
227
|
+
assert_nil @klass.a
|
228
|
+
assert_nil @sub.a
|
229
|
+
end
|
230
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: class_inheritable_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Burke Libbey
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Forward-port of deprectaed ClassInheritableAttributes functionality removed
|
15
|
+
in rails 3.2
|
16
|
+
email:
|
17
|
+
- burke@burkelibbey.org
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- class_inheritable_attributes.gemspec
|
28
|
+
- lib/class_inheritable_attributes.rb
|
29
|
+
- lib/class_inheritable_attributes/version.rb
|
30
|
+
- test/abstract_unit.rb
|
31
|
+
- test/class_inheritable_attributes_test.rb
|
32
|
+
homepage: ''
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.11
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Forward-port of deprectaed ClassInheritableAttributes functionality removed
|
56
|
+
in rails 3.2.
|
57
|
+
test_files:
|
58
|
+
- test/abstract_unit.rb
|
59
|
+
- test/class_inheritable_attributes_test.rb
|