heredity 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +30 -2
- data/lib/heredity/inheritable_class_instance_variables.rb +6 -5
- data/lib/heredity/version.rb +1 -1
- data/spec/heredity/inheritable_class_instance_variables_spec.rb +26 -26
- metadata +18 -36
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec31e3521f55721c771972fa759a5af017fb8171
|
4
|
+
data.tar.gz: 82d9c7cea355d67b0286ecee5074a65db1e44c1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59526772bd0b154ea527a272de9feb23fb6c02427494563dca3f21492eb04f559f077b00c567b857deb8fa3c88bfc07c6248c1ccd0f449a78110318f1dc08b99
|
7
|
+
data.tar.gz: edb62863333394c783d70da8dbb4f7012ed2d61796ac0a5866682ea89c95d8903c1acb33d6fe7da2fdb33641b0573bdb9d150b082e5bcf593793303dc506d9e2
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Heredity
|
2
2
|
|
3
|
-
|
3
|
+
Heredity adds on_inherit hooks to Ruby which providing a clean way execute code on inherited classes without the need to override `inherited`. It also adds the ability to specify class instance variables that should be copied to subclasses.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,35 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
#### Inheritance hooks
|
22
|
+
|
23
|
+
To use Heredity's inheritance hooks, simply call `on_herit` with a block:
|
24
|
+
|
25
|
+
```Ruby
|
26
|
+
class Foo
|
27
|
+
on_inherit do
|
28
|
+
puts 'Child of Foo!'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
This is very useful for injecting behavior into subclasses that is dependent on class state (e.g. behavior that relies on the columns of an active record model).
|
34
|
+
|
35
|
+
#### Class instance variables
|
36
|
+
|
37
|
+
Because class instance variables in Ruby are not inherited (and rightfully so), Heredity provides the ability to define specific class instance variables that should be inherited. To define inheritable attributes:
|
38
|
+
|
39
|
+
```Ruby
|
40
|
+
class Foo
|
41
|
+
include Heredity
|
42
|
+
|
43
|
+
# Define inheritable attributes
|
44
|
+
inheritable_attributes :bar
|
45
|
+
|
46
|
+
# Initialize inheritable attributes (so there's something to copy).
|
47
|
+
self.bar = {}
|
48
|
+
end
|
49
|
+
```
|
22
50
|
|
23
51
|
## Contributing
|
24
52
|
|
@@ -1,11 +1,12 @@
|
|
1
|
-
require 'thread'
|
2
|
-
|
3
1
|
module Heredity
|
4
2
|
module InheritableClassInstanceVariables
|
3
|
+
|
4
|
+
MUTEX_FOR_THREAD_EXCLUSIVE = Mutex.new
|
5
|
+
|
5
6
|
def self.included(klass)
|
6
7
|
return if klass.respond_to?(:_inheritable_class_instance_variables)
|
7
8
|
|
8
|
-
|
9
|
+
MUTEX_FOR_THREAD_EXCLUSIVE.synchronize do
|
9
10
|
klass.extend(::Heredity::InheritableClassInstanceVariables::ClassMethods)
|
10
11
|
|
11
12
|
klass.class_eval do
|
@@ -26,7 +27,7 @@ module Heredity
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def inheritable_attributes(*args)
|
29
|
-
|
30
|
+
MUTEX_FOR_THREAD_EXCLUSIVE.synchronize do
|
30
31
|
args.flatten.compact.uniq.each do |class_instance_variable|
|
31
32
|
unless @_inheritable_class_instance_variables.include?(class_instance_variable)
|
32
33
|
@_inheritable_class_instance_variables << class_instance_variable
|
@@ -54,7 +55,7 @@ module Heredity
|
|
54
55
|
def inherited(klass)
|
55
56
|
super # ActiveRecord needs the inherited hook to setup fields
|
56
57
|
|
57
|
-
|
58
|
+
MUTEX_FOR_THREAD_EXCLUSIVE.synchronize do
|
58
59
|
@_inheritable_class_instance_variables.each do |attribute|
|
59
60
|
attr_sym = :"@#{attribute}"
|
60
61
|
klass.instance_variable_set(attr_sym, self.instance_variable_get(attr_sym))
|
data/lib/heredity/version.rb
CHANGED
@@ -30,41 +30,41 @@ describe Heredity::InheritableClassInstanceVariables do
|
|
30
30
|
|
31
31
|
# API
|
32
32
|
[TestInheritTop, TestSecondTier, TestThirdTier].each do |test_class|
|
33
|
-
specify { test_class.
|
34
|
-
specify { test_class.
|
35
|
-
specify { test_class.
|
36
|
-
specify { test_class.
|
33
|
+
specify { expect(test_class).to respond_to(:inheritable_attributes) }
|
34
|
+
specify { expect(test_class).to respond_to(:inheritable_attribute) }
|
35
|
+
specify { expect(test_class).to respond_to(:class_inheritable_attributes) }
|
36
|
+
specify { expect(test_class).to respond_to(:class_inheritable_attribute) }
|
37
37
|
end
|
38
38
|
|
39
39
|
context "when inheritable class instance variables is already included" do
|
40
40
|
it "doesn't reset inheritable attributes" do
|
41
41
|
TestInheritTop.__send__(:include, ::Heredity::InheritableClassInstanceVariables)
|
42
|
-
TestInheritTop.inheritable_attributes.
|
42
|
+
expect(TestInheritTop.inheritable_attributes).to include :first, :awesomeness, :bestest
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
context "when overriding child class instance variables" do
|
47
47
|
describe "single tier inheritance" do
|
48
48
|
it "overrides the instance variables with the child defined values" do
|
49
|
-
TestEditSecondTier.first.
|
50
|
-
TestEditSecondTier.awesomeness.
|
51
|
-
TestEditSecondTier.bestest.
|
49
|
+
expect(TestEditSecondTier.first).to eq("Songs are for Singing")
|
50
|
+
expect(TestEditSecondTier.awesomeness).to eq("Songs are for Singing" * 3)
|
51
|
+
expect(TestEditSecondTier.bestest).to eq("SSSSSOOOOOOOOOONNNNNNNNGGGGGGGSSSSSS")
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
describe "second tier inheritance" do
|
56
56
|
it "overrides the instance variables with the child defined values" do
|
57
|
-
TestEditThirdTier.first.
|
58
|
-
TestEditThirdTier.awesomeness.
|
59
|
-
TestEditThirdTier.bestest.
|
57
|
+
expect(TestEditThirdTier.first).to eq([])
|
58
|
+
expect(TestEditThirdTier.awesomeness).to eq([])
|
59
|
+
expect(TestEditThirdTier.bestest).to eq([])
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
describe "second tier inheritance without changing first override" do
|
64
64
|
it "keeps instance variables the same as first override" do
|
65
|
-
TestEditThirdTierWithoutEdit.first.
|
66
|
-
TestEditThirdTierWithoutEdit.awesomeness.
|
67
|
-
TestEditThirdTierWithoutEdit.bestest.
|
65
|
+
expect(TestEditThirdTierWithoutEdit.first).to eq("Songs are for Singing")
|
66
|
+
expect(TestEditThirdTierWithoutEdit.awesomeness).to eq("Songs are for Singing" * 3)
|
67
|
+
expect(TestEditThirdTierWithoutEdit.bestest).to eq("SSSSSOOOOOOOOOONNNNNNNNGGGGGGGSSSSSS")
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -73,21 +73,21 @@ describe Heredity::InheritableClassInstanceVariables do
|
|
73
73
|
describe "single tier inheritance" do
|
74
74
|
|
75
75
|
it "creates public attr_readers for inherited attributes" do
|
76
|
-
TestSecondTier.
|
77
|
-
TestSecondTier.
|
78
|
-
TestSecondTier.
|
76
|
+
expect(TestSecondTier).to respond_to(:first)
|
77
|
+
expect(TestSecondTier).to respond_to(:awesomeness)
|
78
|
+
expect(TestSecondTier).to respond_to(:bestest)
|
79
79
|
end
|
80
80
|
|
81
81
|
it "inherits the values of hashes" do
|
82
|
-
TestSecondTier.first.
|
82
|
+
expect(TestSecondTier.first).to eq({})
|
83
83
|
end
|
84
84
|
|
85
85
|
it "inherits the values of arrays" do
|
86
|
-
TestSecondTier.awesomeness.
|
86
|
+
expect(TestSecondTier.awesomeness).to eq([:this, :that, :the_other])
|
87
87
|
end
|
88
88
|
|
89
89
|
it "inherits the values of Objects" do
|
90
|
-
TestSecondTier.bestest.
|
90
|
+
expect(TestSecondTier.bestest).to eq("dirt apple (Po-ta-toes!)")
|
91
91
|
end
|
92
92
|
|
93
93
|
end
|
@@ -95,21 +95,21 @@ describe Heredity::InheritableClassInstanceVariables do
|
|
95
95
|
describe "2nd tier inheritance" do
|
96
96
|
|
97
97
|
it "creates public attr_readers for inherited attributes" do
|
98
|
-
TestThirdTier.
|
99
|
-
TestThirdTier.
|
100
|
-
TestThirdTier.
|
98
|
+
expect(TestThirdTier).to respond_to(:first)
|
99
|
+
expect(TestThirdTier).to respond_to(:awesomeness)
|
100
|
+
expect(TestThirdTier).to respond_to(:bestest)
|
101
101
|
end
|
102
102
|
|
103
103
|
it "inherits the values of hashes" do
|
104
|
-
TestThirdTier.first.
|
104
|
+
expect(TestThirdTier.first).to eq({})
|
105
105
|
end
|
106
106
|
|
107
107
|
it "inherits the values of arrays" do
|
108
|
-
TestThirdTier.awesomeness.
|
108
|
+
expect(TestThirdTier.awesomeness).to eq([:this, :that, :the_other])
|
109
109
|
end
|
110
110
|
|
111
111
|
it "inherits the values of Objects" do
|
112
|
-
TestThirdTier.bestest.
|
112
|
+
expect(TestThirdTier.bestest).to eq("dirt apple (Po-ta-toes!)")
|
113
113
|
end
|
114
114
|
|
115
115
|
end
|
metadata
CHANGED
@@ -1,94 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heredity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Brandon Dewitt
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-07-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec-pride
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: pry-nav
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - ">="
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - ">="
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: simplecov
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - ">="
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - ">="
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: provides class inheritable attributes outside of rails and other gem/frameworks
|
@@ -98,7 +87,7 @@ executables: []
|
|
98
87
|
extensions: []
|
99
88
|
extra_rdoc_files: []
|
100
89
|
files:
|
101
|
-
- .gitignore
|
90
|
+
- ".gitignore"
|
102
91
|
- Gemfile
|
103
92
|
- LICENSE.txt
|
104
93
|
- README.md
|
@@ -113,33 +102,26 @@ files:
|
|
113
102
|
- spec/spec_helper.rb
|
114
103
|
homepage: ''
|
115
104
|
licenses: []
|
105
|
+
metadata: {}
|
116
106
|
post_install_message:
|
117
107
|
rdoc_options: []
|
118
108
|
require_paths:
|
119
109
|
- lib
|
120
110
|
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
111
|
requirements:
|
123
|
-
- -
|
112
|
+
- - ">="
|
124
113
|
- !ruby/object:Gem::Version
|
125
114
|
version: '0'
|
126
|
-
segments:
|
127
|
-
- 0
|
128
|
-
hash: 252453511989656056
|
129
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
-
none: false
|
131
116
|
requirements:
|
132
|
-
- -
|
117
|
+
- - ">="
|
133
118
|
- !ruby/object:Gem::Version
|
134
119
|
version: '0'
|
135
|
-
segments:
|
136
|
-
- 0
|
137
|
-
hash: 252453511989656056
|
138
120
|
requirements: []
|
139
121
|
rubyforge_project:
|
140
|
-
rubygems_version:
|
122
|
+
rubygems_version: 2.4.6
|
141
123
|
signing_key:
|
142
|
-
specification_version:
|
124
|
+
specification_version: 4
|
143
125
|
summary: inherit class instance variables
|
144
126
|
test_files:
|
145
127
|
- spec/heredity/inheritable_class_instance_variables_spec.rb
|