attr_required 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attr_required (0.0.1)
4
+ attr_required (0.0.2)
5
5
 
6
6
  GEM
7
7
  specs:
data/README.rdoc CHANGED
@@ -1,15 +1,57 @@
1
1
  = attr_required
2
2
 
3
- Provide attr_required with presence validation
3
+ Provide attr_required and attr_optional
4
4
 
5
5
  == Installation
6
6
 
7
7
  gem install attr_required
8
8
 
9
- == Resources
10
-
11
9
  == Usage
12
10
 
11
+ Include <code>AttrRequired</code>, and use <code>attr_required</code> like <code>attr_accessor</code>
12
+
13
+ # Attributes Definitions
14
+
15
+ class A
16
+ include AttrRequired, AttrOptional
17
+ attr_required :required_a
18
+ attr_optional :optional_a
19
+ end
20
+
21
+ class B < A
22
+ attr_required :required_b
23
+ attr_optional :optional_b
24
+ end
25
+
26
+ # Class Methods
27
+
28
+ A.required_attributes #=> [:required_a]
29
+ B.required_attributes #=> [:required_a, :required_b]
30
+ A.required_attributes #=> [:optional_a]
31
+ B.required_attributes #=> [:optional_a, :optional_b]
32
+
33
+ # Instance Methods
34
+
35
+ @a = A.new
36
+ @b = B.new
37
+
38
+ @a.required_attributes #=> [:required_a]
39
+ @b.required_attributes #=> [:required_a, :required_b]
40
+ @a.optional_attributes #=> [:optional_a]
41
+ @b.optional_attributes #=> [:optional_a, :optional_b]
42
+
43
+ @a.attr_required?(:required_a) #=> true
44
+ @a.attr_optional?(:optiona_a) #=> true
45
+ @a.attr_missing? #=> true
46
+ @a.attr_missing #=> [:required_a]
47
+ @a.attr_missing! #=> raise AttrRequired::AttrMissing
48
+ @a.required_a = "foo"
49
+ @a.attr_missing? #=> false
50
+ @a.attr_missing #=> []
51
+ @a.attr_missing! #=> do nothing
52
+
53
+ Check spec/attr_(required|optional).rb for more details.
54
+
13
55
  == Note on Patches/Pull Requests
14
56
 
15
57
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.version = File.read("VERSION")
4
4
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
5
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}
6
+ s.description = %q{attr_required and attr_optional}
7
+ s.summary = %q{attr_required and attr_optional}
8
8
  s.email = "nov@matake.jp"
9
9
  s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
10
10
  s.rdoc_options = ["--charset=UTF-8"]
@@ -0,0 +1,45 @@
1
+ module AttrOptional
2
+
3
+ def self.included(klass)
4
+ klass.send :include, Includable
5
+ klass.send :extend, Extendable
6
+ end
7
+
8
+ module Extendable
9
+
10
+ def inherited(klass)
11
+ super
12
+ unless optional_attributes.empty?
13
+ klass.attr_optional *optional_attributes
14
+ end
15
+ end
16
+
17
+ def attr_optional(*keys)
18
+ @optional_attributes ||= []
19
+ @optional_attributes += Array(keys)
20
+ attr_accessor *keys
21
+ end
22
+
23
+ def attr_optional?(key)
24
+ optional_attributes.include?(key)
25
+ end
26
+
27
+ def optional_attributes
28
+ Array(@optional_attributes)
29
+ end
30
+
31
+ end
32
+
33
+ module Includable
34
+
35
+ def optional_attributes
36
+ self.class.optional_attributes
37
+ end
38
+
39
+ def attr_optional?(key)
40
+ self.class.attr_optional? key
41
+ end
42
+
43
+ end
44
+
45
+ end
data/lib/attr_required.rb CHANGED
@@ -1,59 +1,68 @@
1
1
  module AttrRequired
2
+
2
3
  class AttrMissing < StandardError; end
3
4
 
