attr_required 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,13 @@
1
1
  = attr_required
2
2
 
3
- Provide attr_required and attr_optional
3
+ This gem provide <code>attr_required</code> and <code>attr_optional</code> like <code>attr_accessor</code>.
4
+
5
+ REQUIRED and OPTIONAL are common terminology in RFCs, and used for protocol parameters.
6
+ This gem helps RFC library developers to define which parameters (attributes in Ruby world) are REQUIRED and which are OPTIONAL.
7
+ It might be also helpful for other developers.
8
+
9
+ I've developed this gem to use for rack-oauth2, a Rack-based OAuth 2.0 library.
10
+ http://github.com/nov/rack-oauth2
4
11
 
5
12
  == Installation
6
13
 
@@ -8,10 +15,11 @@ Provide attr_required and attr_optional
8
15
 
9
16
  == Usage
10
17
 
11
- Include <code>AttrRequired</code>, and use <code>attr_required</code> like <code>attr_accessor</code>
12
-
13
18
  # Attributes Definitions
14
19
 
20
+ require 'attr_required'
21
+ require 'attr_optional'
22
+
15
23
  class A
16
24
  include AttrRequired, AttrOptional
17
25
  attr_required :required_a
@@ -27,8 +35,11 @@ Include <code>AttrRequired</code>, and use <code>attr_required</code> like <code
27
35
 
28
36
  A.required_attributes #=> [:required_a]
29
37
  B.required_attributes #=> [:required_a, :required_b]
30
- A.required_attributes #=> [:optional_a]
31
- B.required_attributes #=> [:optional_a, :optional_b]
38
+ A.optional_attributes #=> [:optional_a]
39
+ B.optional_attributes #=> [:optional_a, :optional_b]
40
+
41
+ A.attr_required?(:required_a) #=> true
42
+ B.attr_optional?(:optional_b) #=> true
32
43
 
33
44
  # Instance Methods
34
45
 
@@ -42,6 +53,7 @@ Include <code>AttrRequired</code>, and use <code>attr_required</code> like <code
42
53
 
43
54
  @a.attr_required?(:required_a) #=> true
44
55
  @a.attr_optional?(:optiona_a) #=> true
56
+
45
57
  @a.attr_missing? #=> true
46
58
  @a.attr_missing #=> [:required_a]
47
59
  @a.attr_missing! #=> raise AttrRequired::AttrMissing
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/attr_optional.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  module AttrOptional
2
2
 
3
3
  def self.included(klass)
4
- klass.send :include, Includable
5
- klass.send :extend, Extendable
4
+ klass.send :extend, ClassMethods
6
5
  end
7
6
 
8
- module Extendable
7
+ module ClassMethods
9
8
 
10
9
  def inherited(klass)
11
10
  super
@@ -30,16 +29,12 @@ module AttrOptional
30
29
 
31
30
  end
32
31
 
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
32
+ def optional_attributes
33
+ self.class.optional_attributes
34
+ end
42
35
 
36
+ def attr_optional?(key)
37
+ self.class.attr_optional? key
43
38
  end
44
39
 
45
40
  end
data/lib/attr_required.rb CHANGED
@@ -3,11 +3,10 @@ module AttrRequired
3
3
  class AttrMissing < StandardError; end
4
4
 
5
5
  def self.included(klass)
6
- klass.send :include, Includable
7
- klass.send :extend, Extendable
6
+ klass.send :extend, ClassMethods
8
7
  end
9
8
 
10
- module Extendable
9
+ module ClassMethods
11
10
 
12
11
  def inherited(klass)
13
12
  super
@@ -32,37 +31,33 @@ module AttrRequired
32
31
 
33
32
  end
34
33
 
35
- module Includable
36
-
37
- def required_attributes
38
- self.class.required_attributes
39
- end
34
+ def required_attributes
35
+ self.class.required_attributes
36
+ end
40
37
 
41
- def attr_required?(key)
42
- self.class.attr_required? key
43
- end
38
+ def attr_required?(key)
39
+ self.class.attr_required? key
40
+ end
44
41
 
45
- def attr_missing?
46
- !attr_missing.empty?
47
- end
42
+ def attr_missing?
43
+ !attr_missing.empty?
44
+ end
48
45
 
49
- def attr_missing!
50
- if attr_missing?
51
- raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
52
- end
46
+ def attr_missing!
47
+ if attr_missing?
48
+ raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
53
49
  end
50
+ end
54
51
 
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
52
+ def attr_missing
53
+ required_attributes.select do |key|
54
+ value = send(key)
55
+ if value.respond_to?(:empty?)
56
+ value.empty?
57
+ else
58
+ value.nil?
63
59
  end
64
60
  end
65
-
66
61
  end
67
62
 
68
63
  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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - nov matake
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-04 00:00:00 +09:00
18
+ date: 2010-12-06 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency