inheritable_attr 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/inheritable_attr.gemspec +26 -0
- data/lib/inheritable_attr.rb +59 -0
- data/lib/inheritable_attr/version.rb +3 -0
- data/spec/inheritable_attr_spec.rb +48 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NTA2Y2VjOGU2YWRhYjA5ODhhMzhjMDk4ZTgzNGRiMjI1ZmY3YTNhMQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzA3YTY0NjdlOGQwYzM2MDI2Y2ZjMmZkNDUyZDdiOTY5ZDFhMDJmNQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjUxMmNlN2FiYWU5OTk3ZGJiNDZhYTFkNDZhYmE4MGIzYjUyZjJkMzMwYmE1
|
10
|
+
ZjAwODBjNmU4MjFjMzkyOTYyOWY2ZDY2Njc4ZTZjODE1ZGU0ZjIzZjdiYjNm
|
11
|
+
ZjYyODBiYTlmZGU3YzI5OTczNjU4YjY2YWFmZmZkZWQ3Y2IxMmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjIwZWYxMmUwMTRmMDc5NTAxYTc2NDA0ZDMwYzcyZjhmOWQ3NjcxZTFhYzRl
|
14
|
+
ODJkNzZhZWMwODE4YzNlYjc5ZTBlMWM4MDVhZjhkY2NhM2MzNTEyYjUwMTE3
|
15
|
+
NTUwNTI2NzIwOTExNDdmNGY3MGM1MTc5MmM5OGMwN2Y5YTQxYjk=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Daniel Vandersluis
|
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
|
+
# InheritableAttr
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'inheritable_attr'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install inheritable_attr
|
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
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'inheritable_attr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'inheritable_attr'
|
8
|
+
spec.version = InheritableAttr::VERSION
|
9
|
+
spec.authors = ['Daniel Vandersluis']
|
10
|
+
spec.email = ['dvandersluis@selfmgmt.com']
|
11
|
+
spec.description = %q{Creates a class-level attribute that is inherited (but modifiable) by subclasses }
|
12
|
+
spec.summary = %q{Creates class-level inheritable attributes that can also be accessed through the instance}
|
13
|
+
spec.homepage = 'https://github.com/dvandersluis/inheritable_attr'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = %w(lib)
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
|
25
|
+
spec.add_dependency 'activesupport', '>= 3.0.0'
|
26
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'active_support/core_ext/object/duplicable'
|
2
|
+
require 'active_support/core_ext/array/extract_options'
|
3
|
+
require "inheritable_attr/version"
|
4
|
+
|
5
|
+
module InheritableAttr
|
6
|
+
# Creates an inheritable attribute with accessors in the singleton class. Derived classes inherit the
|
7
|
+
# attributes. This is especially helpful with arrays or hashes that are extended in the inheritance
|
8
|
+
# chain. Note that you have to initialize the inheritable attribute.
|
9
|
+
# Based on https://github.com/apotonick/hooks/blob/master/lib/hooks/inheritable_attribute.rb
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# class Cat
|
14
|
+
# inheritable_attr :drinks
|
15
|
+
# self.drinks = ["Becks"]
|
16
|
+
#
|
17
|
+
# class Garfield < Cat
|
18
|
+
# self.drinks << "Fireman's 4"
|
19
|
+
#
|
20
|
+
# and then, later
|
21
|
+
#
|
22
|
+
# Cat.drinks #=> ["Becks"]
|
23
|
+
# Garfield.drinks #=> ["Becks", "Fireman's 4"]
|
24
|
+
def inheritable_attr(*names)
|
25
|
+
raise ArgumentError, 'at least one attribute must be specified' if names.empty?
|
26
|
+
|
27
|
+
options = names.extract_options!
|
28
|
+
|
29
|
+
names.each do |name|
|
30
|
+
instance_eval(<<-EOS, __FILE__, __LINE__)
|
31
|
+
def #{name}=(v)
|
32
|
+
@#{name} = v
|
33
|
+
end
|
34
|
+
|
35
|
+
def #{name}
|
36
|
+
return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
|
37
|
+
initial = value.duplicable? ? value.clone : value
|
38
|
+
defined?(@#{name}) ? @#{name} : @#{name} = initial
|
39
|
+
end
|
40
|
+
EOS
|
41
|
+
|
42
|
+
class_eval(<<-EOS, __FILE__, __LINE__) if options.fetch(:instance_reader, true)
|
43
|
+
def #{name}
|
44
|
+
@#{name} ||= self.class.#{name}.duplicable? ? self.class.#{name}.clone : self.class.#{name}
|
45
|
+
end
|
46
|
+
EOS
|
47
|
+
|
48
|
+
class_eval(<<-EOS, __FILE__, __LINE__) if options.fetch(:instance_writer, true)
|
49
|
+
def #{name}=(v)
|
50
|
+
@#{name} = v
|
51
|
+
end
|
52
|
+
EOS
|
53
|
+
|
54
|
+
send(:"#{name}=", options[:default]) if options.key?(:default)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
Class.send(:include, InheritableAttr)
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'inheritable_attr'
|
2
|
+
|
3
|
+
class Base
|
4
|
+
inheritable_attr :numbers, default: []
|
5
|
+
inheritable_attr :letters
|
6
|
+
end
|
7
|
+
|
8
|
+
class Sub < Base
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
describe InheritableAttr do
|
13
|
+
subject { Base }
|
14
|
+
|
15
|
+
its(:numbers) { should == [] }
|
16
|
+
its(:letters) { should be_nil }
|
17
|
+
its('new.numbers') { should == [] }
|
18
|
+
its('new.letters') { should be_nil }
|
19
|
+
|
20
|
+
its(:numbers) { should_not equal subject.new.numbers }
|
21
|
+
|
22
|
+
context "with a subclass" do
|
23
|
+
before do
|
24
|
+
Base.numbers = [1, 2, 3]
|
25
|
+
Base.letters = %w(a b c)
|
26
|
+
end
|
27
|
+
|
28
|
+
subject { Sub }
|
29
|
+
|
30
|
+
# Objects should be cloned
|
31
|
+
its(:numbers) { should_not equal Base.numbers }
|
32
|
+
its(:letters) { should_not equal Base.letters }
|
33
|
+
|
34
|
+
its(:numbers) { should == [1, 2, 3] }
|
35
|
+
its(:letters) { should == %w(a b c) }
|
36
|
+
|
37
|
+
context "when changing subclass values" do
|
38
|
+
subject { Sub.new }
|
39
|
+
before { subject.numbers << 4 }
|
40
|
+
|
41
|
+
its(:numbers) { should == [1, 2, 3, 4] }
|
42
|
+
|
43
|
+
it "should not affect the superclass" do
|
44
|
+
Base.numbers.should == [1, 2, 3]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inheritable_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Vandersluis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
type: :development
|
15
|
+
prerelease: false
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
type: :development
|
29
|
+
prerelease: false
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
type: :development
|
43
|
+
prerelease: false
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
type: :runtime
|
57
|
+
prerelease: false
|
58
|
+
name: activesupport
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 3.0.0
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
description: ! 'Creates a class-level attribute that is inherited (but modifiable)
|
70
|
+
by subclasses '
|
71
|
+
email:
|
72
|
+
- dvandersluis@selfmgmt.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- inheritable_attr.gemspec
|
83
|
+
- lib/inheritable_attr.rb
|
84
|
+
- lib/inheritable_attr/version.rb
|
85
|
+
- spec/inheritable_attr_spec.rb
|
86
|
+
homepage: https://github.com/dvandersluis/inheritable_attr
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.1.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Creates class-level inheritable attributes that can also be accessed through
|
110
|
+
the instance
|
111
|
+
test_files:
|
112
|
+
- spec/inheritable_attr_spec.rb
|
113
|
+
has_rdoc:
|