enumattr 0.0.3 → 0.0.4
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/Gemfile +0 -4
- data/README.ja.md +144 -0
- data/README.md +55 -97
- data/enumattr.gemspec +2 -0
- data/lib/enumattr/base.rb +11 -22
- data/lib/enumattr/enums.rb +40 -14
- data/lib/enumattr/version.rb +1 -1
- data/spec/enumattr/enums_spec.rb +98 -88
- metadata +15 -3
data/Gemfile
CHANGED
data/README.ja.md
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# Enumattr
|
2
|
+
|
3
|
+
* 定数値とキーワードをマッピングすることができます
|
4
|
+
* クラスはそのマッピングを知っています
|
5
|
+
* インスタンスは自身の属性値から、マッピングされたキーワードを知ることができます
|
6
|
+
|
7
|
+
## 導入
|
8
|
+
|
9
|
+
bundler を利用しているなら、Gemfile に以下の行を加え、
|
10
|
+
|
11
|
+
gem 'enumattr'
|
12
|
+
|
13
|
+
以下のコマンドを実行します。
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
あるいは自身でインストールすることもできます。
|
18
|
+
|
19
|
+
$ gem install enumattr
|
20
|
+
|
21
|
+
## 使い方
|
22
|
+
|
23
|
+
### 基本
|
24
|
+
|
25
|
+
1. `include Enumattr::Base`
|
26
|
+
2. `enumattr :attr_name do ... end` を宣言
|
27
|
+
3. `enum :keyword, value` を `do ... end` ブロックのなかで記述
|
28
|
+
|
29
|
+
例:
|
30
|
+
|
31
|
+
class User
|
32
|
+
include Enumattr::Base
|
33
|
+
|
34
|
+
attr_accessor :status
|
35
|
+
|
36
|
+
enumattr :status do
|
37
|
+
enum :active, 1
|
38
|
+
enum :inactive, 2
|
39
|
+
enum :deleted, 3
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(status)
|
43
|
+
@status = status
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
すると、クラスメソッドとインスタンスメソッドが定義されます。
|
48
|
+
|
49
|
+
* クラスメソッド
|
50
|
+
* `User.status_keys` (`{attr_name}_keys`) は、キーワードの集合を返します
|
51
|
+
* `User.status_values` (`{attr_name}_values`) は、値の集合を返します
|
52
|
+
* `User.status_enums` (`{attr_name}_enums`) は Enum オブジェクトの集合を返します
|
53
|
+
* `User.status_enum(:active)` (`{attr_name}_enum(:keyword)`) は、`:keyword` に対応する Enum オブジェクトを返します
|
54
|
+
* `User.status_value(:active)` (`{attr_name}_value(:keyword)`) は、`:keyword` に対応する値を返します
|
55
|
+
* インスタンスメソッド
|
56
|
+
* `User#status_key` (`{attr_name}_key`) はマッピングに対応するキーワードを返します
|
57
|
+
* `User#status_enum` (`{attr_name}_enum`) はマッピングに対応する Enum オブジェクトを返します
|
58
|
+
* `User#status_value` (`{attr_name}_value`) は値を返します (`alias status_value status` とほぼ同義です)
|
59
|
+
* `User#status_key = :inactive` (`{attr_name}_key=`) というキーワードによるセッターが定義されます
|
60
|
+
* `User#status_active?` (`{attr_name}_{keyword}?`) という、オブジェクトに尋ねるメソッドが定義されます
|
61
|
+
|
62
|
+
_Enum オブジェクト_ (`Enumattr::Enums::Enum`) は、`key` と `value` という属性を持ったオブジェクトです。
|
63
|
+
|
64
|
+
例:
|
65
|
+
|
66
|
+
User.status_keys
|
67
|
+
#=> #<Set: {:active, :inactive, :deleted}>
|
68
|
+
|
69
|
+
User.status_values
|
70
|
+
#=> #<Set: {1, 2, 3}>
|
71
|
+
|
72
|
+
User.status_enums
|
73
|
+
#=> #<Set: {#<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:deleted, @value=3, @extras=[]>}>
|
74
|
+
|
75
|
+
|
76
|
+
enum = User.status_enum(:active)
|
77
|
+
#=> #<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:active, @value=1, @extras=[]>
|
78
|
+
|
79
|
+
# Enum オブジェクトは key と value という属性を持っている
|
80
|
+
enum.key
|
81
|
+
#=> :active
|
82
|
+
|
83
|
+
enum.value
|
84
|
+
#=> 1
|
85
|
+
|
86
|
+
User.status_enum(:dummy)
|
87
|
+
#=> nil
|
88
|
+
|
89
|
+
User.status_value(:active)
|
90
|
+
#=> 1
|
91
|
+
|
92
|
+
User.status_value(:dummy)
|
93
|
+
#=> nil
|
94
|
+
|
95
|
+
|
96
|
+
user = User.new(1)
|
97
|
+
#=> #<User:0x007ff58b050dd8 @status=1>
|
98
|
+
|
99
|
+
user.status
|
100
|
+
#=> 1
|
101
|
+
|
102
|
+
user.status_key
|
103
|
+
#=> :active
|
104
|
+
|
105
|
+
user.status_value
|
106
|
+
#=> 1
|
107
|
+
|
108
|
+
user.status_key = :inactive
|
109
|
+
#=> :inactive
|
110
|
+
|
111
|
+
user.status
|
112
|
+
#=> 2
|
113
|
+
|
114
|
+
user.status_active?
|
115
|
+
#=> false
|
116
|
+
|
117
|
+
user.status_inactive?
|
118
|
+
#=> true
|
119
|
+
|
120
|
+
### オプション
|
121
|
+
|
122
|
+
* `:on`
|
123
|
+
* enumattr に指定している名前と同じメソッドあるいは属性が存在しない場合に、参照すべきメソッドや属性を指定できます
|
124
|
+
* `enumattr :enumattr_name, :on => :existent_attribute do ...`
|
125
|
+
* `:enums`
|
126
|
+
* ハッシュで enum オブジェクトのマッピングを定義できます。ブロックによる enum オブジェクトの指定の代替記法です
|
127
|
+
* `enumattr :enumattr_name, :enums => {:keyword1 => value1, :keyword2 => value2}`
|
128
|
+
* `:extend`
|
129
|
+
* モジュールを指定して enum オブジェクトを拡張することができます
|
130
|
+
* `enumattr :enumattr_name, :extend => Extension do ...`
|
131
|
+
|
132
|
+
## その他の例
|
133
|
+
|
134
|
+
_examples/*.rb_ と _spec/enumattr/*.rb_ を参照してください。
|
135
|
+
|
136
|
+
## ご意見・ご指導
|
137
|
+
|
138
|
+
ご意見やご指導を歓迎しています!
|
139
|
+
|
140
|
+
1. Fork して
|
141
|
+
2. feature branch を作って (`git checkout -b my-new-feature`)
|
142
|
+
3. コミットして (`git commit -am 'Added some feature'`)
|
143
|
+
4. branch を push して (`git push origin my-new-feature`)
|
144
|
+
5. Pull Request してください
|
data/README.md
CHANGED
@@ -20,11 +20,11 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
###
|
23
|
+
### Basics
|
24
24
|
|
25
25
|
1. `include Enumattr::Base`
|
26
|
-
2. declare `enumattr
|
27
|
-
|
26
|
+
2. declare `enumattr :attr_name do ... end`
|
27
|
+
3. `enum :keyword, value` in `do ... end` block
|
28
28
|
|
29
29
|
example:
|
30
30
|
|
@@ -44,112 +44,66 @@ example:
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
then defining class methods and instance methods.
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
def initialize(status)
|
62
|
-
@status = status
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
### defining options
|
49
|
+
* class methods
|
50
|
+
* `User.status_keys` as `{attr_name}_keys` return keyword set
|
51
|
+
* `User.status_values` as `{attr_name}_values` return value set
|
52
|
+
* `User.status_enums` as `{attr_name}_enums` return Enum object set
|
53
|
+
* `User.status_enum(:active)` as `{attr_name}_enum(:keyword)` return an Enum object
|
54
|
+
* `User.status_value(:active)` as `{attr_name}_value(:keyword)` return a value
|
55
|
+
* instance methods
|
56
|
+
* `User#status_key` as `{attr_name}_key` return a keyword correspond to mapping
|
57
|
+
* `User#status_enum` as `{attr_name}_enum` return a Enum object correspond to mapping
|
58
|
+
* `User#status_value` as `{attr_name}_value` return a value (`alias status_value status`)
|
59
|
+
* `User#status_key = :inactive` as `{attr_name}_key=` setter by keyword
|
60
|
+
* `User#status_active?` as `{attr_name}_{keyword}?` query method return true or false
|
67
61
|
|
68
|
-
|
69
|
-
include Enumattr::Base
|
70
|
-
|
71
|
-
attr_accessor :status
|
72
|
-
|
73
|
-
# alt enum attribute_name and :on option with real_attribute_name
|
74
|
-
enumattr :use_status, :on => :status do
|
75
|
-
enum :active, 1
|
76
|
-
enum :inactive, 2
|
77
|
-
enum :deleted, 3
|
78
|
-
end
|
79
|
-
|
80
|
-
def initialize(status)
|
81
|
-
@status = status
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
#### class methods
|
86
|
-
|
87
|
-
* `#{attribute_name}_enums` return enum set
|
88
|
-
* `#{attribute_name}_keys` return key set
|
89
|
-
* `#{attribute_name}_values` return value set
|
62
|
+
_Enum object_ (`Enumattr::Enums::Enum`) has `key` and `value` attributes
|
90
63
|
|
91
64
|
example:
|
92
65
|
|
93
|
-
User.status_enums
|
94
|
-
#=> #<Set: {#<Enumattr::Enums::Enum:0x8a8d13c @key=:active, @value=1>, #<Enumattr::Enums::Enum:0x8a8d100 @key=:inactive, @value=2>, #<Enumattr::Enums::Enum:0x8a8d0ec @key=:deleted, @value=3>}>
|
95
|
-
|
96
66
|
User.status_keys
|
97
67
|
#=> #<Set: {:active, :inactive, :deleted}>
|
98
68
|
|
99
69
|
User.status_values
|
100
70
|
#=> #<Set: {1, 2, 3}>
|
101
71
|
|
102
|
-
|
103
|
-
|
72
|
+
User.status_enums
|
73
|
+
#=> #<Set: {#<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:inactive, @value=2, @extras=[]>, #<Enumattr::Enums::Enum:0x007ff58b220488 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:deleted, @value=3, @extras=[]>}>
|
104
74
|
|
105
|
-
example:
|
106
75
|
|
107
|
-
User.status_enum
|
108
|
-
#=> #<Enumattr::Enums::Enum:
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
User.status_value :active
|
76
|
+
enum = User.status_enum(:active)
|
77
|
+
#=> #<Enumattr::Enums::Enum:0x007ff58b220618 @container=#<Enumattr::Enums:0x007ff58b2207a8>, @key=:active, @value=1, @extras=[]>
|
78
|
+
|
79
|
+
# Enum object has key and value attributes
|
80
|
+
enum.key
|
81
|
+
#=> :active
|
82
|
+
|
83
|
+
enum.value
|
117
84
|
#=> 1
|
118
|
-
|
119
|
-
User.
|
120
|
-
#=> 2
|
121
|
-
|
122
|
-
User.status_value :dummy
|
85
|
+
|
86
|
+
User.status_enum(:dummy)
|
123
87
|
#=> nil
|
124
88
|
|
125
|
-
|
89
|
+
User.status_value(:active)
|
90
|
+
#=> 1
|
126
91
|
|
127
|
-
|
128
|
-
|
129
|
-
* `#{attribute_name}_key` return symbol
|
130
|
-
* `#{attribute_name}_enum` return enum
|
131
|
-
* setter (if writable)
|
132
|
-
* `#{attribute_name}_key=(key)`
|
133
|
-
|
92
|
+
User.status_value(:dummy)
|
93
|
+
#=> nil
|
134
94
|
|
135
|
-
getters example:
|
136
95
|
|
137
96
|
user = User.new(1)
|
138
|
-
#=> #<User:
|
139
|
-
|
97
|
+
#=> #<User:0x007ff58b050dd8 @status=1>
|
98
|
+
|
140
99
|
user.status
|
141
100
|
#=> 1
|
142
|
-
|
143
|
-
user.status_value # alias
|
144
|
-
#=> 1
|
145
|
-
|
101
|
+
|
146
102
|
user.status_key
|
147
103
|
#=> :active
|
148
|
-
|
149
|
-
user.status_enum
|
150
|
-
#=> #<Enumattr::Enums::Enum:0x8de2e68 @key=:active, @value=1>
|
151
104
|
|
152
|
-
|
105
|
+
user.status_value
|
106
|
+
#=> 1
|
153
107
|
|
154
108
|
user.status_key = :inactive
|
155
109
|
#=> :inactive
|
@@ -157,23 +111,27 @@ setter example:
|
|
157
111
|
user.status
|
158
112
|
#=> 2
|
159
113
|
|
160
|
-
|
161
|
-
* Query Method
|
162
|
-
* `#{attribute_name}_#{key}?` return true or false
|
163
|
-
|
164
|
-
example:
|
165
|
-
|
166
114
|
user.status_active?
|
167
115
|
#=> false
|
168
|
-
|
116
|
+
|
169
117
|
user.status_inactive?
|
170
118
|
#=> true
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
119
|
+
|
120
|
+
### Options
|
121
|
+
|
122
|
+
* `:on`
|
123
|
+
* specify existent attribute or method if `enumattr_name` attribute or method doesn't exist
|
124
|
+
* `enumattr :enumattr_name, :on => :existent_attribute do ...`
|
125
|
+
* `:enums`
|
126
|
+
* altenative enum defining leteral by hash instead of block
|
127
|
+
* `enumattr :enumattr_name, :enums => {:keyword1 => value1, :keyword2 => value2}`
|
128
|
+
* `:extend`
|
129
|
+
* enum object extension
|
130
|
+
* `enumattr :enumattr_name, :extend => Extension do ...`
|
131
|
+
|
132
|
+
## More examples
|
133
|
+
|
134
|
+
see: _examples/*.rb_ and _spec/enumattr/*.rb_
|
177
135
|
|
178
136
|
## Contributing
|
179
137
|
|
data/enumattr.gemspec
CHANGED
data/lib/enumattr/base.rb
CHANGED
@@ -9,29 +9,18 @@ module Enumattr
|
|
9
9
|
module ClassMethods
|
10
10
|
private
|
11
11
|
def enumattr(enumattr_name, options = {}, &block)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
else
|
17
|
-
closure = block
|
18
|
-
end
|
19
|
-
|
20
|
-
context = options.merge(:enumattr => enumattr_name, :base => self)
|
21
|
-
|
22
|
-
enumattrs[enumattr_name] = Enums.new(context, &closure)
|
23
|
-
enumattr_bases[enumattr_name] = options[:on] || enumattr_name
|
24
|
-
|
25
|
-
define_enumattr_class_methods enumattr_name
|
26
|
-
define_enumattr_instance_methods enumattr_name
|
12
|
+
enums = Enums.new(enumattr_name, self, options, &block)
|
13
|
+
enumattrs[enumattr_name] = enums
|
14
|
+
define_enumattr_methods(enumattr_name)
|
27
15
|
end
|
28
16
|
|
29
17
|
def enumattrs
|
30
18
|
@enumattrs ||= {}
|
31
19
|
end
|
32
20
|
|
33
|
-
def
|
34
|
-
|
21
|
+
def define_enumattr_methods(enumattr_name)
|
22
|
+
define_enumattr_class_methods(enumattr_name)
|
23
|
+
define_enumattr_instance_methods(enumattr_name)
|
35
24
|
end
|
36
25
|
|
37
26
|
def define_enumattr_class_methods(enumattr_name)
|
@@ -66,10 +55,10 @@ module Enumattr
|
|
66
55
|
def define_enumattr_instance_methods(enumattr_name)
|
67
56
|
enums = enumattrs[enumattr_name]
|
68
57
|
method_prefix = "#{enumattr_name}_"
|
69
|
-
|
58
|
+
enumattr_on = enums.opts[:on] || enumattr_name
|
70
59
|
|
71
60
|
define_method("#{method_prefix}enum") do
|
72
|
-
value = send
|
61
|
+
value = send enumattr_on
|
73
62
|
enums.enum_by_value(value)
|
74
63
|
end
|
75
64
|
|
@@ -79,20 +68,20 @@ module Enumattr
|
|
79
68
|
end
|
80
69
|
|
81
70
|
define_method(:"#{method_prefix}value") do
|
82
|
-
send
|
71
|
+
send enumattr_on
|
83
72
|
end
|
84
73
|
|
85
74
|
# setter by key
|
86
75
|
define_method("#{method_prefix}key=") do |new_key|
|
87
76
|
new_enum = enums.enum_by_key(new_key)
|
88
77
|
new_value = new_enum && new_enum.value
|
89
|
-
send "#{
|
78
|
+
send "#{enumattr_on}=", new_value
|
90
79
|
end
|
91
80
|
|
92
81
|
# Query methods
|
93
82
|
enums.enums.each do |enum|
|
94
83
|
define_method("#{method_prefix}#{enum.key}?") do
|
95
|
-
value = send
|
84
|
+
value = send enumattr_on
|
96
85
|
value == enum.value
|
97
86
|
end
|
98
87
|
end
|
data/lib/enumattr/enums.rb
CHANGED
@@ -3,12 +3,14 @@ require 'set'
|
|
3
3
|
|
4
4
|
module Enumattr
|
5
5
|
class Enums
|
6
|
-
attr_reader :
|
6
|
+
attr_reader :enumattr, :base, :opts
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
-
@
|
11
|
-
|
8
|
+
def initialize(enumattr, base, opts = {}, &block)
|
9
|
+
@enumattr = enumattr
|
10
|
+
@base = base
|
11
|
+
@opts = opts.freeze
|
12
|
+
@set = enum_set(&block)
|
13
|
+
decorate @set
|
12
14
|
end
|
13
15
|
|
14
16
|
def enums
|
@@ -32,21 +34,44 @@ module Enumattr
|
|
32
34
|
end
|
33
35
|
|
34
36
|
private
|
35
|
-
def
|
36
|
-
|
37
|
+
def enum_set(&block)
|
38
|
+
if enums_hash = @opts[:enums]
|
39
|
+
closure = proc{ enums_hash.each{|key, value| enum key, value } }
|
40
|
+
else
|
41
|
+
closure = block
|
42
|
+
end
|
43
|
+
|
44
|
+
context = Context.new(self, &closure)
|
45
|
+
context.instance_variable_get(:@set)
|
37
46
|
end
|
38
47
|
|
39
|
-
|
40
|
-
|
48
|
+
def decorate(set)
|
49
|
+
if @opts.has_key?(:extend)
|
50
|
+
set.each{|enum| enum.extend @opts[:extend] }
|
51
|
+
end
|
52
|
+
end
|
41
53
|
|
42
|
-
|
43
|
-
|
44
|
-
@value = value
|
54
|
+
class Context
|
55
|
+
def initialize(container, &closure)
|
45
56
|
@container = container
|
57
|
+
@set = Set.new
|
58
|
+
instance_eval(&closure)
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def enum(key, value, *extras)
|
63
|
+
@set.add Enum.new(@container, key, value, *extras)
|
46
64
|
end
|
65
|
+
end
|
47
66
|
|
48
|
-
|
49
|
-
|
67
|
+
class Enum
|
68
|
+
attr_reader :key, :value
|
69
|
+
|
70
|
+
def initialize(container, key, value, *extras)
|
71
|
+
@container = container
|
72
|
+
@key = key.to_sym
|
73
|
+
@value = value
|
74
|
+
@extras = extras
|
50
75
|
end
|
51
76
|
|
52
77
|
def hash
|
@@ -57,5 +82,6 @@ module Enumattr
|
|
57
82
|
@key == other.key
|
58
83
|
end
|
59
84
|
end
|
85
|
+
|
60
86
|
end
|
61
87
|
end
|
data/lib/enumattr/version.rb
CHANGED
data/spec/enumattr/enums_spec.rb
CHANGED
@@ -2,119 +2,129 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Enumattr::Enums do
|
5
|
-
|
6
|
-
|
7
|
-
:
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
enum :key1, 1
|
14
|
-
enum :key2, 2
|
15
|
-
enum :key3, 3
|
5
|
+
shared_examples "Enumattr::Enums find methods" do
|
6
|
+
describe "enum_by_key" do
|
7
|
+
context "with :test1" do
|
8
|
+
subject { enums.enum_by_key(:test1) }
|
9
|
+
it { should be_a Enumattr::Enums::Enum }
|
10
|
+
its(:key) { should == :test1 }
|
11
|
+
its(:value) { should == 1 }
|
12
|
+
end
|
16
13
|
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#context" do
|
20
|
-
subject { enums.context }
|
21
|
-
it { should be_a Hash }
|
22
|
-
it { should be_frozen }
|
23
|
-
end
|
24
14
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
15
|
+
describe "enum_by_value" do
|
16
|
+
context "with 1" do
|
17
|
+
subject { enums.enum_by_value(1) }
|
18
|
+
it { should be_a Enumattr::Enums::Enum }
|
19
|
+
its(:key) { should == :test1 }
|
20
|
+
its(:value) { should == 1 }
|
21
|
+
end
|
29
22
|
end
|
30
23
|
|
31
|
-
describe ":
|
32
|
-
subject { enums.
|
33
|
-
it { should
|
24
|
+
describe "enum_by_key :test1" do
|
25
|
+
subject { enums.enum_by_key(:test1) }
|
26
|
+
it { should be_a Enumattr::Enums::Enum }
|
27
|
+
its(:key) { should == :test1 }
|
28
|
+
its(:value) { should == 1 }
|
34
29
|
end
|
35
30
|
|
36
|
-
describe ":
|
37
|
-
subject { enums.
|
38
|
-
it { should
|
31
|
+
describe "enum_by_key :not_registered" do
|
32
|
+
subject { enums.enum_by_key(:not_registered) }
|
33
|
+
it { should be_nil }
|
39
34
|
end
|
40
35
|
end
|
41
36
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
37
|
+
context "with enumattr, base and block" do
|
38
|
+
class EnumsTest1
|
39
|
+
include Enumattr::Base
|
40
|
+
|
41
|
+
attr_accessor :test
|
42
|
+
|
43
|
+
enumattr :test do
|
44
|
+
enum :test1, 1
|
45
|
+
enum :test2, 2
|
46
|
+
enum :test3, 3
|
47
|
+
enum :test4, 4
|
48
|
+
enum :test5, 5
|
49
|
+
end
|
50
50
|
end
|
51
|
-
end
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
should
|
59
|
-
|
60
|
-
}
|
52
|
+
let(:enums) { EnumsTest1.instance_eval("enumattrs[:test]") }
|
53
|
+
|
54
|
+
describe "attributes" do
|
55
|
+
subject { enums }
|
56
|
+
its(:base) { should == EnumsTest1 }
|
57
|
+
its(:enums) { should have(5).enums }
|
58
|
+
its(:opts) { should be_empty }
|
59
|
+
its(:keys) { should == Set.new([:test1, :test2, :test3, :test4, :test5]) }
|
60
|
+
its(:values) { should == Set.new([1, 2, 3, 4, 5]) }
|
61
61
|
end
|
62
|
+
|
63
|
+
include_examples "Enumattr::Enums find methods"
|
64
|
+
|
62
65
|
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
}
|
67
|
+
context "with enumattr, base and :enums option" do
|
68
|
+
class EnumsTest2
|
69
|
+
include Enumattr::Base
|
70
|
+
|
71
|
+
attr_accessor :test
|
72
|
+
|
73
|
+
enumattr :test, :enums => {:test1 => 1, :test2 => 2, :test3 => 3}
|
72
74
|
end
|
73
|
-
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
its(:
|
76
|
+
let(:enums) { EnumsTest2.instance_eval("enumattrs[:test]") }
|
77
|
+
|
78
|
+
describe "attributes" do
|
79
|
+
subject { enums }
|
80
|
+
its(:base) { should == EnumsTest2 }
|
81
|
+
its(:enums) { should have(3).enums }
|
82
|
+
its(:opts) { should_not be_empty }
|
83
|
+
its(:keys) { should == Set.new([:test1, :test2, :test3]) }
|
84
|
+
its(:values) { should == Set.new([1, 2, 3]) }
|
80
85
|
end
|
81
86
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
subject { enums.enum_by_key(key) }
|
90
|
-
samples.each do |sample|
|
91
|
-
context "key: #{sample[:key]}" do
|
92
|
-
let(:key) { sample[:key] }
|
93
|
-
let(:expects) { sample }
|
94
|
-
|
95
|
-
include_examples "#enum_by_foo(foo) each items"
|
96
|
-
end
|
87
|
+
include_examples "Enumattr::Enums find methods"
|
88
|
+
end
|
89
|
+
|
90
|
+
context "with enumattr, base and :extend option" do
|
91
|
+
module NameExteision
|
92
|
+
def name
|
93
|
+
@extras.first
|
97
94
|
end
|
98
95
|
end
|
99
96
|
|
100
|
-
|
101
|
-
|
97
|
+
class EnumsTest3
|
98
|
+
include Enumattr::Base
|
102
99
|
|
103
|
-
|
104
|
-
context "value: #{sample[:value]}" do
|
105
|
-
let(:value) { sample[:value] }
|
106
|
-
let(:expects) { sample }
|
100
|
+
attr_accessor :test
|
107
101
|
|
108
|
-
|
109
|
-
|
102
|
+
enumattr :test, :extend => NameExteision do
|
103
|
+
enum :test1, 1, "test1 name"
|
104
|
+
enum :test2, 2, "test2 name"
|
105
|
+
enum :test3, 3, "test3 name"
|
106
|
+
enum :test4, 4, "test4 name"
|
110
107
|
end
|
111
108
|
end
|
112
|
-
end
|
113
109
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
110
|
+
let(:enums) { EnumsTest3.instance_eval("enumattrs[:test]") }
|
111
|
+
|
112
|
+
describe "attributes" do
|
113
|
+
subject { enums }
|
114
|
+
its(:base) { should == EnumsTest3 }
|
115
|
+
its(:enums) { should have(4).enums }
|
116
|
+
its(:opts) { should_not be_empty }
|
117
|
+
its(:keys) { should == Set.new([:test1, :test2, :test3, :test4]) }
|
118
|
+
its(:values) { should == Set.new([1, 2, 3, 4]) }
|
119
|
+
end
|
120
|
+
|
121
|
+
include_examples "Enumattr::Enums find methods"
|
122
|
+
|
123
|
+
describe "enum_by_key :test1" do
|
124
|
+
describe "extension method" do
|
125
|
+
subject { enums.enum_by_key(:test1) }
|
126
|
+
its(:name) { should == "test1 name" }
|
127
|
+
end
|
128
|
+
end
|
119
129
|
end
|
120
130
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumattr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70160534844020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70160534844020
|
14
25
|
description: simple enum
|
15
26
|
email:
|
16
27
|
- aisuiiaisuii@gmail.com
|
@@ -22,6 +33,7 @@ files:
|
|
22
33
|
- .rspec
|
23
34
|
- Gemfile
|
24
35
|
- LICENSE
|
36
|
+
- README.ja.md
|
25
37
|
- README.md
|
26
38
|
- Rakefile
|
27
39
|
- enumattr.gemspec
|