4
5
  def self.included(klass)
5
- class << klass
6
+ klass.send :include, Includable
7
+ klass.send :extend, Extendable
8
+ end
6
9
 
7
- def attr_required(*keys)
8
- @required_attributes ||= []
9
- @required_attributes += Array(keys)
10
- attr_accessor *keys
11
- end
10
+ module Extendable
12
11
 
13
- def attr_required?(key)
14
- required_attributes.include?(key)
12
+ def inherited(klass)
13
+ super
14
+ unless required_attributes.empty?
15
+ klass.attr_required *required_attributes
15
16
  end
17
+ end
16
18
 
17
- def required_attributes
18
- Array(@required_attributes)
19
- end
19
+ def attr_required(*keys)
20
+ @required_attributes ||= []
21
+ @required_attributes += Array(keys)
22
+ attr_accessor *keys
23
+ end
20
24
 
21
- def inherited(subclass)
22
- unless required_attributes.empty?
23
- subclass.attr_required *required_attributes
24
- end
25
- end
25
+ def attr_required?(key)
26
+ required_attributes.include?(key)
27
+ end
26
28
 
29
+ def required_attributes
30
+ Array(@required_attributes)
27
31
  end
28
- end
29
32
 
30
- def required_attributes
31
- self.class.required_attributes
32
33
  end
33
34
 
34
- def attr_required?(key)
35
- self.class.attr_required? key
36
- end
35
+ module Includable
37
36
 
38
- def attr_missing?
39
- !attr_missing.empty?
40
- end
37
+ def required_attributes
38
+ self.class.required_attributes
39
+ end
41
40
 
42
- def attr_missing!
43
- if attr_missing?
44
- raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
41
+ def attr_required?(key)
42
+ self.class.attr_required? key
43
+ end
44
+
45
+ def attr_missing?
46
+ !attr_missing.empty?
45
47
  end
46
- end
47
48
 
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?
49
+ def attr_missing!
50
+ if attr_missing?
51
+ raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
55
52
  end
56
53
  end
54
+
55
+ def attr_missing
56
+ required_attributes.select do |key|
57
+ value = send(key)
58
+ if value.respond_to?(:empty?)
59
+ value.empty?
60
+ else
61
+ value.nil?
62
+ end
63
+ end
64
+ end
65
+
57
66
  end
58
67
 
59
68
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe AttrOptional, '.attr_optional' do
4
+ before do
5
+ @a, @b = A.new, B.new
6
+ end
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
14
+
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
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
27
+ end
28
+ end
29
+
30
+ describe AttrOptional, '#attr_optional?' do
31
+ before do
32
+ @a, @b = A.new, B.new
33
+ end
34
+
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
40
+ end
41
+ end
42
+
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
+ end
48
+ end
49
+
50
+ describe AttrOptional, '#optional_attributes' do
51
+ before do
52
+ @a, @b = A.new, B.new
53
+ end
54
+
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
59
+ end
@@ -1,37 +1,28 @@
1
1
  require 'spec_helper.rb'
2
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
3
  describe AttrRequired, '.attr_required' do
13
4
  before do
14
5
  @a, @b = A.new, B.new
15
6
  end
16
7
 
17
8
  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=)
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=)
22
13
  end
23
14
 
24
15
  it 'should be inherited' do
25
- @b.should respond_to(:attr_a)
26
- @b.should respond_to(:attr_a=)
16
+ @b.should respond_to(:attr_required_a)
17
+ @b.should respond_to(:attr_required_a=)
27
18
  end
28
19
  end
29
20
 
30
21
  describe AttrRequired, '.attr_required?' do
31
22
  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
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
35
26
  B.attr_required?(:to_s).should be_false
36
27
  end
37
28
  end
@@ -42,9 +33,10 @@ describe AttrRequired, '#attr_required?' do
42
33
  end
43
34
 
44
35
  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
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
48
40
  @b.attr_required?(:to_s).should be_false
49
41
  end
