enumattr 0.0.2 → 0.0.3
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/README.md +43 -5
- data/lib/enumattr/base.rb +4 -2
- data/lib/enumattr/enums.rb +3 -3
- data/lib/enumattr/version.rb +1 -1
- data/spec/enumattr/enums_spec.rb +28 -6
- metadata +2 -2
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# Enumattr
|
2
2
|
|
3
|
-
*
|
4
|
-
*
|
3
|
+
* mapping constant value and keyword
|
4
|
+
* _class_ knows the mapping
|
5
|
+
* _instance_ knows its attribute value's keyword from the mapping
|
5
6
|
|
6
7
|
## Installation
|
7
8
|
|
@@ -21,7 +22,8 @@ Or install it yourself as:
|
|
21
22
|
|
22
23
|
### defining
|
23
24
|
|
24
|
-
1. `include Enumattr::Base`
|
25
|
+
1. `include Enumattr::Base`
|
26
|
+
2. declare `enumattr attribute_name do ... end`
|
25
27
|
2. `enum :symbol, value` in `do ... end`
|
26
28
|
|
27
29
|
example:
|
@@ -42,7 +44,43 @@ example:
|
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
|
-
|
47
|
+
or alternative style
|
48
|
+
|
49
|
+
1. `include Enumattr::Base`
|
50
|
+
2. declare `enumattr attribute_name, :enums => {:symbol => value ...}`
|
51
|
+
|
52
|
+
example:
|
53
|
+
|
54
|
+
class User
|
55
|
+
include Enumattr::Base
|
56
|
+
|
57
|
+
attr_accessor :status
|
58
|
+
|
59
|
+
enumattr :status, :enums => {:active => 1, :inactive => 2, :deleted => 3}
|
60
|
+
|
61
|
+
def initialize(status)
|
62
|
+
@status = status
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
### defining options
|
67
|
+
|
68
|
+
class User
|
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
|
46
84
|
|
47
85
|
#### class methods
|
48
86
|
|
@@ -135,7 +173,7 @@ example:
|
|
135
173
|
#=> false
|
136
174
|
|
137
175
|
user.status_dummy?
|
138
|
-
NoMethodError: undefined method `status_dummy?' for #<User:0x8e17dac @status=
|
176
|
+
NoMethodError: undefined method `status_dummy?' for #<User:0x8e17dac @status=2>
|
139
177
|
|
140
178
|
## Contributing
|
141
179
|
|
data/lib/enumattr/base.rb
CHANGED
@@ -13,11 +13,13 @@ module Enumattr
|
|
13
13
|
closure = Proc.new do
|
14
14
|
options[:enums].each{|key, value| enum key, value }
|
15
15
|
end
|
16
|
-
enumattrs[enumattr_name] = Enums.new(self, &closure)
|
17
16
|
else
|
18
|
-
|
17
|
+
closure = block
|
19
18
|
end
|
20
19
|
|
20
|
+
context = options.merge(:enumattr => enumattr_name, :base => self)
|
21
|
+
|
22
|
+
enumattrs[enumattr_name] = Enums.new(context, &closure)
|
21
23
|
enumattr_bases[enumattr_name] = options[:on] || enumattr_name
|
22
24
|
|
23
25
|
define_enumattr_class_methods enumattr_name
|
data/lib/enumattr/enums.rb
CHANGED
data/lib/enumattr/version.rb
CHANGED
data/spec/enumattr/enums_spec.rb
CHANGED
@@ -2,19 +2,41 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Enumattr::Enums do
|
5
|
-
let(:base) { Object }
|
6
|
-
|
7
5
|
let(:enums) do
|
8
|
-
|
6
|
+
defining_context = {
|
7
|
+
:class => Object,
|
8
|
+
:enumattr => :example,
|
9
|
+
:something => :something
|
10
|
+
}
|
11
|
+
|
12
|
+
Enumattr::Enums.new(defining_context) do
|
9
13
|
enum :key1, 1
|
10
14
|
enum :key2, 2
|
11
15
|
enum :key3, 3
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
15
|
-
describe "#
|
16
|
-
subject { enums.
|
17
|
-
it { should
|
19
|
+
describe "#context" do
|
20
|
+
subject { enums.context }
|
21
|
+
it { should be_a Hash }
|
22
|
+
it { should be_frozen }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#context contents" do
|
26
|
+
describe ":class" do
|
27
|
+
subject { enums.context[:class] }
|
28
|
+
it { should == Object }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ":enumattr" do
|
32
|
+
subject { enums.context[:enumattr] }
|
33
|
+
it { should == :example }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe ":something" do
|
37
|
+
subject { enums.context[:something] }
|
38
|
+
it { should == :something }
|
39
|
+
end
|
18
40
|
end
|
19
41
|
|
20
42
|
describe "#enums" do
|
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.3
|
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-30 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: simple enum
|
15
15
|
email:
|