attr_required 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby
data/Gemfile CHANGED
@@ -1 +1,2 @@
1
+ source 'http://rubygems.org'
1
2
  gemspec
data/Gemfile.lock CHANGED
@@ -1,21 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attr_required (0.0.2)
4
+ attr_required (0.0.3)
5
5
 
6
6
  GEM
7
+ remote: http://rubygems.org/
7
8
  specs:
8
9
  diff-lcs (1.1.2)
9
- rake (0.8.7)
10
+ rake (0.9.0)
10
11
  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)
12
+ rspec (2.6.0)
13
+ rspec-core (~> 2.6.0)
14
+ rspec-expectations (~> 2.6.0)
15
+ rspec-mocks (~> 2.6.0)
16
+ rspec-core (2.6.3)
17
+ rspec-expectations (2.6.0)
17
18
  diff-lcs (~> 1.1.2)
18
- rspec-mocks (2.1.0)
19
+ rspec-mocks (2.6.0)
19
20
 
20
21
  PLATFORMS
21
22
  ruby
data/Rakefile CHANGED
@@ -9,12 +9,4 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
9
9
  spec.rcov_opts = ['-Ilib -Ispec --exclude spec,gems']
10
10
  end
11
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
12
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/attr_optional.rb CHANGED
@@ -14,8 +14,10 @@ module AttrOptional
14
14
  end
15
15
 
16
16
  def attr_optional(*keys)
17
- @optional_attributes ||= []
18
- @optional_attributes += Array(keys)
17
+ if defined?(:undef_required_attributes)
18
+ undef_required_attributes *keys
19
+ end
20
+ optional_attributes.concat(keys)
19
21
  attr_accessor *keys
20
22
  end
21
23
 
@@ -24,7 +26,16 @@ module AttrOptional
24
26
  end
25
27
 
26
28
  def optional_attributes
27
- Array(@optional_attributes)
29
+ @optional_attributes ||= []
30
+ end
31
+
32
+ def undef_optional_attributes(*keys)
33
+ keys.each do |key|
34
+ if attr_optional?(key)
35
+ undef_method key, :"#{key}="
36
+ optional_attributes.delete key
37
+ end
38
+ end
28
39
  end
29
40
 
30
41
  end
data/lib/attr_required.rb CHANGED
@@ -16,17 +16,28 @@ module AttrRequired
16
16
  end
17
17
 
18
18
  def attr_required(*keys)
19
- @required_attributes ||= []
20
- @required_attributes += Array(keys)
19
+ if defined?(:undef_optional_attributes)
20
+ undef_optional_attributes *keys
21
+ end
22
+ required_attributes.concat keys
21
23
  attr_accessor *keys
22
24
  end
23
25
 
24
26
  def attr_required?(key)
25
- required_attributes.include?(key)
27
+ required_attributes.include? key
26
28
  end
27
29
 
28
30
  def required_attributes
29
- Array(@required_attributes)
31
+ @required_attributes ||= []
32
+ end
33
+
34
+ def undef_required_attributes(*keys)
35
+ keys.each do |key|
36
+ if attr_required?(key)
37
+ undef_method key, :"#{key}="
38
+ required_attributes.delete key
39
+ end
40
+ end
30
41
  end
31
42
 
32
43
  end
@@ -1,59 +1,69 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe AttrOptional, '.attr_optional' do
3
+ describe AttrOptional do
4
4
  before do
5
- @a, @b = A.new, B.new
5
+ @a, @b, @c = A.new, B.new, C.new
6
6
  end
7
7
 
8
- it 'should define accessible attributes' do
9
- @a.should respond_to(:attr_optional_a)
10
- @a.should respond_to(:attr_optional_a=)
11
- @b.should respond_to(:attr_optional_b)
12
- @b.should respond_to(:attr_optional_b=)
13
- end
8
+ describe '.attr_optional' do
9
+ it 'should define accessible attributes' do
10
+ @a.should respond_to :attr_optional_a
11
+ @a.should respond_to :attr_optional_a=
12
+ @b.should respond_to :attr_optional_b
13
+ @b.should respond_to :attr_optional_b=
14
+ end
14
15
 
15
- it 'should be inherited' do
16
- @b.should respond_to(:attr_optional_a)
17
- @b.should respond_to(:attr_optional_a=)
18
- end
19
- end
16
+ it 'should be inherited' do
17
+ @b.should respond_to :attr_optional_a
18
+ @b.should respond_to :attr_optional_a=
19
+ end
20
20
 
