enumattr 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +95 -85
- data/enumattr.gemspec +1 -1
- data/examples/admin_user.rb +17 -0
- data/examples/entry.rb +13 -0
- data/examples/user.rb +4 -4
- data/lib/enumattr/base.rb +49 -31
- data/lib/enumattr/enums.rb +27 -3
- data/lib/enumattr/version.rb +1 -1
- data/lib/enumattr.rb +0 -1
- data/spec/enumattr/enums_spec.rb +18 -4
- data/spec/enumattr/included_spec.rb +290 -20
- metadata +6 -7
- data/lib/enumattr/enum.rb +0 -20
- data/spec/enumattr/enum_spec.rb +0 -38
data/README.md
CHANGED
@@ -21,111 +21,121 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
### defining
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
1. `include Enumattr::Base` and declare `enumattr attribute_name do ... end`
|
25
|
+
2. `enum :symbol, value` in `do ... end`
|
26
26
|
|
27
|
+
example:
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
include Enumattr::Base
|
29
|
+
class User
|
30
|
+
include Enumattr::Base
|
31
31
|
|
32
|
-
|
32
|
+
attr_accessor :status
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
enumattr :status do
|
35
|
+
enum :active, 1
|
36
|
+
enum :inactive, 2
|
37
|
+
enum :deleted, 3
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
</pre>
|
40
|
+
def initialize(status)
|
41
|
+
@status = status
|
42
|
+
end
|
43
|
+
end
|
45
44
|
|
46
45
|
### defined methods
|
47
46
|
|
48
47
|
#### class methods
|
49
48
|
|
50
|
-
*
|
51
|
-
*
|
52
|
-
*
|
49
|
+
* `#{attribute_name}_enums` return enum set
|
50
|
+
* `#{attribute_name}_keys` return key set
|
51
|
+
* `#{attribute_name}_values` return value set
|
53
52
|
|
54
|
-
|
55
|
-
User.status_enums
|
56
|
-
#=> #<Set: {#<Enumattr::Enum:0x8a8d13c @key=:active, @value=1>, #<Enumattr::Enum:0x8a8d100 @key=:suspend, @value=2>, #<Enumattr::Enum:0x8a8d0ec @key=:deleted, @value=3>}>
|
53
|
+
example:
|
57
54
|
|
58
|
-
User.
|
59
|
-
|
55
|
+
User.status_enums
|
56
|
+
#=> #<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>}>
|
60
57
|
|
61
|
-
User.
|
62
|
-
|
63
|
-
</pre>
|
58
|
+
User.status_keys
|
59
|
+
#=> #<Set: {:active, :inactive, :deleted}>
|
64
60
|
|
65
|
-
|
66
|
-
|
61
|
+
User.status_values
|
62
|
+
#=> #<Set: {1, 2, 3}>
|
67
63
|
|
68
|
-
|
69
|
-
|
70
|
-
#=> #<Enumattr::Enum:0x8a8d13c @key=:active, @value=1>
|
64
|
+
* `#{attribute_name}_enum(key)` return an enum
|
65
|
+
* `#{attribute_name}_value(key)` return a value
|
71
66
|
|
72
|
-
|
73
|
-
#=> #<Enumattr::Enum:0x8a8d100 @key=:suspend, @value=2>
|
67
|
+
example:
|
74
68
|
|
75
|
-
User.
|
76
|
-
|
77
|
-
|
78
|
-
User.
|
79
|
-
|
80
|
-
|
81
|
-
User.
|
82
|
-
|
83
|
-
|
84
|
-
User.
|
85
|
-
|
86
|
-
|
69
|
+
User.status_enum :active
|
70
|
+
#=> #<Enumattr::Enums::Enum:0x8a8d13c @key=:active, @value=1>
|
71
|
+
|
72
|
+
User.status_enum :inactive
|
73
|
+
#=> #<Enumattr::Enums::Enum:0x8a8d100 @key=:inactive, @value=2>
|
74
|
+
|
75
|
+
User.status_enum :dummy
|
76
|
+
#=> nil
|
77
|
+
|
78
|
+
User.status_value :active
|
79
|
+
#=> 1
|
80
|
+
|
81
|
+
User.status_value :inactive
|
82
|
+
#=> 2
|
83
|
+
|
84
|
+
User.status_value :dummy
|
85
|
+
#=> nil
|
87
86
|
|
88
87
|
#### instance methods
|
89
88
|
|
90
|
-
*
|
91
|
-
* `#{
|
92
|
-
* `#{
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
89
|
+
* getters
|
90
|
+
* alias `#{attribute_name}_value` `attribute_name`
|
91
|
+
* `#{attribute_name}_key` return symbol
|
92
|
+
* `#{attribute_name}_enum` return enum
|
93
|
+
* setter (if writable)
|
94
|
+
* `#{attribute_name}_key=(key)`
|
95
|
+
|
96
|
+
|
97
|
+
getters example:
|
98
|
+
|
99
|
+
user = User.new(1)
|
100
|
+
#=> #<User:0x8e17dac @status=1>
|
101
|
+
|
102
|
+
user.status
|
103
|
+
#=> 1
|
104
|
+
|
105
|
+
user.status_value # alias
|
106
|
+
#=> 1
|
107
|
+
|
108
|
+
user.status_key
|
109
|
+
#=> :active
|
110
|
+
|
111
|
+
user.status_enum
|
112
|
+
#=> #<Enumattr::Enums::Enum:0x8de2e68 @key=:active, @value=1>
|
113
|
+
|
114
|
+
setter example:
|
115
|
+
|
116
|
+
user.status_key = :inactive
|
117
|
+
#=> :inactive
|
118
|
+
|
119
|
+
user.status
|
120
|
+
#=> 2
|
121
|
+
|
122
|
+
|
123
|
+
* Query Method
|
124
|
+
* `#{attribute_name}_#{key}?` return true or false
|
125
|
+
|
126
|
+
example:
|
127
|
+
|
128
|
+
user.status_active?
|
129
|
+
#=> false
|
130
|
+
|
131
|
+
user.status_inactive?
|
132
|
+
#=> true
|
133
|
+
|
134
|
+
user.status_deleted?
|
135
|
+
#=> false
|
136
|
+
|
137
|
+
user.status_dummy?
|
138
|
+
NoMethodError: undefined method `status_dummy?' for #<User:0x8e17dac @status=1>
|
129
139
|
|
130
140
|
## Contributing
|
131
141
|
|
data/enumattr.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ["aisuiiaisuii@gmail.com"]
|
7
7
|
gem.description = %q{simple enum}
|
8
8
|
gem.summary = %q{manage constants by enum like object}
|
9
|
-
gem.homepage = ""
|
9
|
+
gem.homepage = "https://github.com/aisuii/enumattr"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
class AdminUser
|
4
|
+
include Enumattr::Base
|
5
|
+
|
6
|
+
attr_accessor :role
|
7
|
+
|
8
|
+
enumattr :authority, :on => :role do
|
9
|
+
enum :super, 1
|
10
|
+
enum :approver, 2
|
11
|
+
enum :editor, 3
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(role)
|
15
|
+
@role = role
|
16
|
+
end
|
17
|
+
end
|
data/examples/entry.rb
ADDED
data/examples/user.rb
CHANGED
data/lib/enumattr/base.rb
CHANGED
@@ -8,71 +8,89 @@ module Enumattr
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
private
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def enumattr(enumattr_name, options = {}, &block)
|
12
|
+
if options[:enums]
|
13
|
+
closure = Proc.new do
|
14
|
+
options[:enums].each{|key, value| enum key, value }
|
15
|
+
end
|
16
|
+
enumattrs[enumattr_name] = Enums.new(self, &closure)
|
17
|
+
else
|
18
|
+
enumattrs[enumattr_name] = Enums.new(self, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
enumattr_bases[enumattr_name] = options[:on] || enumattr_name
|
22
|
+
|
23
|
+
define_enumattr_class_methods enumattr_name
|
24
|
+
define_enumattr_instance_methods enumattr_name
|
16
25
|
end
|
17
26
|
|
18
|
-
def
|
19
|
-
@
|
27
|
+
def enumattrs
|
28
|
+
@enumattrs ||= {}
|
20
29
|
end
|
21
30
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
31
|
+
def enumattr_bases
|
32
|
+
@enumattr_bases ||= {}
|
33
|
+
end
|
25
34
|
|
26
|
-
|
35
|
+
def define_enumattr_class_methods(enumattr_name)
|
36
|
+
method_prefix = "#{enumattr_name}_"
|
37
|
+
|
38
|
+
enumattr_class_methods = Module.new do
|
27
39
|
define_method("#{method_prefix}enums") do
|
28
|
-
|
40
|
+
enumattrs[enumattr_name].enums
|
29
41
|
end
|
30
42
|
|
31
43
|
define_method("#{method_prefix}keys") do
|
32
|
-
|
44
|
+
enumattrs[enumattr_name].keys
|
33
45
|
end
|
34
46
|
|
35
47
|
define_method("#{method_prefix}values") do
|
36
|
-
|
48
|
+
enumattrs[enumattr_name].values
|
37
49
|
end
|
38
50
|
|
39
|
-
define_method("#{method_prefix}
|
40
|
-
|
51
|
+
define_method("#{method_prefix}enum") do |key|
|
52
|
+
enumattrs[enumattr_name].enum_by_key(key)
|
41
53
|
end
|
42
54
|
|
43
|
-
define_method("#{method_prefix}
|
44
|
-
enum =
|
55
|
+
define_method("#{method_prefix}value") do |key|
|
56
|
+
enum = enumattrs[enumattr_name].enum_by_key(key)
|
45
57
|
enum && enum.value
|
46
58
|
end
|
47
59
|
end
|
48
60
|
|
49
|
-
extend
|
61
|
+
extend enumattr_class_methods
|
50
62
|
end
|
51
63
|
|
52
|
-
def
|
53
|
-
enums =
|
54
|
-
method_prefix = "#{
|
64
|
+
def define_enumattr_instance_methods(enumattr_name)
|
65
|
+
enums = enumattrs[enumattr_name]
|
66
|
+
method_prefix = "#{enumattr_name}_"
|
67
|
+
enumattr_base = enumattr_bases[enumattr_name]
|
55
68
|
|
56
69
|
define_method("#{method_prefix}enum") do
|
57
|
-
value =
|
70
|
+
value = send enumattr_base
|
58
71
|
enums.enum_by_value(value)
|
59
72
|
end
|
60
73
|
|
61
74
|
define_method("#{method_prefix}key") do
|
62
|
-
enum =
|
63
|
-
enum.key
|
75
|
+
enum = send "#{method_prefix}enum"
|
76
|
+
enum && enum.key
|
64
77
|
end
|
65
78
|
|
66
|
-
|
67
|
-
|
79
|
+
define_method(:"#{method_prefix}value") do
|
80
|
+
send enumattr_base
|
81
|
+
end
|
68
82
|
|
69
|
-
|
70
|
-
|
71
|
-
|
83
|
+
# setter by key
|
84
|
+
define_method("#{method_prefix}key=") do |new_key|
|
85
|
+
new_enum = enums.enum_by_key(new_key)
|
86
|
+
new_value = new_enum && new_enum.value
|
87
|
+
send "#{enumattr_base}=", new_value
|
88
|
+
end
|
72
89
|
|
90
|
+
# Query methods
|
73
91
|
enums.enums.each do |enum|
|
74
92
|
define_method("#{method_prefix}#{enum.key}?") do
|
75
|
-
value =
|
93
|
+
value = send enumattr_base
|
76
94
|
value == enum.value
|
77
95
|
end
|
78
96
|
end
|
data/lib/enumattr/enums.rb
CHANGED
@@ -3,10 +3,12 @@ require 'set'
|
|
3
3
|
|
4
4
|
module Enumattr
|
5
5
|
class Enums
|
6
|
-
|
6
|
+
attr_reader :base
|
7
|
+
|
8
|
+
def initialize(base, &block)
|
9
|
+
@base = base
|
7
10
|
@set = Set.new
|
8
11
|
instance_eval(&block)
|
9
|
-
freeze
|
10
12
|
end
|
11
13
|
|
12
14
|
def enums
|
@@ -31,7 +33,29 @@ module Enumattr
|
|
31
33
|
|
32
34
|
private
|
33
35
|
def enum(key, value)
|
34
|
-
@set.add Enum.new(key, value)
|
36
|
+
@set.add Enum.new(key, value, self)
|
37
|
+
end
|
38
|
+
|
39
|
+
class Enum
|
40
|
+
attr_reader :key, :value
|
41
|
+
|
42
|
+
def initialize(key, value, container)
|
43
|
+
@key = key.to_sym
|
44
|
+
@value = value
|
45
|
+
@container = container
|
46
|
+
end
|
47
|
+
|
48
|
+
def enums
|
49
|
+
@container.enums
|
50
|
+
end
|
51
|
+
|
52
|
+
def hash
|
53
|
+
@key.hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def eql?(other)
|
57
|
+
@key == other.key
|
58
|
+
end
|
35
59
|
end
|
36
60
|
end
|
37
61
|
end
|
data/lib/enumattr/version.rb
CHANGED
data/lib/enumattr.rb
CHANGED
data/spec/enumattr/enums_spec.rb
CHANGED
@@ -2,21 +2,28 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Enumattr::Enums do
|
5
|
+
let(:base) { Object }
|
6
|
+
|
5
7
|
let(:enums) do
|
6
|
-
Enumattr::Enums.new do
|
8
|
+
Enumattr::Enums.new(base) do
|
7
9
|
enum :key1, 1
|
8
10
|
enum :key2, 2
|
9
11
|
enum :key3, 3
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
15
|
+
describe "#base" do
|
16
|
+
subject { enums.base }
|
17
|
+
it { should == Object }
|
18
|
+
end
|
19
|
+
|
13
20
|
describe "#enums" do
|
14
21
|
subject { enums.enums }
|
15
22
|
it { should be_a Set }
|
16
23
|
it { should have(3).items }
|
17
|
-
it "should have Enumattr::Enum instances" do
|
24
|
+
it "should have Enumattr::Enums::Enum instances" do
|
18
25
|
should satisfy { |enums|
|
19
|
-
enums.all?{|item| item.is_a? Enumattr::Enum }
|
26
|
+
enums.all?{|item| item.is_a? Enumattr::Enums::Enum }
|
20
27
|
}
|
21
28
|
end
|
22
29
|
end
|
@@ -45,7 +52,7 @@ describe Enumattr::Enums do
|
|
45
52
|
|
46
53
|
describe "find methods" do
|
47
54
|
shared_examples "#enum_by_foo(foo) each items" do
|
48
|
-
it { should be_a Enumattr::Enum }
|
55
|
+
it { should be_a Enumattr::Enums::Enum }
|
49
56
|
its(:key) { should == expects[:key] }
|
50
57
|
its(:value) { should == expects[:value] }
|
51
58
|
end
|
@@ -81,4 +88,11 @@ describe Enumattr::Enums do
|
|
81
88
|
end
|
82
89
|
end
|
83
90
|
end
|
91
|
+
|
92
|
+
describe Enumattr::Enums::Enum do
|
93
|
+
subject { enums.enum_by_key(:key1) }
|
94
|
+
its(:key) { should == :key1 }
|
95
|
+
its(:value) { should == 1 }
|
96
|
+
its(:enums) { should == enums.enums }
|
97
|
+
end
|
84
98
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'user'
|
4
|
+
require 'admin_user'
|
5
|
+
require 'entry'
|
4
6
|
|
5
|
-
describe User do
|
7
|
+
describe User, 'declare with block' do
|
6
8
|
describe "class methods" do
|
7
|
-
describe '.status_enums as #{
|
9
|
+
describe '.status_enums as #{enumattr_name}_enums' do
|
8
10
|
subject { described_class.status_enums }
|
9
11
|
|
10
12
|
it { should be_a Set }
|
11
13
|
it { should have(3).items }
|
12
14
|
it "should have Enum instances" do
|
13
15
|
should satisfy { |enums|
|
14
|
-
enums.all?{|item| item.is_a? Enumattr::Enum }
|
16
|
+
enums.all?{|item| item.is_a? Enumattr::Enums::Enum }
|
15
17
|
}
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
describe '.status_keys as #{
|
21
|
+
describe '.status_keys as #{enumattr_name}_keys' do
|
20
22
|
subject { described_class.status_keys }
|
21
23
|
|
22
24
|
it { should be_a Set }
|
@@ -28,7 +30,7 @@ describe User do
|
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
|
-
describe '.status_values as #{
|
33
|
+
describe '.status_values as #{enumattr_name}_values' do
|
32
34
|
subject { described_class.status_values }
|
33
35
|
|
34
36
|
it { should be_a Set }
|
@@ -40,23 +42,23 @@ describe User do
|
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
|
-
describe '.
|
44
|
-
subject { described_class.
|
45
|
+
describe '.status_enum(key) as #{enumattr_name}_enum(key)' do
|
46
|
+
subject { described_class.status_enum(:active) }
|
45
47
|
|
46
|
-
it { should be_a Enumattr::Enum }
|
48
|
+
it { should be_a Enumattr::Enums::Enum }
|
47
49
|
its(:key) { should == :active }
|
48
50
|
its(:value) { should == 1 }
|
49
51
|
end
|
50
52
|
|
51
|
-
describe '.
|
53
|
+
describe '.status_value(key) as #{enumattr_name}_value(key)' do
|
52
54
|
context "present key" do
|
53
|
-
subject { described_class.
|
55
|
+
subject { described_class.status_value(:active) }
|
54
56
|
|
55
57
|
it { should == 1 }
|
56
58
|
end
|
57
59
|
|
58
60
|
context "not present key" do
|
59
|
-
subject { described_class.
|
61
|
+
subject { described_class.status_value(:not_present_key) }
|
60
62
|
|
61
63
|
it { should be_nil }
|
62
64
|
end
|
@@ -65,7 +67,7 @@ describe User do
|
|
65
67
|
|
66
68
|
|
67
69
|
describe "instance methods" do
|
68
|
-
let(:user) {
|
70
|
+
let(:user) { described_class.new(1) }
|
69
71
|
|
70
72
|
describe "#status" do
|
71
73
|
subject { user.status }
|
@@ -73,44 +75,312 @@ describe User do
|
|
73
75
|
it { should == 1 }
|
74
76
|
end
|
75
77
|
|
76
|
-
describe '#status_enum as #{
|
78
|
+
describe '#status_enum as #{enumattr_name}_enum' do
|
77
79
|
subject { user.status_enum }
|
78
80
|
|
79
|
-
it { should be_a Enumattr::Enum }
|
81
|
+
it { should be_a Enumattr::Enums::Enum }
|
80
82
|
its(:key) { should == :active }
|
81
83
|
its(:value) { should == 1 }
|
82
84
|
end
|
83
85
|
|
84
|
-
describe '#status_key as #{
|
86
|
+
describe '#status_key as #{enumattr_name}_key' do
|
85
87
|
subject { user.status_key }
|
86
88
|
|
87
89
|
it { should be_a Symbol }
|
88
90
|
it { should == :active }
|
89
91
|
end
|
90
92
|
|
91
|
-
describe '#
|
93
|
+
describe '#status_key=(key) as #{enumattr_name}_key=(key)' do
|
94
|
+
before do
|
95
|
+
user.status_key = :inactive
|
96
|
+
end
|
97
|
+
|
98
|
+
subject { user }
|
99
|
+
|
100
|
+
its(:status) { should == 2 }
|
101
|
+
its(:status_key) { should == :inactive }
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#status_value as #{enumattr_name}_value' do
|
92
105
|
subject { user.status_value }
|
93
106
|
|
94
107
|
it { should be_a Numeric }
|
95
108
|
it { should == 1 }
|
96
109
|
end
|
97
110
|
|
98
|
-
describe '#status_active? as #{
|
111
|
+
describe '#status_active? as #{enumattr_name}_#{status_key}?' do
|
99
112
|
subject { user.status_active? }
|
100
113
|
|
101
114
|
it { should be_true }
|
102
115
|
end
|
103
116
|
|
104
|
-
describe '#
|
105
|
-
subject { user.
|
117
|
+
describe '#status_inactive? as #{enumattr_name}_#{other_status_key}?' do
|
118
|
+
subject { user.status_inactive? }
|
106
119
|
|
107
120
|
it { should be_false }
|
108
121
|
end
|
109
122
|
|
110
|
-
describe '#status_deleted? as #{
|
123
|
+
describe '#status_deleted? as #{enumattr_name}_#{other_status_key}?' do
|
111
124
|
subject { user.status_deleted? }
|
112
125
|
|
113
126
|
it { should be_false }
|
114
127
|
end
|
128
|
+
|
129
|
+
context "out of range value" do
|
130
|
+
subject { user }
|
131
|
+
before { user.status = 4 }
|
132
|
+
its(:status_enum) { should be_nil }
|
133
|
+
its(:status_key) { should be_nil }
|
134
|
+
its(:status_value) { should == 4 }
|
135
|
+
its(:status_active?) { should be_false }
|
136
|
+
its(:status_inactive?) { should be_false }
|
137
|
+
its(:status_deleted?) { should be_false }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
describe AdminUser,"with :on option" do
|
144
|
+
describe 'class methods' do
|
145
|
+
describe '.authority_enums as #{enumattr_name}_enums' do
|
146
|
+
subject { described_class.authority_enums }
|
147
|
+
|
148
|
+
it { should be_a Set }
|
149
|
+
it { should have(3).items }
|
150
|
+
it "should have Enum instances" do
|
151
|
+
should satisfy { |enums|
|
152
|
+
enums.all?{|item| item.is_a? Enumattr::Enums::Enum }
|
153
|
+
}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe '.authority_keys as #{enumattr_name}_keys' do
|
158
|
+
subject { described_class.authority_keys }
|
159
|
+
|
160
|
+
it { should be_a Set }
|
161
|
+
it { should have(3).items }
|
162
|
+
it "should have Symbol instances" do
|
163
|
+
should satisfy { |keys|
|
164
|
+
keys.all?{|item| item.is_a? Symbol }
|
165
|
+
}
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe '.authority_values as #{enumattr_name}_values' do
|
170
|
+
subject { described_class.authority_values }
|
171
|
+
|
172
|
+
it { should be_a Set }
|
173
|
+
it { should have(3).items }
|
174
|
+
it "should have Numeric instances" do
|
175
|
+
should satisfy { |values|
|
176
|
+
values.all?{|item| item.is_a? Numeric }
|
177
|
+
}
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe '.authority_enum(key) as #{enumattr_name}_enum(key)' do
|
182
|
+
subject { described_class.authority_enum(:super) }
|
183
|
+
|
184
|
+
it { should be_a Enumattr::Enums::Enum }
|
185
|
+
its(:key) { should == :super }
|
186
|
+
its(:value) { should == 1 }
|
187
|
+
end
|
188
|
+
|
189
|
+
describe '.authority_value(key) as #{enumattr_name}_value(key)' do
|
190
|
+
context "present key" do
|
191
|
+
subject { described_class.authority_value(:super) }
|
192
|
+
|
193
|
+
it { should == 1 }
|
194
|
+
end
|
195
|
+
|
196
|
+
context "not present key" do
|
197
|
+
subject { described_class.authority_value(:not_present_key) }
|
198
|
+
|
199
|
+
it { should be_nil }
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
describe "instance methods" do
|
206
|
+
let(:user) { described_class.new(1) }
|
207
|
+
|
208
|
+
describe "#role" do
|
209
|
+
subject { user.role }
|
210
|
+
|
211
|
+
it { should == 1 }
|
212
|
+
end
|
213
|
+
|
214
|
+
describe '#authority_enum as #{enumattr_name}_enum' do
|
215
|
+
subject { user.authority_enum }
|
216
|
+
|
217
|
+
it { should be_a Enumattr::Enums::Enum }
|
218
|
+
its(:key) { should == :super }
|
219
|
+
its(:value) { should == 1 }
|
220
|
+
end
|
221
|
+
|
222
|
+
describe '#authority_key as #{enumattr_name}_key' do
|
223
|
+
subject { user.authority_key }
|
224
|
+
|
225
|
+
it { should be_a Symbol }
|
226
|
+
it { should == :super }
|
227
|
+
end
|
228
|
+
|
229
|
+
describe '#authority_key=(key) as #{enumattr_name}_key=(key)' do
|
230
|
+
before do
|
231
|
+
user.authority_key = :approver
|
232
|
+
end
|
233
|
+
|
234
|
+
subject { user }
|
235
|
+
|
236
|
+
its(:role) { should == 2 }
|
237
|
+
its(:authority_value) { should == 2 }
|
238
|
+
its(:authority_key) { should == :approver }
|
239
|
+
end
|
240
|
+
|
241
|
+
describe '#authority_value as #{enumattr_name}_value' do
|
242
|
+
subject { user.authority_value }
|
243
|
+
|
244
|
+
it { should be_a Numeric }
|
245
|
+
it { should == 1 }
|
246
|
+
end
|
247
|
+
|
248
|
+
describe '#authority_super? as #{enumattr_name}_#{authority_key}?' do
|
249
|
+
subject { user.authority_super? }
|
250
|
+
|
251
|
+
it { should be_true }
|
252
|
+
end
|
253
|
+
|
254
|
+
describe '#authority_approver? as #{enumattr_name}_#{other_authority_key}?' do
|
255
|
+
subject { user.authority_approver? }
|
256
|
+
|
257
|
+
it { should be_false }
|
258
|
+
end
|
259
|
+
|
260
|
+
describe '#authority_editor? as #{enumattr_name}_#{other_authority_key}?' do
|
261
|
+
subject { user.authority_editor? }
|
262
|
+
|
263
|
+
it { should be_false }
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
|
269
|
+
describe Entry,"with :enums option" do
|
270
|
+
describe 'class methods' do
|
271
|
+
describe '.show_flag_enums as #{enumattr_name}_enums' do
|
272
|
+
subject { described_class.show_flag_enums }
|
273
|
+
|
274
|
+
it { should be_a Set }
|
275
|
+
it { should have(2).items }
|
276
|
+
it "should have Enum instances" do
|
277
|
+
should satisfy { |enums|
|
278
|
+
enums.all?{|item| item.is_a? Enumattr::Enums::Enum }
|
279
|
+
}
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe '.show_flag_keys as #{enumattr_name}_keys' do
|
284
|
+
subject { described_class.show_flag_keys }
|
285
|
+
|
286
|
+
it { should be_a Set }
|
287
|
+
it { should have(2).items }
|
288
|
+
it "should have Symbol instances" do
|
289
|
+
should satisfy { |keys|
|
290
|
+
keys.all?{|item| item.is_a? Symbol }
|
291
|
+
}
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe '.show_flag_values as #{enumattr_name}_values' do
|
296
|
+
subject { described_class.show_flag_values }
|
297
|
+
|
298
|
+
it { should be_a Set }
|
299
|
+
it { should have(2).items }
|
300
|
+
it "should have TrueClass or FalseClass instances" do
|
301
|
+
should satisfy { |values|
|
302
|
+
values.all?{|item| item.is_a? TrueClass or item.is_a? FalseClass }
|
303
|
+
}
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe '.show_flag_enum(key) as #{enumattr_name}_enum(key)' do
|
308
|
+
subject { described_class.show_flag_enum(:opened) }
|
309
|
+
|
310
|
+
it { should be_a Enumattr::Enums::Enum }
|
311
|
+
its(:key) { should == :opened }
|
312
|
+
its(:value) { should == true }
|
313
|
+
end
|
314
|
+
|
315
|
+
describe '.show_flag_value(key) as #{enumattr_name}_value(key)' do
|
316
|
+
context "present key" do
|
317
|
+
subject { described_class.show_flag_value(:opened) }
|
318
|
+
|
319
|
+
it { should == true }
|
320
|
+
end
|
321
|
+
|
322
|
+
context "not present key" do
|
323
|
+
subject { described_class.show_flag_value(:not_present_key) }
|
324
|
+
|
325
|
+
it { should be_nil }
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
|
331
|
+
describe "instance methods" do
|
332
|
+
let(:entry) { described_class.new(true) }
|
333
|
+
|
334
|
+
describe "#show_flag" do
|
335
|
+
subject { entry.show_flag }
|
336
|
+
|
337
|
+
it { should == true }
|
338
|
+
end
|
339
|
+
|
340
|
+
describe '#show_flag_enum as #{enumattr_name}_enum' do
|
341
|
+
subject { entry.show_flag_enum }
|
342
|
+
|
343
|
+
it { should be_a Enumattr::Enums::Enum }
|
344
|
+
its(:key) { should == :opened }
|
345
|
+
its(:value) { should == true }
|
346
|
+
end
|
347
|
+
|
348
|
+
describe '#show_flag_key as #{enumattr_name}_key' do
|
349
|
+
subject { entry.show_flag_key }
|
350
|
+
|
351
|
+
it { should be_a Symbol }
|
352
|
+
it { should == :opened }
|
353
|
+
end
|
354
|
+
|
355
|
+
describe '#show_flag_key=(key) as #{enumattr_name}_key=(key)' do
|
356
|
+
before do
|
357
|
+
entry.show_flag_key = :closed
|
358
|
+
end
|
359
|
+
|
360
|
+
subject { entry }
|
361
|
+
|
362
|
+
its(:show_flag) { should == false }
|
363
|
+
its(:show_flag_value) { should == false }
|
364
|
+
its(:show_flag_key) { should == :closed }
|
365
|
+
end
|
366
|
+
|
367
|
+
describe '#show_flag_value as #{enumattr_name}_value' do
|
368
|
+
subject { entry.show_flag_value }
|
369
|
+
|
370
|
+
it { should be_a TrueClass }
|
371
|
+
it { should == true }
|
372
|
+
end
|
373
|
+
|
374
|
+
describe '#show_flag_opened? as #{enumattr_name}_#{show_flag_key}?' do
|
375
|
+
subject { entry.show_flag_opened? }
|
376
|
+
|
377
|
+
it { should be_true }
|
378
|
+
end
|
379
|
+
|
380
|
+
describe '#show_flag_closed? as #{enumattr_name}_#{other_show_flag_key}?' do
|
381
|
+
subject { entry.show_flag_closed? }
|
382
|
+
|
383
|
+
it { should be_false }
|
384
|
+
end
|
115
385
|
end
|
116
386
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: simple enum
|
15
15
|
email:
|
@@ -25,17 +25,17 @@ files:
|
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
27
|
- enumattr.gemspec
|
28
|
+
- examples/admin_user.rb
|
29
|
+
- examples/entry.rb
|
28
30
|
- examples/user.rb
|
29
31
|
- lib/enumattr.rb
|
30
32
|
- lib/enumattr/base.rb
|
31
|
-
- lib/enumattr/enum.rb
|
32
33
|
- lib/enumattr/enums.rb
|
33
34
|
- lib/enumattr/version.rb
|
34
|
-
- spec/enumattr/enum_spec.rb
|
35
35
|
- spec/enumattr/enums_spec.rb
|
36
36
|
- spec/enumattr/included_spec.rb
|
37
37
|
- spec/spec_helper.rb
|
38
|
-
homepage:
|
38
|
+
homepage: https://github.com/aisuii/enumattr
|
39
39
|
licenses: []
|
40
40
|
post_install_message:
|
41
41
|
rdoc_options: []
|
@@ -55,12 +55,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
55
|
version: '0'
|
56
56
|
requirements: []
|
57
57
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.8.
|
58
|
+
rubygems_version: 1.8.17
|
59
59
|
signing_key:
|
60
60
|
specification_version: 3
|
61
61
|
summary: manage constants by enum like object
|
62
62
|
test_files:
|
63
|
-
- spec/enumattr/enum_spec.rb
|
64
63
|
- spec/enumattr/enums_spec.rb
|
65
64
|
- spec/enumattr/included_spec.rb
|
66
65
|
- spec/spec_helper.rb
|
data/lib/enumattr/enum.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
module Enumattr
|
4
|
-
class Enum
|
5
|
-
attr_reader :key, :value
|
6
|
-
|
7
|
-
def initialize(key, value)
|
8
|
-
@key = key.to_sym
|
9
|
-
@value = value
|
10
|
-
end
|
11
|
-
|
12
|
-
def hash
|
13
|
-
@key.hash
|
14
|
-
end
|
15
|
-
|
16
|
-
def eql?(other)
|
17
|
-
@key == other.key
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/spec/enumattr/enum_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe Enumattr::Enum do
|
5
|
-
let(:one_enum) { Enumattr::Enum.new(:my_key, 1) }
|
6
|
-
let(:same_key_enum) { Enumattr::Enum.new(:my_key, 2) }
|
7
|
-
let(:another_enum) { Enumattr::Enum.new(:another, 3) }
|
8
|
-
|
9
|
-
describe "getters" do
|
10
|
-
subject { one_enum }
|
11
|
-
its(:key) { should == :my_key }
|
12
|
-
its(:value) { should == 1 }
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "in hash key" do
|
16
|
-
context "when hash has same key enum as hash key" do
|
17
|
-
subject do
|
18
|
-
hash = {}
|
19
|
-
hash[one_enum] = 1
|
20
|
-
hash[same_key_enum] = 2 # only value overrided
|
21
|
-
hash[another_enum] = 3
|
22
|
-
hash
|
23
|
-
end
|
24
|
-
|
25
|
-
its(:keys) { should include one_enum }
|
26
|
-
its(:keys) { should_not include same_key_enum }
|
27
|
-
its(:keys) { should include another_enum }
|
28
|
-
|
29
|
-
its(:values) { should_not include 1 }
|
30
|
-
its(:values) { should include 2 }
|
31
|
-
its(:values) { should include 3 }
|
32
|
-
|
33
|
-
it "should have 2 items" do
|
34
|
-
subject.should have(2).items
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|