50
42
  end
@@ -57,11 +49,11 @@ describe AttrRequired, '#attr_missing?' do
57
49
  it 'should answer whether any attributes are missing' do
58
50
  @a.attr_missing?.should be_true
59
51
  @b.attr_missing?.should be_true
60
- @a.attr_a = 'attr_a'
61
- @b.attr_a = 'attr_a'
52
+ @a.attr_required_a = 'attr_required_a'
53
+ @b.attr_required_a = 'attr_required_a'
62
54
  @a.attr_missing?.should be_false
63
55
  @b.attr_missing?.should be_true
64
- @b.attr_b = 'attr_b'
56
+ @b.attr_required_b = 'attr_required_b'
65
57
  @b.attr_missing?.should be_false
66
58
  end
67
59
  end
@@ -74,44 +66,44 @@ describe AttrRequired, '#attr_missing!' do
74
66
  it 'should raise AttrMissing error when any attributes are missing' do
75
67
  lambda { @a.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
76
68
  lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
77
- @a.attr_a = 'attr_a'
78
- @b.attr_a = 'attr_a'
69
+ @a.attr_required_a = 'attr_required_a'
70
+ @b.attr_required_a = 'attr_required_a'
79
71
  lambda { @a.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
80
72
  lambda { @b.attr_missing! }.should raise_error(AttrRequired::AttrMissing)
81
- @b.attr_b = 'attr_b'
73
+ @b.attr_required_b = 'attr_required_b'
82
74
  lambda { @b.attr_missing! }.should_not raise_error(AttrRequired::AttrMissing)
83
75
  end
84
76
  end
85
77
 
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
78
+ describe AttrRequired, '#attr_missing' do
94
79
  before do
95
80
  @a, @b = A.new, B.new
96
81
  end
97
82
 
98
- it 'should return required attributes keys' do
99
- @a.required_attributes.should == [:attr_a]
100
- @b.required_attributes.should == [:attr_a, :attr_b]
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]
101
90
  end
102
91
  end
103
92
 
104
- describe AttrRequired, '#attr_missing' do
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]
97
+ end
98
+ end
99
+
100
+ describe AttrRequired, '#required_attributes' do
105
101
  before do
106
102
  @a, @b = A.new, B.new
107
103
  end
108
104
 
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]
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]
116
108
  end
117
109
  end
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,16 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'attr_required'
5
- require 'rspec'
5
+ require 'attr_optional'
6
+ require 'rspec'
7
+
8
+ class A
9
+ include AttrRequired, AttrOptional
10
+ attr_required :attr_required_a
11
+ attr_optional :attr_optional_a
12
+ end
13
+
14
+ class B < A
15
+ attr_required :attr_required_b
16
+ attr_optional :attr_optional_b
17
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_required
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - nov matake
@@ -62,7 +62,7 @@ dependencies:
62
62
  version: "2"
63
63
  type: :development
64
64
  version_requirements: *id003
65
- description: Provide attr_required with presence validation
65
+ description: attr_required and attr_optional
66
66
  email: nov@matake.jp
67
67
  executables: []
68
68
 
@@ -81,7 +81,9 @@ files:
81
81
  - Rakefile
82
82
  - VERSION
83
83
  - attr_required.gemspec
84
+ - lib/attr_optional.rb
84
85
  - lib/attr_required.rb
86
+ - spec/attr_optional_spec.rb
85
87
  - spec/attr_required_spec.rb
86
88
  - spec/spec_helper.rb
87
89
  has_rdoc: true
@@ -119,7 +121,8 @@ rubyforge_project:
119
121
  rubygems_version: 1.3.7
120
122
  signing_key:
121
123
  specification_version: 3
122
- summary: Provide attr_required with presence validation
124
+ summary: attr_required and attr_optional
123
125
  test_files:
126
+ - spec/attr_optional_spec.rb
124
127
  - spec/attr_required_spec.rb
125
128
  - spec/spec_helper.rb