21
- describe AttrOptional, '.attr_optional?' do
22
- it 'should answer whether the attributes is optional or not' do
23
- A.attr_optional?(:attr_optional_a).should be_true
24
- B.attr_optional?(:attr_optional_a).should be_true
25
- B.attr_optional?(:attr_optional_b).should be_true
26
- B.attr_optional?(:to_s).should be_false
21
+ context 'when already required' do
22
+ it 'should be optional' do
23
+ @c.attr_required?(:attr_required_b).should be_false
24
+ @c.attr_optional?(:attr_required_b).should be_true
25
+ end
26
+ end
27
27
  end
28
- end
29
28
 
30
- describe AttrOptional, '#attr_optional?' do
31
- before do
32
- @a, @b = A.new, B.new
29
+ describe '.attr_optional?' do
30
+ it 'should answer whether the attributes is optional or not' do
31
+ A.attr_optional?(:attr_optional_a).should be_true
32
+ B.attr_optional?(:attr_optional_a).should be_true
33
+ B.attr_optional?(:attr_optional_b).should be_true
34
+ B.attr_optional?(:to_s).should be_false
35
+ end
33
36
  end
34
37
 
35
- it 'should answer whether the attributes is optional or not' do
36
- @a.attr_optional?(:attr_optional_a).should be_true
37
- @b.attr_optional?(:attr_optional_a).should be_true
38
- @b.attr_optional?(:attr_optional_b).should be_true
39
- @b.attr_optional?(:to_s).should be_false
38
+ describe '#attr_optional?' do
39
+ it 'should answer whether the attributes is optional or not' do
40
+ @a.attr_optional?(:attr_optional_a).should be_true
41
+ @b.attr_optional?(:attr_optional_a).should be_true
42
+ @b.attr_optional?(:attr_optional_b).should be_true
43
+ @b.attr_optional?(:to_s).should be_false
44
+ end
40
45
  end
41
- end
42
46
 
43
- describe AttrOptional, '.optional_attributes' do
44
- it 'should return all optional attributes keys' do
45
- A.optional_attributes.should == [:attr_optional_a]
46
- B.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
47
+ describe '.optional_attributes' do
48
+ it 'should return all optional attributes keys' do
49
+ A.optional_attributes.should == [:attr_optional_a]
50
+ B.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
51
+ end
47
52
  end
48
- end
49
53
 
50
- describe AttrOptional, '#optional_attributes' do
51
- before do
52
- @a, @b = A.new, B.new
54
+ describe '#optional_attributes' do
55
+ it 'should return optional attributes keys' do
56
+ @a.optional_attributes.should == [:attr_optional_a]
57
+ @b.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
58
+ end
53
59
  end
54
60
 
55
- it 'should return optional attributes keys' do
56
- @a.optional_attributes.should == [:attr_optional_a]
57
- @b.optional_attributes.should == [:attr_optional_a, :attr_optional_b]
61
+ describe '.undef_optional_attributes' do
62
+ it 'should undefine accessors and remove from optional attributes' do
63
+ C.optional_attributes.should_not include :attr_optional_a
64
+ @c.optional_attributes.should_not include :attr_optional_a
65
+ @c.should_not respond_to :attr_optional_a
66
+ @c.should_not respond_to :attr_optional_a=
67
+ end
58
68
  end
59
69
  end
@@ -1,109 +1,107 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe AttrRequired, '.attr_required' do
3
+ describe AttrRequired do
4
4
  before do
5
- @a, @b = A.new, B.new
5
+ @a, @b, @c = A.new, B.new, C.new
6
6
  end
7
7
 
8
- it 'should define accessible attributes' do
9
- @a.should respond_to(:attr_required_a)
10
- @a.should respond_to(:attr_required_a=)
11
- @b.should respond_to(:attr_required_b)
12
- @b.should respond_to(:attr_required_b=)
13
- end
14
-
15
- it 'should be inherited' do
16
- @b.should respond_to(:attr_required_a)
17
- @b.should respond_to(:attr_required_a=)
18
- end
19
- end
20
-
21
- describe AttrRequired, '.attr_required?' do
22
- it 'should answer whether the attributes is required or not' do
23
- A.attr_required?(:attr_required_a).should be_true
24
- B.attr_required?(:attr_required_a).should be_true
25
- B.attr_required?(:attr_required_b).should be_true
26
- B.attr_required?(:to_s).should be_false
27
- end
28
- end
29
-
30
- describe AttrRequired, '#attr_required?' do
31
- before do
32
- @a, @b = A.new, B.new
33
- end
8
+ describe '.attr_required' do
9
+ it 'should define accessible attributes' do
10
+ @a.should respond_to :attr_required_a
11
+ @a.should respond_to :attr_required_a=
12
+ @b.should respond_to :attr_required_b
13
+ @b.should respond_to :attr_required_b=
14
+ end
34
15
 
