rails_attr_enum 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 594e470d58bf61cf2a5a268d001ffd9350ed350d
4
- data.tar.gz: 2c06a6695f1830ddd243128fd7ccd40d5e963ced
3
+ metadata.gz: b9801e3908003e982b471d4af5ddbd4e589eb128
4
+ data.tar.gz: e0ad1758a752118f79a55e87a6d2b8a44f03cc8e
5
5
  SHA512:
6
- metadata.gz: d14af5e3c4f4b2b79dc8b7c966e3df7677d3a5b59b026f84005ee1b8a89ea0b24e573fcc90dd6e1f790facc99b0625f6b21eb2633a9798f8f504e5e0764f302d
7
- data.tar.gz: 8a94f8c39d9c41c6675c9de3f6cadee2e9814876c51e5df2056c840203b91e4341ba19d0638cb3920edad9e8274da4faee0252173be174965a58c9d634911e10
6
+ metadata.gz: de99edcb330ea895b1b5fa548d0291cb4b71ef742a6aa84066fb02f150b3a9286c8eb8ce8cde65e53cd2a66a6e55944ea44b2d5524c0c31211ec220fed70592b
7
+ data.tar.gz: 8ddb3ae5b3cbf6bf8cc0561b405aff12000c035bd24f713ae4cf13b0fba5f5db2498d498994ea085d747e87a24828c6ef9be06e14993953858b2e7abee029aaf
data/README.md ADDED
@@ -0,0 +1,168 @@
1
+ # RailsAttrEnum
2
+ ## Enums for Rails models
3
+
4
+ I created RailsAttrEnum as a way to create an enum-like structure similar to
5
+ enums in C languages. You can specify the accepted identifiers for the possible
6
+ integer values for the model's attribute as well have built-in validation to
7
+ ensure only the values are accepted.
8
+
9
+ ### Usage
10
+
11
+ Here's an example given a class `User` with an attribute `role`:
12
+
13
+ # Example model User for a blog app
14
+ class User < ActiveRecord::Base
15
+ extend RailsAttrEnum
16
+
17
+ attr_enum :role, :admin, :author, :editor, :user
18
+ end
19
+
20
+ # Creates module `User::Role` with constants for each possible value
21
+ User::Role::ADMIN == 0
22
+ User::Role::AUTHOR == 1
23
+ User::Role::EDITOR == 2
24
+ User::Role::USER == 3
25
+
26
+ As you can see, this would give a module `User::Role` containing constants `ADMIN`, `AUTHOR`,
27
+ `EDITOR`, and `USER` with the respective values of `0`, `1`, `2`, and `3`.
28
+
29
+ You can also specify the integer values for each identifier or only some. Those
30
+ you don't specify will automatically be filled with the first available integer
31
+ value.
32
+
33
+ # Target specific identifiers
34
+ class User < ActiveRecord::Base
35
+ extend RailsAttrEnum
36
+
37
+ attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
38
+ end
39
+
40
+ User::Role::ADMIN == 0
41
+ User::Role::AUTHOR == 12
42
+ User::Role::EDITOR == 1 # Notice this still defaulted to 1
43
+ User::Role::USER == 42
44
+
45
+ # Use a hash to specify all values
46
+ class User < ActiveRecord::Base
47
+ extend RailsAttrEnum
48
+
49
+ attr_enum :role, {
50
+ admin: 1,
51
+ author: 2,
52
+ editor: 4,
53
+ user: 8
54
+ }
55
+ end
56
+
57
+ User::Role::ADMIN == 1
58
+ User::Role::AUTHOR == 2
59
+ User::Role::EDITOR == 4
60
+ User::Role::USER == 8
61
+
62
+ # Use a block to specify some (or all)
63
+ class User < ActiveRecord::Base
64
+ extend RailsAttrEnum
65
+
66
+ attr_enum :role do
67
+ add admin: 42
68
+ add :author
69
+ add :editor
70
+ add user: 7
71
+ end
72
+ end
73
+
74
+ User::Role::ADMIN == 42
75
+ User::Role::AUTHOR == 0 # Again notice how `AUTHOR` and `EDITOR` defaulted
76
+ User::Role::EDITOR == 1
77
+ User::Role::USER == 7
78
+
79
+ ### Labels
80
+ RailsAttrEnum also creates a label for each identifier that you can use in your
81
+ app to display something meaningful for a value. Appropriate label constants are
82
+ added to the module enum as well as a helper `display_*` method on instances of
83
+ your model.
84
+
85
+ class User < ActiveRecord::Base
86
+ extend RailsAttrEnum
87
+
88
+ attr_enum :role, :admin, :author, :editor, :user
89
+ end
90
+
91
+ User::Role::ADMIN_LABEL == 'Admin'
92
+ User::Role::AUTHOR_LABEL == 'Author'
93
+ User::Role::EDITOR_LABEL == 'Editor'
94
+ User::Role::USER_LABEL == 'User'
95
+
96
+ user = User.new(role: User::Role::ADMIN)
97
+ user.display_role == 'Admin' # Helper method added by RailsAttrEnum
98
+
99
+ You can specify your own labels if you like. By default, RailAttrEnum calls
100
+ `.to_s.titleize` on the symbol identifier.
101
+
102
+ class User < ActiveRecord::Base
103
+ extend RailsAttrEnum
104
+
105
+ attr_enum :role,
106
+ { admin: 'Admin Role' }, :author, { editor: 'Editor Role' }, :user
107
+ end
108
+
109
+ User::Role::ADMIN_LABEL == 'Admin Role'
110
+ User::Role::AUTHOR_LABEL == 'Author'
111
+ User::Role::EDITOR_LABEL == 'Editor Role'
112
+ User::Role::USER_LABEL == 'User'
113
+
114
+ # With a hash
115
+ class User < ActiveRecord::Base
116
+ extend RailsAttrEnum
117
+
118
+ attr_enum :role, {
119
+ admin: 'Admin Role',
120
+ author: 'Author Role',
121
+ editor: 'Editor Role',
122
+ user: 'User Role'
123
+ }
124
+ end
125
+
126
+ User::Role::ADMIN_LABEL == 'Admin Role'
127
+ User::Role::AUTHOR_LABEL == 'Author Role'
128
+ User::Role::EDITOR_LABEL == 'Editor Role'
129
+ User::Role::USER_LABEL == 'User Role'
130
+
131
+ # With a block
132
+ class User < ActiveRecord::Base
133
+ extend RailsAttrEnum
134
+
135
+ attr_enum :role do
136
+ add :admin
137
+ add author: 'Author Role'
138
+ add editor: 'Editor Role'
139
+ add :user
140
+ end
141
+ end
142
+
143
+ User::Role::ADMIN_LABEL == 'Admin'
144
+ User::Role::AUTHOR_LABEL == 'Author Role'
145
+ User::Role::EDITOR_LABEL == 'Editor Role'
146
+ User::Role::USER_LABEL == 'User'
147
+
148
+ ### Mix-and-match
149
+ If you need to be very specific about values and labels, then you can specify
150
+ both at the same time too.
151
+
152
+ class User < ActiveRecord::Base
153
+ extend RailsAttrEnum
154
+
155
+ attr_enum :role, { admin: { label: 'Admin Role', value: 1 } },
156
+ { author: 'Author Role' },
157
+ { editor: 42 },
158
+ :user
159
+ end
160
+
161
+ User::Role::ADMIN == 1
162
+ User::Role::ADMIN_LABEL == 'Admin Role'
163
+ User::Role::AUTHOR == 0
164
+ User::Role::AUTHOR_LABEL == 'Author Role'
165
+ User::Role::EDITOR == 42
166
+ User::Role::EDITOR_LABEL == 'Editor'
167
+ User::Role::USER == 2
168
+ User::Role::USER_LABEL == 'User'
@@ -33,11 +33,11 @@ module RailsAttrEnum
33
33
  @_attr_enums ||= {}
34
34
 
35
35
  unless column_names.include?(attr.name)
36
- raise "Invalid attribute name #{attr_name_s}"
36
+ raise "Invalid attribute name #{attr.name}"
37
37
  end
38
38
 
39
39
  if @_attr_enums.any? { |(_, enm)| enm.attr_name == attr.name }
40
- raise "Already defined enum for '#{attr_name_s}'"
40
+ raise "Already defined enum for '#{attr.name}'"
41
41
  end
42
42
 
43
43
  @_attr_enums[attr.name.to_sym] = Module.new { include Enum }.tap do |mod|
@@ -1,3 +1,3 @@
1
1
  module RailsAttrEnum
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,25 +1,2 @@
1
1
  class User < ActiveRecord::Base
2
- extend RailsAttrEnum
3
-
4
- # attr_enum :role, :admin, :author, :editor, :user
5
- # attr_enum :role, :admin, { author: 2 }, :editor, { user: 10 }
6
-
7
- attr_enum :role, { admin: { value: 10, label: 'ADMIN' } },
8
- { author: 'Author Role' },
9
- :editor,
10
- { user: { label: 'ID10T', value: 45 } }
11
-
12
- # attr_enum :role, {
13
- # admin: 'ADMIN',
14
- # author: 'AUTHOR',
15
- # editor: 'EDITOR',
16
- # user: 'ID10T'
17
- # }
18
-
19
- # attr_enum :role do
20
- # add admin: 1
21
- # add :author
22
- # add editor: 42
23
- # add user: 7
24
- # end
25
2
  end
