has_attributes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +2 -0
- data/History.md +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/Rakefile +5 -0
- data/has_attributes.gemspec +24 -0
- data/lib/has_attributes/version.rb +3 -0
- data/lib/has_attributes.rb +50 -0
- data/spec/has_attributes_spec.rb +115 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p392
|
data/Gemfile
ADDED
data/History.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ben Reinhart
|
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,86 @@
|
|
1
|
+
# has_attributes
|
2
|
+
|
3
|
+
has_attributes is a ruby gem which provides a module for extending your classes with simple methods for creating elegant plain-ruby models.
|
4
|
+
|
5
|
+
## API
|
6
|
+
|
7
|
+
has_attributes delivers the following class and instance methods:
|
8
|
+
|
9
|
+
#### .model_attributes() → Set
|
10
|
+
|
11
|
+
An attribute accessor on the class that contains a set of attributes specified with `has_attributes`
|
12
|
+
|
13
|
+
#### .has_attributes(*args) → nil
|
14
|
+
|
15
|
+
Takes any number of symbols or strings and defines attribute accessors for those attributes. It also defines a `model_attributes` method on the class which will return the set of attributes. `has_attributes` will attempt to copy any existing attributes from the parent class unless the last argument is a `Hash` and contains an `inherit` key set to `false`.
|
16
|
+
|
17
|
+
#### #attributes() → Hash
|
18
|
+
|
19
|
+
Returns a hash whose keys are all the attributes declared `has_attributes` and the values are their corresponding values. If the values have not been set, the key will still be present in the hash but with a value of `nil`.
|
20
|
+
|
21
|
+
#### #attributes=(attrs) → Hash
|
22
|
+
|
23
|
+
Takes a hash `attrs` and sets all the attributes declared with `has_attributes` to either a corresponding value in `attrs` or `nil`.
|
24
|
+
|
25
|
+
## Examples
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'has_attributes'
|
29
|
+
|
30
|
+
class Person
|
31
|
+
include HasAttributes
|
32
|
+
has_attributes :first_name, :last_name
|
33
|
+
end
|
34
|
+
|
35
|
+
class President < Person
|
36
|
+
# invoked multiple times
|
37
|
+
has_attributes :party
|
38
|
+
has_attributes :term
|
39
|
+
end
|
40
|
+
|
41
|
+
class Student < Person
|
42
|
+
has_attributes :id, :major, inherit: false
|
43
|
+
end
|
44
|
+
|
45
|
+
Person.model_attributes # #<Set: {:first_name, :last_name}>
|
46
|
+
President.model_attributes # #<Set: {:first_name, :last_name, :party, :term}>
|
47
|
+
Student.model_attributes # #<Set: {:id, :major}>
|
48
|
+
|
49
|
+
person = Person.new
|
50
|
+
person.first_name = "John"
|
51
|
+
person.last_name = "Smith"
|
52
|
+
|
53
|
+
person.first_name # "John"
|
54
|
+
person.last_name # "Smith"
|
55
|
+
|
56
|
+
person.attributes # {:first_name=>"John", :last_name=>"Smith"}
|
57
|
+
|
58
|
+
president = President.new
|
59
|
+
president.first_name = "Barack"
|
60
|
+
president.last_name = "Obama"
|
61
|
+
president.party = "democratic"
|
62
|
+
president.term = "8 years"
|
63
|
+
|
64
|
+
president.attributes # {:first_name=>"Barack", :last_name=>"Obama", :party=>"democratic", :term=>"8 years"}
|
65
|
+
|
66
|
+
president.attributes = {}
|
67
|
+
president.attributes # {}
|
68
|
+
|
69
|
+
president.attributes = {:first_name=>"Barack", :last_name=>"Obama", :party=>"democratic", :term=>"8 years"}
|
70
|
+
president.attributes # {:first_name=>"Barack", :last_name=>"Obama", :party=>"democratic", :term=>"8 years"}
|
71
|
+
|
72
|
+
student = Student.new
|
73
|
+
# Student did not inherit attributes from Person
|
74
|
+
student.first_name = "lilly" # Exception: undefined method "first_name=" ...
|
75
|
+
student.id = 123
|
76
|
+
student.major = "CS"
|
77
|
+
|
78
|
+
student.id # 123
|
79
|
+
student.major # "CS"
|
80
|
+
|
81
|
+
student.attributes # {:id=>123, :major=>"CS"}
|
82
|
+
```
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
[MIT](https://github.com/benjreinhart/has_attributes/blob/master/LICENSE.txt)
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_attributes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "has_attributes"
|
8
|
+
spec.version = HasAttributes::VERSION
|
9
|
+
spec.authors = ["Ben Reinhart"]
|
10
|
+
spec.email = ["benjreinhart@gmail.com"]
|
11
|
+
spec.summary = %q{Better plain-ruby models}
|
12
|
+
spec.description = %q{Extend your classes with simple methods for creating elegant plain-ruby models}
|
13
|
+
spec.homepage = "https://github.com/benjreinhart/has_attributes"
|
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 = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "has_attributes/version"
|
2
|
+
require "set"
|
3
|
+
|
4
|
+
module HasAttributes
|
5
|
+
def self.included(base)
|
6
|
+
base.class.send(:attr_accessor, :model_attributes)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def has_attributes(*attrs)
|
12
|
+
options = attrs.last.instance_of?(Hash) ? attrs.pop : {}
|
13
|
+
inherit_parent_attributes = options[:inherit] != false
|
14
|
+
|
15
|
+
parent_attributes = if superclass.respond_to?(:model_attributes) && superclass.model_attributes.is_a?(Set)
|
16
|
+
superclass.model_attributes.dup
|
17
|
+
else
|
18
|
+
Set.new
|
19
|
+
end
|
20
|
+
|
21
|
+
attrs = (model_attributes || Set.new).merge(attrs.map(&:to_sym))
|
22
|
+
|
23
|
+
if inherit_parent_attributes
|
24
|
+
attrs = parent_attributes.merge(attrs)
|
25
|
+
elsif !parent_attributes.empty?
|
26
|
+
methods_to_remove = parent_attributes.reduce([]) do |memo, getter|
|
27
|
+
setter = (getter.to_s << "=").to_sym
|
28
|
+
memo.push(getter) if public_method_defined?(getter)
|
29
|
+
memo.push(setter) if public_method_defined?(setter)
|
30
|
+
memo
|
31
|
+
end
|
32
|
+
instance_eval {undef_method *methods_to_remove}
|
33
|
+
end
|
34
|
+
|
35
|
+
self.model_attributes = attrs
|
36
|
+
instance_eval {attr_accessor *attrs}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def attributes=(attrs)
|
41
|
+
self.class.model_attributes.each {|attr| send((attr.to_s << "=").to_sym, attrs[attr])}
|
42
|
+
attributes
|
43
|
+
end
|
44
|
+
|
45
|
+
def attributes
|
46
|
+
self.class.model_attributes.reduce({}) do |memo, attr|
|
47
|
+
memo.merge(attr => send(attr))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'has_attributes'
|
2
|
+
|
3
|
+
describe HasAttributes do
|
4
|
+
def subclass_model(model, *attrs)
|
5
|
+
Class.new(model) do
|
6
|
+
include HasAttributes
|
7
|
+
has_attributes *attrs
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_model(*attrs)
|
12
|
+
Class.new do
|
13
|
+
include HasAttributes
|
14
|
+
has_attributes *attrs
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".has_attributes" do
|
19
|
+
context "with a single class" do
|
20
|
+
let(:klass) { create_model :attr1, :attr2 }
|
21
|
+
|
22
|
+
it "adds the attributes to the model_attributes accessor" do
|
23
|
+
expect(klass.model_attributes).to eq(Set.new [:attr1, :attr2])
|
24
|
+
end
|
25
|
+
|
26
|
+
it "defines attributes accessors" do
|
27
|
+
expect(klass.public_method_defined?(:attr1)).to be_true
|
28
|
+
expect(klass.public_method_defined?(:attr1=)).to be_true
|
29
|
+
|
30
|
+
expect(klass.public_method_defined?(:attr2)).to be_true
|
31
|
+
expect(klass.public_method_defined?(:attr2=)).to be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when subclassing" do
|
36
|
+
let(:parent) { create_model :attr1, :attr2 }
|
37
|
+
|
38
|
+
context "when inheriting parent attributes" do
|
39
|
+
let(:child) { subclass_model parent, :attr3 }
|
40
|
+
|
41
|
+
it "adds the attributes of both parent and child to the model_attributes accessor" do
|
42
|
+
expect(child.model_attributes).to eq(Set.new [:attr1, :attr2, :attr3])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "defines new accessors in addition to the parent accessors" do
|
46
|
+
expect(child.public_method_defined?(:attr1)).to be_true
|
47
|
+
expect(child.public_method_defined?(:attr1=)).to be_true
|
48
|
+
|
49
|
+
expect(child.public_method_defined?(:attr2)).to be_true
|
50
|
+
expect(child.public_method_defined?(:attr2=)).to be_true
|
51
|
+
|
52
|
+
expect(child.public_method_defined?(:attr3)).to be_true
|
53
|
+
expect(child.public_method_defined?(:attr3=)).to be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when not inheriting parent attributes" do
|
58
|
+
let(:child) { subclass_model parent, :attr3, inherit: false }
|
59
|
+
|
60
|
+
it "adds attributes of the child only to the model_attributes accessor" do
|
61
|
+
expect(child.model_attributes).to eq(Set.new [:attr3])
|
62
|
+
end
|
63
|
+
|
64
|
+
it "defines new accessors only" do
|
65
|
+
expect(child.public_method_defined?(:attr1)).to be_false
|
66
|
+
expect(child.public_method_defined?(:attr1=)).to be_false
|
67
|
+
|
68
|
+
expect(child.public_method_defined?(:attr2)).to be_false
|
69
|
+
expect(child.public_method_defined?(:attr2=)).to be_false
|
70
|
+
|
71
|
+
expect(child.public_method_defined?(:attr3)).to be_true
|
72
|
+
expect(child.public_method_defined?(:attr3=)).to be_true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#attributes" do
|
79
|
+
let(:klass) { create_model :attr1, :attr2 }
|
80
|
+
|
81
|
+
it "sets a hash of attributes on the instance" do
|
82
|
+
instance = klass.new
|
83
|
+
|
84
|
+
instance.attributes = {attr1: true, attr2: false}
|
85
|
+
|
86
|
+
expect(instance.attr1).to be_true
|
87
|
+
expect(instance.attr2).to be_false
|
88
|
+
|
89
|
+
expect(instance.attributes).to eq(attr1: true, attr2: false)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "overrides existing values" do
|
93
|
+
instance = klass.new
|
94
|
+
instance.attr1 = true
|
95
|
+
|
96
|
+
instance.attributes = {attr2: false}
|
97
|
+
|
98
|
+
expect(instance.attr1).to be_nil
|
99
|
+
expect(instance.attr2).to be_false
|
100
|
+
|
101
|
+
expect(instance.attributes).to eq(attr1: nil, attr2: false)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "only sets attributes that were declared with .has_attributes" do
|
105
|
+
instance = klass.new
|
106
|
+
|
107
|
+
instance.attributes = {attr1: true, another_attr: false}
|
108
|
+
|
109
|
+
expect(instance.attr1).to be_true
|
110
|
+
expect(instance.attr2).to be_nil
|
111
|
+
|
112
|
+
expect(instance.attributes).to eq(attr1: true, attr2: nil)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Reinhart
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
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: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
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
|
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
|
+
description: Extend your classes with simple methods for creating elegant plain-ruby
|
63
|
+
models
|
64
|
+
email:
|
65
|
+
- benjreinhart@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- .ruby-version
|
73
|
+
- Gemfile
|
74
|
+
- History.md
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- has_attributes.gemspec
|
79
|
+
- lib/has_attributes.rb
|
80
|
+
- lib/has_attributes/version.rb
|
81
|
+
- spec/has_attributes_spec.rb
|
82
|
+
homepage: https://github.com/benjreinhart/has_attributes
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.23
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Better plain-ruby models
|
107
|
+
test_files:
|
108
|
+
- spec/has_attributes_spec.rb
|