35
- it 'should answer whether the attributes is required or not' do
36
- @a.attr_required?(:attr_required_a).should be_true
37
- @b.attr_required?(:attr_required_a).should be_true
38
- @b.attr_required?(:attr_required_b).should be_true
39
- @a.attr_required?(:attr_required_b).should be_false
40
- @b.attr_required?(:to_s).should be_false
41
- end
42
- end
16
+ it 'should be inherited' do
17
+ @b.should respond_to :attr_required_a
18
+ @b.should respond_to :attr_required_a=
19
+ end
43
20
 
44
- describe AttrRequired, '#attr_missing?' do
45
- before do
46
- @a, @b = A.new, B.new
21
+ context 'when already optional' do
22
+ it 'should be optional' do
23
+ @c.attr_required?(:attr_optional_b).should be_true
24
+ @c.attr_optional?(:attr_optional_b).should be_false
25
+ end
26
+ end
47
27
  end
48
28
 
49
- it 'should answer whether any attributes are missing' do
50
- @a.attr_missing?.should be_true
51
- @b.attr_missing?.should be_true
52
- @a.attr_required_a = 'attr_required_a'
53
- @b.attr_required_a = 'attr_required_a'
54
- @a.attr_missing?.should be_false
55
- @b.attr_missing?.should be_true
56
- @b.attr_required_b = 'attr_required_b'
57
- @b.attr_missing?.should be_false
29
+ describe '.attr_required?' do
30
+ it 'should answer whether the attributes is required or not' do
31
+ A.attr_required?(:attr_required_a).should be_true
32
+ B.attr_required?(:attr_required_a).should be_true
33
+ B.attr_required?(:attr_required_b).should be_true
34
+ B.attr_required?(:to_s).should be_false
35
+ end
58
36
  end
59
- end
60
37
 
61
- describe AttrRequired, '#attr_missing!' do
62
- before do
63
- @a, @b = A.new, B.new
38
+ describe '#attr_required?' do
39
+ it 'should answer whether the attributes is required or not' do
40
+ @a.attr_required?(:attr_required_a).should be_true
41
+ @b.attr_required?(:attr_required_a).should be_true
42
+ @b.attr_required?(:attr_required_b).should be_true
43
+ @a.attr_required?(:attr_required_b).should be_false
44
+ @b.attr_required?(:to_s).should be_false
45
+ end
64
46
  end
65
47
 