Binary file
@@ -0,0 +1,268 @@
1
+  (2.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
2
+  (1.4ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
3
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
4
+ Migrating to CreateUsers (20131023212242)
5
+  (0.1ms) begin transaction
6
+  (0.4ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
7
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20131023212242"]]
8
+  (1.0ms) commit transaction
9
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
10
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
11
+  (0.1ms) begin transaction
12
+  (0.2ms) rollback transaction
13
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
14
+  (0.2ms) begin transaction
15
+  (0.2ms) rollback transaction
16
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
17
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
18
+  (0.1ms) begin transaction
19
+  (0.1ms) rollback transaction
20
+  (0.1ms) begin transaction
21
+  (0.1ms) rollback transaction
22
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
23
+  (0.1ms) begin transaction
24
+  (0.1ms) rollback transaction
25
+  (0.1ms) begin transaction
26
+  (0.1ms) rollback transaction
27
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
28
+  (0.1ms) begin transaction
29
+  (0.1ms) rollback transaction
30
+  (0.1ms) begin transaction
31
+  (0.2ms) rollback transaction
32
+  (0.2ms) begin transaction
33
+  (0.1ms) rollback transaction
34
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
35
+  (0.1ms) begin transaction
36
+  (0.1ms) rollback transaction
37
+  (0.1ms) begin transaction
38
+  (0.1ms) rollback transaction
39
+  (0.1ms) begin transaction
40
+  (0.1ms) rollback transaction
41
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
42
+  (0.1ms) begin transaction
43
+  (0.1ms) rollback transaction
44
+  (0.1ms) begin transaction
45
+  (0.1ms) rollback transaction
46
+  (0.1ms) begin transaction
47
+  (0.1ms) rollback transaction
48
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
49
+  (0.1ms) begin transaction
50
+  (0.0ms) rollback transaction
51
+  (0.1ms) begin transaction
52
+  (0.1ms) rollback transaction
53
+  (0.1ms) begin transaction
54
+  (0.0ms) rollback transaction
55
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
56
+  (0.1ms) begin transaction
57
+  (0.1ms) rollback transaction
58
+  (0.1ms) begin transaction
59
+  (0.1ms) rollback transaction
60
+  (0.1ms) begin transaction
61
+  (0.1ms) rollback transaction
62
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
63
+  (0.1ms) begin transaction
64
+  (0.1ms) rollback transaction
65
+  (0.1ms) begin transaction
66
+  (0.1ms) rollback transaction
67
+  (0.1ms) begin transaction
68
+  (0.1ms) rollback transaction
69
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
70
+  (0.1ms) begin transaction
71
+  (0.1ms) rollback transaction
72
+  (0.1ms) begin transaction
73
+  (0.1ms) rollback transaction
74
+  (0.1ms) begin transaction
75
+  (0.1ms) rollback transaction
76
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
77
+  (0.1ms) begin transaction
78
+  (0.1ms) rollback transaction
79
+  (0.1ms) begin transaction
80
+  (0.1ms) rollback transaction
81
+  (0.1ms) begin transaction
82
+  (0.1ms) rollback transaction
83
+  (0.1ms) begin transaction
84
+  (0.1ms) rollback transaction
85
+  (0.1ms) begin transaction
86
+  (0.1ms) rollback transaction
87
+  (0.1ms) begin transaction
88
+  (0.1ms) rollback transaction
89
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
90
+  (0.1ms) begin transaction
91
+  (0.1ms) rollback transaction
92
+  (0.1ms) begin transaction
93
+  (0.1ms) rollback transaction
94
+  (0.1ms) begin transaction
95
+  (0.1ms) rollback transaction
96
+  (0.1ms) begin transaction
97
+  (0.1ms) rollback transaction
98
+  (0.1ms) begin transaction
99
+  (0.1ms) rollback transaction
100
+  (0.1ms) begin transaction
101
+  (0.1ms) rollback transaction
102
+  (0.0ms) begin transaction
103
+  (0.1ms) rollback transaction
104
+  (0.1ms) begin transaction
105
+  (0.1ms) rollback transaction
106
+  (0.1ms) begin transaction
107
+  (0.1ms) rollback transaction
108
+  (0.1ms) begin transaction
109
+  (0.1ms) rollback transaction
110
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
111
+  (0.1ms) begin transaction
112
+  (0.1ms) rollback transaction
113
+  (0.1ms) begin transaction
114
+  (0.1ms) rollback transaction
115
+  (0.1ms) begin transaction
116
+  (0.1ms) rollback transaction
117
+  (0.1ms) begin transaction
118
+  (0.1ms) rollback transaction
119
+  (0.1ms) begin transaction
120
+  (0.1ms) rollback transaction
121
+  (0.1ms) begin transaction
122
+  (0.1ms) rollback transaction
123
+  (0.1ms) begin transaction
124
+  (0.1ms) rollback transaction
125
+  (0.1ms) begin transaction
126
+  (0.1ms) rollback transaction
127
+  (0.1ms) begin transaction
128
+  (0.1ms) rollback transaction
129
+  (0.1ms) begin transaction
130
+  (0.1ms) rollback transaction
131
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
132
+  (0.1ms) begin transaction
133
+  (0.1ms) rollback transaction
134
+  (0.1ms) begin transaction
135
+  (0.1ms) rollback transaction
136
+  (0.1ms) begin transaction
137
+  (0.1ms) rollback transaction
138
+  (0.1ms) begin transaction
139
+  (0.1ms) rollback transaction
140
+  (0.1ms) begin transaction
141
+  (0.1ms) rollback transaction
142
+  (0.1ms) begin transaction
143
+  (0.1ms) rollback transaction
144
+  (0.1ms) begin transaction
145
+  (0.1ms) rollback transaction
146
+  (0.1ms) begin transaction
147
+  (0.1ms) rollback transaction
148
+  (0.1ms) begin transaction
149
+  (0.1ms) rollback transaction
150
+  (0.1ms) begin transaction
151
+  (0.1ms) rollback transaction
152
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
153
+  (0.1ms) begin transaction
154
+  (0.1ms) rollback transaction
155
+  (0.1ms) begin transaction
156
+  (0.1ms) rollback transaction
157
+  (0.1ms) begin transaction
158
+  (0.1ms) rollback transaction
159
+  (0.1ms) begin transaction
160
+  (0.1ms) rollback transaction
161
+  (0.1ms) begin transaction
162
+  (0.1ms) rollback transaction
163
+  (0.1ms) begin transaction
164
+  (0.1ms) rollback transaction
165
+  (0.1ms) begin transaction
166
+  (0.1ms) rollback transaction
167
+  (0.1ms) begin transaction
168
+  (0.1ms) rollback transaction
169
+  (0.1ms) begin transaction
170
+  (0.1ms) rollback transaction
171
+  (0.1ms) begin transaction
172
+  (0.1ms) rollback transaction
173
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
174
+  (0.1ms) begin transaction
175
+  (0.1ms) rollback transaction
176
+  (0.1ms) begin transaction
177
+  (0.1ms) rollback transaction
178
+  (0.1ms) begin transaction
179
+  (0.1ms) rollback transaction
180
+  (0.1ms) begin transaction
181
+  (0.1ms) rollback transaction
182
+  (0.1ms) begin transaction
183
+  (0.1ms) rollback transaction
184
+  (0.1ms) begin transaction
185
+  (0.1ms) rollback transaction
186
+  (0.1ms) begin transaction
187
+  (0.1ms) rollback transaction
188
+  (0.1ms) begin transaction
189
+  (0.1ms) rollback transaction
190
+  (0.1ms) begin transaction
191
+  (0.1ms) rollback transaction
192
+  (0.1ms) begin transaction
193
+  (0.1ms) rollback transaction
194
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
195
+  (0.1ms) begin transaction
196
+  (0.1ms) rollback transaction
197
+  (0.1ms) begin transaction
198
+  (0.1ms) rollback transaction
199
+  (0.1ms) begin transaction
200
+  (0.1ms) rollback transaction
201
+  (0.1ms) begin transaction
202
+  (0.1ms) rollback transaction
203
+  (0.1ms) begin transaction
204
+  (0.1ms) rollback transaction
205
+  (0.1ms) begin transaction
206
+  (0.1ms) rollback transaction
207
+  (0.1ms) begin transaction
208
+  (0.2ms) rollback transaction
209
+  (0.1ms) begin transaction
210
+  (0.1ms) rollback transaction
211
+  (0.1ms) begin transaction
212
+  (0.1ms) rollback transaction
213
+  (0.1ms) begin transaction
214
+  (0.1ms) rollback transaction
215
+  (0.1ms) begin transaction
216
+  (0.1ms) rollback transaction
217
+  (0.1ms) begin transaction
218
+  (0.1ms) rollback transaction
219
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
220
+  (0.1ms) begin transaction
221
+  (0.1ms) rollback transaction
222
+  (0.1ms) begin transaction
223
+  (0.1ms) rollback transaction
224
+  (0.1ms) begin transaction
225
+  (0.1ms) rollback transaction
226
+  (0.1ms) begin transaction
227
+  (0.1ms) rollback transaction
228
+  (0.1ms) begin transaction
229
+  (0.1ms) rollback transaction
230
+  (0.1ms) begin transaction
231
+  (0.1ms) rollback transaction
232
+  (0.1ms) begin transaction
233
+  (0.1ms) rollback transaction
234
+  (0.1ms) begin transaction
235
+  (0.1ms) rollback transaction
236
+  (0.1ms) begin transaction
237
+  (0.1ms) rollback transaction
238
+  (0.1ms) begin transaction
239
+  (0.1ms) rollback transaction
240
+  (0.1ms) begin transaction
241
+  (0.1ms) rollback transaction
242
+  (0.1ms) begin transaction
243
+  (0.1ms) rollback transaction
244
+ ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
245
+  (0.1ms) begin transaction
246
+  (0.2ms) rollback transaction
247
+  (0.1ms) begin transaction
248
+  (0.1ms) rollback transaction
249
+  (0.1ms) begin transaction
250
+  (0.1ms) rollback transaction
251
+  (0.1ms) begin transaction
252
+  (0.1ms) rollback transaction
253
+  (0.1ms) begin transaction
254
+  (0.1ms) rollback transaction
255
+  (0.1ms) begin transaction
256
+  (0.1ms) rollback transaction
257
+  (0.1ms) begin transaction
258
+  (0.1ms) rollback transaction
259
+  (0.1ms) begin transaction
260
+  (0.1ms) rollback transaction
261
+  (0.1ms) begin transaction
262
+  (0.1ms) rollback transaction
263
+  (0.1ms) begin transaction
264
+  (0.1ms) rollback transaction
265
+  (0.1ms) begin transaction
266
+  (0.1ms) rollback transaction
267
+  (0.1ms) begin transaction
268
+  (0.1ms) rollback transaction
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+ shared_examples 'it has the enum module' do
5
+ it 'has the enum module' do
6
+ expect(User::Role).to be_a Module
7
+ end
8
+ end
9
+
10
+ shared_examples 'it sets default values' do
11
+ it 'sets the default values' do
12
+ expect(User::Role::ADMIN).to eq(0)
13
+ expect(User::Role::AUTHOR).to eq(1)
14
+ expect(User::Role::EDITOR).to eq(2)
15
+ expect(User::Role::USER).to eq(3)
16
+ end
17
+
18
+ it 'sets the right value for an instance' do
19
+ expect(u_admin.role).to eq(0)
20
+ expect(u_author.role).to eq(1)
21
+ expect(u_editor.role).to eq(2)
22
+ expect(u_user.role).to eq(3)
23
+ end
24
+ end
25
+
26
+ shared_examples 'it sets default labels' do
27
+ it 'sets the default labels' do
28
+ expect(User::Role::ADMIN_LABEL).to eq('Admin')
29
+ expect(User::Role::AUTHOR_LABEL).to eq('Author')
30
+ expect(User::Role::EDITOR_LABEL).to eq('Editor')
31
+ expect(User::Role::USER_LABEL).to eq('User')
32
+ end
33
+ end
34
+
35
+ shared_examples 'it sets the appropriate values' do |admin_value, author_value, editor_value, user_value|
36
+ it 'sets the appropriate values' do
37
+ expect(User::Role::ADMIN).to eq(admin_value)
38
+ expect(User::Role::AUTHOR).to eq(author_value)
39
+ expect(User::Role::EDITOR).to eq(editor_value)
40
+ expect(User::Role::USER).to eq(user_value)
41
+ end
42
+
43
+ it 'sets the right value for an instance' do
44
+ expect(u_admin.role).to eq(admin_value)
45
+ expect(u_author.role).to eq(author_value)
46
+ expect(u_editor.role).to eq(editor_value)
47
+ expect(u_user.role).to eq(user_value)
48
+ end
49
+ end
50
+
51
+ let(:u_admin) { User.new(role: User::Role::ADMIN) }
52
+ let(:u_author) { User.new(role: User::Role::AUTHOR) }
53
+ let(:u_editor) { User.new(role: User::Role::EDITOR) }
54
+ let(:u_user) { User.new(role: User::Role::USER) }
55
+
56
+ def clear_user
57
+ if User.const_defined?(:Role)
58
+ User.send(:remove_const, :Role)
59
+ end
60
+
61
+ if User.instance_variable_defined?(:@_attr_enums)
62
+ User.remove_instance_variable(:@_attr_enums)
63
+ end
64
+ end
65
+
66
+ context 'when passing all symbols' do
67
+ before :each do
68
+ clear_user
69
+
70
+ User.class_eval do
71
+ extend RailsAttrEnum
72
+ attr_enum :role, :admin, :author, :editor, :user
73
+ end
74
+ end
75
+
76
+ it_behaves_like 'it has the enum module'
77
+ it_behaves_like 'it sets default values'
78
+ it_behaves_like 'it sets default labels'
79
+ end
80
+
81
+ context 'when specifying values' do
82
+ context 'with a mix of symbols and hashes and a symbol is first' do
83
+ before :each do
84
+ clear_user
85
+
86
+ User.class_eval do
87
+ extend RailsAttrEnum
88
+ attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
89
+ end
90
+ end
91
+
92
+ it_behaves_like 'it has the enum module'
93
+ it_behaves_like 'it sets the appropriate values', 0, 12, 1, 42
94
+ it_behaves_like 'it sets default labels'
95
+ end
96
+
97
+ context 'with a hash' do
98
+ before :each do
99
+ clear_user
100
+
101
+ User.class_eval do
102
+ extend RailsAttrEnum
103
+ attr_enum :role, admin: 1, author: 2, editor: 4, user: 8
104
+ end
105
+ end
106
+
107
+ it_behaves_like 'it has the enum module'
108
+ it_behaves_like 'it sets the appropriate values', 1, 2, 4, 8
109
+ it_behaves_like 'it sets default labels'
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,42 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require File.expand_path("../../config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'rspec/autorun'
6
+
7
+ # Requires supporting ruby files with custom matchers and macros, etc,
8
+ # in spec/support/ and its subdirectories.
9
+ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
10
+
11
+ # Checks for pending migrations before tests are run.
12
+ # If you are not using ActiveRecord, you can remove this line.
13
+ ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
14
+
15
+ RSpec.configure do |config|
16
+ # ## Mock Framework
17
+ #
18
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
19
+ #
20
+ # config.mock_with :mocha
21
+ # config.mock_with :flexmock
22
+ # config.mock_with :rr
23
+
24
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
25
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
26
+
27
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
28
+ # examples within a transaction, remove the following line or assign false
29
+ # instead of true.
30
+ config.use_transactional_fixtures = true
31
+
32
+ # If true, the base class of anonymous controllers will be inferred
33
+ # automatically. This will be the default behavior in future versions of
34
+ # rspec-rails.
35
+ config.infer_base_class_for_anonymous_controllers = false
36
+
37
+ # Run specs in random order to surface order dependencies. If you find an
38
+ # order dependency and want to debug it, you can fix the order by providing
39
+ # the seed, which is printed after each run.
40
+ # --seed 1234
41
+ config.order = "random"
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_attr_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Fairbank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-23 00:00:00.000000000 Z
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Create enum values for a Rails model's attribute
41
+ description: Create enums for Rails model attributes like enums in C languages
42
42
  email:
43
43
  - elpapapollo@gmail.com
44
44
  executables: []
@@ -55,7 +55,7 @@ files:
55
55
  - lib/tasks/rails_attr_enum_tasks.rake
56
56
  - LICENSE
57
57
  - Rakefile
58
- - README.rdoc
58
+ - README.md
59
59
  - test/dummy/app/assets/javascripts/application.js
60
60
  - test/dummy/app/assets/stylesheets/application.css
61
61
  - test/dummy/app/controllers/application_controller.rb
@@ -85,19 +85,20 @@ files:
85
85
  - test/dummy/db/development.sqlite3
86
86
  - test/dummy/db/migrate/20131023212242_create_users.rb
87
87
  - test/dummy/db/schema.rb
88
+ - test/dummy/db/test.sqlite3
88
89
  - test/dummy/log/development.log
90
+ - test/dummy/log/test.log
89
91
  - test/dummy/public/404.html
90
92
  - test/dummy/public/422.html
91
93
  - test/dummy/public/500.html
92
94
  - test/dummy/public/favicon.ico
93
95
  - test/dummy/Rakefile
94
96
  - test/dummy/README.rdoc
95
- - test/dummy/test/fixtures/users.yml
96
- - test/dummy/test/models/user_test.rb
97
- - test/rails_attr_enum_test.rb
98
- - test/test_helper.rb
97
+ - test/dummy/spec/models/user_spec.rb
98
+ - test/dummy/spec/spec_helper.rb
99
99
  homepage: https://github.com/jfairbank/rails_attr_enum
100
- licenses: []
100
+ licenses:
101
+ - MIT
101
102
  metadata: {}
102
103
  post_install_message:
103
104
  rdoc_options: []
@@ -149,14 +150,14 @@ test_files:
149
150
  - test/dummy/db/development.sqlite3
150
151
  - test/dummy/db/migrate/20131023212242_create_users.rb
151
152
  - test/dummy/db/schema.rb
153
+ - test/dummy/db/test.sqlite3
152
154
  - test/dummy/log/development.log
155
+ - test/dummy/log/test.log
153
156
  - test/dummy/public/404.html
154
157
  - test/dummy/public/422.html
155
158
  - test/dummy/public/500.html
156
159
  - test/dummy/public/favicon.ico
157
160
  - test/dummy/Rakefile
158
161
  - test/dummy/README.rdoc
159
- - test/dummy/test/fixtures/users.yml
160
- - test/dummy/test/models/user_test.rb
161
- - test/rails_attr_enum_test.rb
162
- - test/test_helper.rb
162
+ - test/dummy/spec/models/user_spec.rb
163
+ - test/dummy/spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = RailsAttrEnum
2
-
3
- This project rocks and uses MIT-LICENSE.
@@ -1,9 +0,0 @@
1
- # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
-
3
- one:
4
- name: MyString
5
- role: 1
6
-
7
- two:
8
- name: MyString
9
- role: 1
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class UserTest < ActiveSupport::TestCase
4
- # test "the truth" do
5
- # assert true
6
- # end
7
- end
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class RailsAttrEnumTest < ActiveSupport::TestCase
4
- test "truth" do
5
- assert_kind_of Module, RailsAttrEnum
6
- end
7
- end
data/test/test_helper.rb DELETED
@@ -1,15 +0,0 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
- require "rails/test_help"
6
-
7
- Rails.backtrace_cleaner.remove_silencers!
8
-
9
- # Load support files
10
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
-
12
- # Load fixtures from the engine
13
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
14
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
15
- end