heredity 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.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/heredity.gemspec +28 -0
- data/lib/heredity.rb +61 -0
- data/lib/heredity/version.rb +3 -0
- data/spec/inheritable_class_instance_variables_spec.rb +110 -0
- data/spec/spec_helper.rb +9 -0
- metadata +143 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Brandon Dewitt
|
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
|
+
# Heredity
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'heredity'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install heredity
|
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 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/heredity.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'heredity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "heredity"
|
8
|
+
gem.version = Heredity::VERSION
|
9
|
+
gem.authors = ["Brandon Dewitt"]
|
10
|
+
gem.email = ["brandonsdewitt@gmail.com"]
|
11
|
+
gem.description = %q{provides class inheritable attributes outside of rails and other gem/frameworks}
|
12
|
+
gem.summary = %q{inherit class instance variables}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
##
|
21
|
+
# Development dependencies
|
22
|
+
#
|
23
|
+
gem.add_development_dependency "rake"
|
24
|
+
gem.add_development_dependency "rspec"
|
25
|
+
gem.add_development_dependency "rspec-pride"
|
26
|
+
gem.add_development_dependency "pry-nav"
|
27
|
+
gem.add_development_dependency "simplecov"
|
28
|
+
end
|
data/lib/heredity.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require "heredity/version"
|
3
|
+
|
4
|
+
module Heredity
|
5
|
+
module InheritableClassInstanceVariables
|
6
|
+
def self.included(klass)
|
7
|
+
Thread.exclusive do
|
8
|
+
klass.extend(::Heredity::InheritableClassInstanceVariables::ClassMethods)
|
9
|
+
|
10
|
+
klass.class_eval do
|
11
|
+
@_inheritable_class_instance_variables = [ :_inheritable_class_instance_variables ]
|
12
|
+
|
13
|
+
class << self
|
14
|
+
alias_method :inheritable_attribute, :inheritable_attributes
|
15
|
+
alias_method :class_inheritable_attributes, :inheritable_attributes
|
16
|
+
alias_method :class_inheritable_attribute, :inheritable_attributes
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def inheritable_attributes(*args)
|
24
|
+
Thread.exclusive do
|
25
|
+
args.flatten.compact.uniq.each do |class_instance_variable|
|
26
|
+
unless @_inheritable_class_instance_variables.include?(class_instance_variable)
|
27
|
+
@_inheritable_class_instance_variables << class_instance_variable
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@_inheritable_class_instance_variables.each do |attr_symbol|
|
32
|
+
unless self.respond_to?("#{attr_symbol}")
|
33
|
+
class_eval %Q{
|
34
|
+
class << self; attr_reader :#{attr_symbol}; end
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
unless self.respond_to?("#{attr_symbol}=")
|
39
|
+
class_eval %Q{
|
40
|
+
class << self; attr_writer :#{attr_symbol}; end
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
@_inheritable_class_instance_variables
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def inherited(klass)
|
50
|
+
super # ActiveRecord needs the inherited hook to setup fields
|
51
|
+
|
52
|
+
Thread.exclusive do
|
53
|
+
@_inheritable_class_instance_variables.each do |attribute|
|
54
|
+
attr_sym = :"@#{attribute}"
|
55
|
+
klass.instance_variable_set(attr_sym, self.instance_variable_get(attr_sym))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Heredity::InheritableClassInstanceVariables do
|
4
|
+
|
5
|
+
class TestInheritTop
|
6
|
+
include ::Heredity::InheritableClassInstanceVariables
|
7
|
+
inheritable_attributes :first, :awesomeness, :bestest
|
8
|
+
|
9
|
+
@first = {}
|
10
|
+
@awesomeness = [:this, :that, :the_other]
|
11
|
+
@bestest = "dirt apple (Po-ta-toes!)"
|
12
|
+
end
|
13
|
+
|
14
|
+
class TestSecondTier < TestInheritTop; end
|
15
|
+
class TestThirdTier < TestSecondTier; end
|
16
|
+
|
17
|
+
class TestEditSecondTier < TestInheritTop
|
18
|
+
@first = "Songs are for Singing"
|
19
|
+
@awesomeness = @first * 3
|
20
|
+
@bestest = "SSSSSOOOOOOOOOONNNNNNNNGGGGGGGSSSSSS"
|
21
|
+
end
|
22
|
+
|
23
|
+
class TestEditThirdTier < TestEditSecondTier
|
24
|
+
@first = []
|
25
|
+
@awesomeness = []
|
26
|
+
@bestest = []
|
27
|
+
end
|
28
|
+
|
29
|
+
class TestEditThirdTierWithoutEdit < TestEditSecondTier; end
|
30
|
+
|
31
|
+
# API
|
32
|
+
[TestInheritTop, TestSecondTier, TestThirdTier].each do |test_class|
|
33
|
+
specify { test_class.should respond_to(:inheritable_attributes) }
|
34
|
+
specify { test_class.should respond_to(:inheritable_attribute) }
|
35
|
+
specify { test_class.should respond_to(:class_inheritable_attributes) }
|
36
|
+
specify { test_class.should respond_to(:class_inheritable_attribute) }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when overiding child class instance varialbes" do
|
40
|
+
describe "single tier inheritance" do
|
41
|
+
it "overrides the instance variables with the child defined values" do
|
42
|
+
TestEditSecondTier.first.should eq("Songs are for Singing")
|
43
|
+
TestEditSecondTier.awesomeness.should eq("Songs are for Singing" * 3)
|
44
|
+
TestEditSecondTier.bestest.should eq("SSSSSOOOOOOOOOONNNNNNNNGGGGGGGSSSSSS")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "second tier inheritance" do
|
49
|
+
it "overrides the instance variables with the child defined values" do
|
50
|
+
TestEditThirdTier.first.should eq([])
|
51
|
+
TestEditThirdTier.awesomeness.should eq([])
|
52
|
+
TestEditThirdTier.bestest.should eq([])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "second tier inheritance without changing first override" do
|
57
|
+
it "keeps instance variables the same as first override" do
|
58
|
+
TestEditThirdTierWithoutEdit.first.should eq("Songs are for Singing")
|
59
|
+
TestEditThirdTierWithoutEdit.awesomeness.should eq("Songs are for Singing" * 3)
|
60
|
+
TestEditThirdTierWithoutEdit.bestest.should eq("SSSSSOOOOOOOOOONNNNNNNNGGGGGGGSSSSSS")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "without overriding inherited values" do
|
66
|
+
describe "single tier inheritance" do
|
67
|
+
|
68
|
+
it "creates public attr_readers for inherited attributes" do
|
69
|
+
TestSecondTier.should respond_to(:first)
|
70
|
+
TestSecondTier.should respond_to(:awesomeness)
|
71
|
+
TestSecondTier.should respond_to(:bestest)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "inherits the values of hashes" do
|
75
|
+
TestSecondTier.first.should eq({})
|
76
|
+
end
|
77
|
+
|
78
|
+
it "inherits the values of arrays" do
|
79
|
+
TestSecondTier.awesomeness.should eq([:this, :that, :the_other])
|
80
|
+
end
|
81
|
+
|
82
|
+
it "inherits the values of Objects" do
|
83
|
+
TestSecondTier.bestest.should eq("dirt apple (Po-ta-toes!)")
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "2nd tier inheritance" do
|
89
|
+
|
90
|
+
it "creates public attr_readers for inherited attributes" do
|
91
|
+
TestThirdTier.should respond_to(:first)
|
92
|
+
TestThirdTier.should respond_to(:awesomeness)
|
93
|
+
TestThirdTier.should respond_to(:bestest)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "inherits the values of hashes" do
|
97
|
+
TestThirdTier.first.should eq({})
|
98
|
+
end
|
99
|
+
|
100
|
+
it "inherits the values of arrays" do
|
101
|
+
TestThirdTier.awesomeness.should eq([:this, :that, :the_other])
|
102
|
+
end
|
103
|
+
|
104
|
+
it "inherits the values of Objects" do
|
105
|
+
TestThirdTier.bestest.should eq("dirt apple (Po-ta-toes!)")
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heredity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brandon Dewitt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-pride
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry-nav
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: provides class inheritable attributes outside of rails and other gem/frameworks
|
95
|
+
email:
|
96
|
+
- brandonsdewitt@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- heredity.gemspec
|
107
|
+
- lib/heredity.rb
|
108
|
+
- lib/heredity/version.rb
|
109
|
+
- spec/inheritable_class_instance_variables_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
homepage: ''
|
112
|
+
licenses: []
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
hash: -1738474534293620341
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
hash: -1738474534293620341
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: inherit class instance variables
|
141
|
+
test_files:
|
142
|
+
- spec/inheritable_class_instance_variables_spec.rb
|
143
|
+
- spec/spec_helper.rb
|