66
- it 'should raise AttrMissing error when any attributes are missing' do
67
- lambda { @a.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
68
- lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
69
- @a.attr_required_a = 'attr_required_a'
70
- @b.attr_required_a = 'attr_required_a'
71
- lambda { @a.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
72
- lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
73
- @b.attr_required_b = 'attr_required_b'
74
- lambda { @b.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
48
+ describe '#attr_missing?' do
49
+ it 'should answer whether any attributes are missing' do
50
+ @a.attr_missing?.should be_true
51
+ @b.attr_missing?.should be_true
52
+ @a.attr_required_a = 'attr_required_a'
53
+ @b.attr_required_a = 'attr_required_a'
54
+ @a.attr_missing?.should be_false
55
+ @b.attr_missing?.should be_true
56
+ @b.attr_required_b = 'attr_required_b'
57
+ @b.attr_missing?.should be_false
58
+ end
75
59
  end
76
- end
77
60
 
78
- describe AttrRequired, '#attr_missing' do
79
- before do
80
- @a, @b = A.new, B.new
61
+ describe '#attr_missing!' do
62
+ it 'should raise AttrMissing error when any attributes are missing' do
63
+ lambda { @a.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
64
+ lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
65
+ @a.attr_required_a = 'attr_required_a'
66
+ @b.attr_required_a = 'attr_required_a'
67
+ lambda { @a.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
68
+ lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
69
+ @b.attr_required_b = 'attr_required_b'
70
+ lambda { @b.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
71
+ end
81
72
  end
82
73
 
83
- it 'should return missing attributes keys' do
84
- @a.attr_missing.should == [:attr_required_a]
85
- @b.attr_missing.should == [:attr_required_a, :attr_required_b]
86
- @a.attr_required_a = 'attr_required_a'
87
- @b.attr_required_a = 'attr_required_a'
88
- @a.attr_missing.should == []
89
- @b.attr_missing.should == [:attr_required_b]
74
+ describe '#attr_missing' do
75
+ it 'should return missing attributes keys' do
76
+ @a.attr_missing.should == [:attr_required_a]
77
+ @b.attr_missing.should == [:attr_required_a, :attr_required_b]
78
+ @a.attr_required_a = 'attr_required_a'
79
+ @b.attr_required_a = 'attr_required_a'
80
+ @a.attr_missing.should == []
81
+ @b.attr_missing.should == [:attr_required_b]
82
+ end
90
83
  end
91
- end
92
84
 
93
- describe AttrRequired, '.required_attributes' do
94
- it 'should return all required attributes keys' do
95
- A.required_attributes.should == [:attr_required_a]
96
- B.required_attributes.should == [:attr_required_a, :attr_required_b]
85
+ describe '.required_attributes' do
86
+ it 'should return all required attributes keys' do
87
+ A.required_attributes.should == [:attr_required_a]
88
+ B.required_attributes.should == [:attr_required_a, :attr_required_b]
89
+ end
97
90
  end
98
- end
99
91
 
100
- describe AttrRequired, '#required_attributes' do
101
- before do
102
- @a, @b = A.new, B.new
92
+ describe '#required_attributes' do
93
+ it 'should return required attributes keys' do
94
+ @a.required_attributes.should == [:attr_required_a]
95
+ @b.required_attributes.should == [:attr_required_a, :attr_required_b]
96
+ end
103
97
  end
104
98
 
105
- it 'should return required attributes keys' do
106
- @a.required_attributes.should == [:attr_required_a]
107
- @b.required_attributes.should == [:attr_required_a, :attr_required_b]
99
+ describe '.undef_required_attributes' do
100
+ it 'should undefine accessors and remove from required attributes' do
101
+ C.required_attributes.should_not include :attr_required_a
102
+ @c.required_attributes.should_not include :attr_required_a
103
+ @c.should_not respond_to :attr_required_a
104
+ @c.should_not respond_to :attr_required_a=
105
+ end
108
106
  end
109
107
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,3 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
1
  require 'attr_required'
5
2
  require 'attr_optional'
6
3
  require 'rspec'
@@ -15,3 +12,10 @@ class B < A
15
12
  attr_required :attr_required_b
16
13
  attr_optional :attr_optional_b
17
14
  end
15
+
16
+ class C < B
17
+ undef_required_attributes :attr_required_a
18
+ undef_optional_attributes :attr_optional_a
19
+ attr_optional :attr_required_b
20
+ attr_required :attr_optional_b
21
+ end
metadata CHANGED
@@ -1,79 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: attr_required
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - nov matake
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-12-06 00:00:00 +09:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70189917584400 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 27
30
- segments:
31
- - 0
32
- - 8
33
- version: "0.8"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.8'
34
22
  type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rcov
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70189917584400
25
+ - !ruby/object:Gem::Dependency
26
+ name: rcov
27
+ requirement: &70189917581600 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 25
45
- segments:
46
- - 0
47
- - 9
48
- version: "0.9"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
49
33
  type: :development
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: rspec
53
34
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70189917581600
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70189917580580 !ruby/object:Gem::Requirement
55
39
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 7
60
- segments:
61
- - 2
62
- version: "2"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '2'
63
44
  type: :development
64
- version_requirements: *id003
45
+ prerelease: false
46
+ version_requirements: *70189917580580
65
47
  description: attr_required and attr_optional
66
48
  email: nov@matake.jp
67
49
  executables: []
68
-
69
50
  extensions: []
70
-
71
- extra_rdoc_files:
51
+ extra_rdoc_files:
72
52
  - LICENSE
73
53
  - README.rdoc
74
- files:
54
+ files:
75
55
  - .gitignore
76
56
  - .rspec
57
+ - .travis.yml
77
58
  - Gemfile
78
59
  - Gemfile.lock
79
60
  - LICENSE
@@ -86,43 +67,32 @@ files:
86
67
  - spec/attr_optional_spec.rb
87
68
  - spec/attr_required_spec.rb
88
69
  - spec/spec_helper.rb
89
- has_rdoc: true
90
70
  homepage: http://github.com/nov/attr_required
91
71
  licenses: []
92
-
93
72
  post_install_message:
94
- rdoc_options:
73
+ rdoc_options:
95
74
  - --charset=UTF-8
96
- require_paths:
75
+ require_paths:
97
76
  - lib
98
- required_ruby_version: !ruby/object:Gem::Requirement
77
+ required_ruby_version: !ruby/object:Gem::Requirement
99
78
  none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
107
- required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
84
  none: false
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- hash: 23
113
- segments:
114
- - 1
115
- - 3
116
- - 6
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
117
88
  version: 1.3.6
118
89
  requirements: []
119
-
120
90
  rubyforge_project:
121
- rubygems_version: 1.3.7
91
+ rubygems_version: 1.8.12
122
92
  signing_key:
123
93
  specification_version: 3
124
94
  summary: attr_required and attr_optional
125
- test_files:
95
+ test_files:
126
96
  - spec/attr_optional_spec.rb
127
97
  - spec/attr_required_spec.rb
128
98
  - spec/spec_helper.rb