attr_required 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/.rspec +2 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +27 -0
- data/LICENSE +20 -0
- data/README.rdoc +25 -0
- data/Rakefile +20 -0
- data/VERSION +1 -0
- data/attr_required.gemspec +19 -0
- data/lib/attr_required.rb +59 -0
- data/spec/attr_required_spec.rb +117 -0
- data/spec/spec_helper.rb +5 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attr_required (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
specs:
|
8
|
+
diff-lcs (1.1.2)
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
rspec (2.1.0)
|
12
|
+
rspec-core (~> 2.1.0)
|
13
|
+
rspec-expectations (~> 2.1.0)
|
14
|
+
rspec-mocks (~> 2.1.0)
|
15
|
+
rspec-core (2.1.0)
|
16
|
+
rspec-expectations (2.1.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.1.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
attr_required!
|
25
|
+
rake (>= 0.8)
|
26
|
+
rcov (>= 0.9)
|
27
|
+
rspec (>= 2)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 nov matake
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
= attr_required
|
2
|
+
|
3
|
+
Provide attr_required with presence validation
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install attr_required
|
8
|
+
|
9
|
+
== Resources
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
== Note on Patches/Pull Requests
|
14
|
+
|
15
|
+
* Fork the project.
|
16
|
+
* Make your feature addition or bug fix.
|
17
|
+
* Add tests for it. This is important so I don't break it in a
|
18
|
+
future version unintentionally.
|
19
|
+
* Commit, do not mess with rakefile, version, or history.
|
20
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
21
|
+
* Send me a pull request. Bonus points for topic branches.
|
22
|
+
|
23
|
+
== Copyright
|
24
|
+
|
25
|
+
Copyright (c) 2010 nov matake. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
8
|
+
spec.rcov = true
|
9
|
+
spec.rcov_opts = ['-Ilib -Ispec --exclude spec,gems']
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
13
|
+
|
14
|
+
require 'rake/rdoctask'
|
15
|
+
Rake::RDocTask.new do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = "attr_required #{File.read('VERSION')}"
|
18
|
+
rdoc.rdoc_files.include('README*')
|
19
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
20
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "attr_required"
|
3
|
+
s.version = File.read("VERSION")
|
4
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
|
5
|
+
s.authors = ["nov matake"]
|
6
|
+
s.description = %q{Provide attr_required with presence validation}
|
7
|
+
s.summary = %q{Provide attr_required with presence validation}
|
8
|
+
s.email = "nov@matake.jp"
|
9
|
+
s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
|
10
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
11
|
+
s.homepage = "http://github.com/nov/attr_required"
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.add_development_dependency "rake", ">= 0.8"
|
17
|
+
s.add_development_dependency "rcov", ">= 0.9"
|
18
|
+
s.add_development_dependency "rspec", ">= 2"
|
19
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module AttrRequired
|
2
|
+
class AttrMissing < StandardError; end
|
3
|
+
|
4
|
+
def self.included(klass)
|
5
|
+
class << klass
|
6
|
+
|
7
|
+
def attr_required(*keys)
|
8
|
+
@required_attributes ||= []
|
9
|
+
@required_attributes += Array(keys)
|
10
|
+
attr_accessor *keys
|
11
|
+
end
|
12
|
+
|
13
|
+
def attr_required?(key)
|
14
|
+
required_attributes.include?(key)
|
15
|
+
end
|
16
|
+
|
17
|
+
def required_attributes
|
18
|
+
Array(@required_attributes)
|
19
|
+
end
|
20
|
+
|
21
|
+
def inherited(subclass)
|
22
|
+
unless required_attributes.empty?
|
23
|
+
subclass.attr_required *required_attributes
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def required_attributes
|
31
|
+
self.class.required_attributes
|
32
|
+
end
|
33
|
+
|
34
|
+
def attr_required?(key)
|
35
|
+
self.class.attr_required? key
|
36
|
+
end
|
37
|
+
|
38
|
+
def attr_missing?
|
39
|
+
!attr_missing.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
def attr_missing!
|
43
|
+
if attr_missing?
|
44
|
+
raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def attr_missing
|
49
|
+
required_attributes.select do |key|
|
50
|
+
value = send(key)
|
51
|
+
if value.respond_to?(:empty?)
|
52
|
+
value.empty?
|
53
|
+
else
|
54
|
+
value.nil?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
class A
|
4
|
+
include AttrRequired
|
5
|
+
attr_required :attr_a
|
6
|
+
end
|
7
|
+
|
8
|
+
class B < A
|
9
|
+
attr_required :attr_b
|
10
|
+
end
|
11
|
+
|
12
|
+
describe AttrRequired, '.attr_required' do
|
13
|
+
before do
|
14
|
+
@a, @b = A.new, B.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should define accessible attributes' do
|
18
|
+
@a.should respond_to(:attr_a)
|
19
|
+
@a.should respond_to(:attr_a=)
|
20
|
+
@b.should respond_to(:attr_b)
|
21
|
+
@b.should respond_to(:attr_b=)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should be inherited' do
|
25
|
+
@b.should respond_to(:attr_a)
|
26
|
+
@b.should respond_to(:attr_a=)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe AttrRequired, '.attr_required?' do
|
31
|
+
it 'should answer whether the attributes is required or not' do
|
32
|
+
A.attr_required?(:attr_a).should be_true
|
33
|
+
B.attr_required?(:attr_a).should be_true
|
34
|
+
B.attr_required?(:attr_b).should be_true
|
35
|
+
B.attr_required?(:to_s).should be_false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe AttrRequired, '#attr_required?' do
|
40
|
+
before do
|
41
|
+
@a, @b = A.new, B.new
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should answer whether the attributes is required or not' do
|
45
|
+
@a.attr_required?(:attr_a).should be_true
|
46
|
+
@b.attr_required?(:attr_a).should be_true
|
47
|
+
@b.attr_required?(:attr_b).should be_true
|
48
|
+
@b.attr_required?(:to_s).should be_false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe AttrRequired, '#attr_missing?' do
|
53
|
+
before do
|
54
|
+
@a, @b = A.new, B.new
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should answer whether any attributes are missing' do
|
58
|
+
@a.attr_missing?.should be_true
|
59
|
+
@b.attr_missing?.should be_true
|
60
|
+
@a.attr_a = 'attr_a'
|
61
|
+
@b.attr_a = 'attr_a'
|
62
|
+
@a.attr_missing?.should be_false
|
63
|
+
@b.attr_missing?.should be_true
|
64
|
+
@b.attr_b = 'attr_b'
|
65
|
+
@b.attr_missing?.should be_false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe AttrRequired, '#attr_missing!' do
|
70
|
+
before do
|
71
|
+
@a, @b = A.new, B.new
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should raise AttrMissing error when any attributes are missing' do
|
75
|
+
lambda { @a.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
|
76
|
+
lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
|
77
|
+
@a.attr_a = 'attr_a'
|
78
|
+
@b.attr_a = 'attr_a'
|
79
|
+
lambda { @a.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
|
80
|
+
lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
|
81
|
+
@b.attr_b = 'attr_b'
|
82
|
+
lambda { @b.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe AttrRequired, '.required_attributes' do
|
87
|
+
it 'should return all required attributes keys' do
|
88
|
+
A.required_attributes.should == [:attr_a]
|
89
|
+
B.required_attributes.should == [:attr_a, :attr_b]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe AttrRequired, '#required_attributes' do
|
94
|
+
before do
|
95
|
+
@a, @b = A.new, B.new
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should return required attributes keys' do
|
99
|
+
@a.required_attributes.should == [:attr_a]
|
100
|
+
@b.required_attributes.should == [:attr_a, :attr_b]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe AttrRequired, '#attr_missing' do
|
105
|
+
before do
|
106
|
+
@a, @b = A.new, B.new
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should return missing attributes keys' do
|
110
|
+
@a.attr_missing.should == [:attr_a]
|
111
|
+
@b.attr_missing.should == [:attr_a, :attr_b]
|
112
|
+
@a.attr_a = 'attr_a'
|
113
|
+
@b.attr_a = 'attr_a'
|
114
|
+
@a.attr_missing.should == []
|
115
|
+
@b.attr_missing.should == [:attr_b]
|
116
|
+
end
|
117
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_required
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- nov matake
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-04 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rake
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 8
|
33
|
+
version: "0.8"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rcov
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 25
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 9
|
48
|
+
version: "0.9"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rspec
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 7
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
version: "2"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: Provide attr_required with presence validation
|
66
|
+
email: nov@matake.jp
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- LICENSE
|
73
|
+
- README.rdoc
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- .rspec
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE
|
80
|
+
- README.rdoc
|
81
|
+
- Rakefile
|
82
|
+
- VERSION
|
83
|
+
- attr_required.gemspec
|
84
|
+
- lib/attr_required.rb
|
85
|
+
- spec/attr_required_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://github.com/nov/attr_required
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options:
|
93
|
+
- --charset=UTF-8
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 23
|
111
|
+
segments:
|
112
|
+
- 1
|
113
|
+
- 3
|
114
|
+
- 6
|
115
|
+
version: 1.3.6
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.3.7
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: Provide attr_required with presence validation
|
123
|
+
test_files:
|
124
|
+
- spec/attr_required_spec.rb
|
125
|
+
- spec/spec_helper.rb
|