inherited-attributes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +104 -0
- data/Rakefile +25 -0
- data/lib/inherited/attributes.rb +37 -0
- data/lib/inherited/attributes/version.rb +5 -0
- data/lib/tasks/inherited/attributes_tasks.rake +4 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: debc03f6def20cff892f28c8dee0e332b4a9021f
|
4
|
+
data.tar.gz: 764ce557abcb939139c0f822eaebb7c653323910
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7090f1d2f5dde67b3cea7bd60f9cec178476b89869550921d65d0ff6585cbb1b82c32e3cda2d474b2be927e56c9ddc60a99f57c76987590146a4f7b73ddeea0
|
7
|
+
data.tar.gz: ac622fe4603b0383e10b3e21f308a3d36b1f889f55b3c82271408a42458990807379751f12ea80c3e40c64840dd00bca1280b059f0d02d6b845ea11f1b19a55f
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Inherited::Attributes
|
2
|
+
|
3
|
+
Extends [ancestry](https://github.com/stefankroes/ancestry) to allow attributes to be inherited from ancestors.
|
4
|
+
|
5
|
+
When you want nodes in your tree to be able to get default values for their
|
6
|
+
attributes from their parent.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
### Tell your model that it has inherited attributes
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class Node < ActiveRecord::Base
|
14
|
+
has_ancestry
|
15
|
+
|
16
|
+
inherited_attribute :value
|
17
|
+
|
18
|
+
# If the root node has a nil location, use this as the default value instead.
|
19
|
+
# Note the special syntax - the default value is evaluated.
|
20
|
+
# strings: '"<the default>"'
|
21
|
+
# booleans: '<true|false>'
|
22
|
+
# integers: '<number>'
|
23
|
+
# enumerations: '"<name of the enum>"'
|
24
|
+
inherited_attribute :location, :default => '"Saint Louis"'
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
### Accessing Inherited Attributes
|
29
|
+
|
30
|
+
```
|
31
|
+
root = Node.create!
|
32
|
+
child = Node.create!(:parent => root, :value => 12, :location => "Boston")
|
33
|
+
grandchild = Node.create!(:parent => child)
|
34
|
+
|
35
|
+
# ------------------------------------------------------------------------------
|
36
|
+
# The value of the node, without ancestry
|
37
|
+
# ------------------------------------------------------------------------------
|
38
|
+
root.value # nil
|
39
|
+
child.value # 12
|
40
|
+
grandchild.value # nil
|
41
|
+
|
42
|
+
root.location #
|
43
|
+
child.location # 'Boston'
|
44
|
+
grandchild.location # nil
|
45
|
+
|
46
|
+
# ------------------------------------------------------------------------------
|
47
|
+
# The value of the node, or the inherited value if its not set
|
48
|
+
# ------------------------------------------------------------------------------
|
49
|
+
root.effective_value # nil
|
50
|
+
child.effective_value # 12
|
51
|
+
grandchild.effective_value # 12 -- inherited from child
|
52
|
+
|
53
|
+
root.effective_location # 'Saint Louis' -- using the default value
|
54
|
+
child.effective_location # 'Boston' -- Using the node's value
|
55
|
+
grandchild.effective_location # 'Boston' -- Inherited from child
|
56
|
+
|
57
|
+
# ------------------------------------------------------------------------------
|
58
|
+
# The inherited value of the node or the default, ignoring the node's value
|
59
|
+
# ------------------------------------------------------------------------------
|
60
|
+
root.inherited_value # nil
|
61
|
+
child.inherited_value # nil
|
62
|
+
grandchild.inherited_value # 12 -- inherited from child
|
63
|
+
|
64
|
+
root.inherited_location # 'Saint Louis' -- using the default value
|
65
|
+
child.inherited_location # 'Saint Louis' -- Inherited from root
|
66
|
+
grandchild.inherited_location # 'Boston' -- Inherited from child
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
## Installation
|
71
|
+
Add this line to your application's Gemfile:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
gem 'inherited-attributes'
|
75
|
+
```
|
76
|
+
|
77
|
+
And then execute:
|
78
|
+
```bash
|
79
|
+
$ bundle
|
80
|
+
```
|
81
|
+
|
82
|
+
Or install it yourself as:
|
83
|
+
```bash
|
84
|
+
$ gem install inherited-attributes
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork the repo.
|
90
|
+
|
91
|
+
2. Run the tests (appraisal rake spec). We only take pull requests with passing tests, and it's great
|
92
|
+
to know that you have a clean slate: `bundle && rake`
|
93
|
+
|
94
|
+
3. Add a test for your change. Only refactoring and documentation changes
|
95
|
+
require no new tests. If you are adding functionality or fixing a bug, we need
|
96
|
+
a test!
|
97
|
+
|
98
|
+
4. Make the test pass.
|
99
|
+
|
100
|
+
5. Push to your fork and submit a pull request.
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'Inherited::Attributes'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
require "bundler/gem_tasks"
|
21
|
+
require "rspec/core/rake_task"
|
22
|
+
|
23
|
+
RSpec::Core::RakeTask.new(:spec)
|
24
|
+
|
25
|
+
task default: :spec
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Inherited
|
2
|
+
module Attributes
|
3
|
+
module ClassMethods
|
4
|
+
def inherited_attribute(*fields_or_method)
|
5
|
+
options = fields_or_method.extract_options!.reverse_merge({:default=>'nil'})
|
6
|
+
fields_or_method.each do |method|
|
7
|
+
eval <<-EOS
|
8
|
+
class_eval do
|
9
|
+
def effective_#{method}
|
10
|
+
if @effective_#{method}.nil?
|
11
|
+
@effective_#{method} = (self.#{method}.blank? && !(self.#{method} == false)) ? inherited_#{method} : self.#{method}
|
12
|
+
end
|
13
|
+
@effective_#{method}
|
14
|
+
end
|
15
|
+
|
16
|
+
def inherited_#{method}
|
17
|
+
if @inherited_#{method}.nil?
|
18
|
+
@inherited_#{method} = ancestors.map(&:#{method}).reject{ |val| val.blank? && !(val == false)}.compact.last
|
19
|
+
if (@inherited_#{method}.blank? && !(@inherited_#{method} == false))
|
20
|
+
@inherited_#{method} = self.class.new(:#{method} => #{options[:default]}).#{method}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@inherited_#{method}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
EOS
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.included(base)
|
32
|
+
base.extend(ClassMethods)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ActiveRecord::Base.send :include, Inherited::Attributes
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inherited-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Naegle
|
8
|
+
- Rob Mathews
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ancestry
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activerecord
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '4.2'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '4.2'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: sqlite3
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: appraisal
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: shoulda
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '3.0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '3.0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: byebug
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: pry
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Extends ancestry to allow attributes to be inherited from ancestors.
|
127
|
+
email:
|
128
|
+
- john.naegle@goodmeasures.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- README.md
|
134
|
+
- Rakefile
|
135
|
+
- lib/inherited/attributes.rb
|
136
|
+
- lib/inherited/attributes/version.rb
|
137
|
+
- lib/tasks/inherited/attributes_tasks.rake
|
138
|
+
homepage: https://github.com/GoodMeasuresLLC/inherited-attributes
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 2.2.0
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.5.1
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Extends ancestry to allow attributes to be inherited from ancestors.
|
162
|
+
test_files: []
|