classy_enum 3.2.1 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +6 -0
- data/README.md +53 -48
- data/gemfiles/Gemfile.rails-4.0.x +1 -1
- data/lib/classy_enum/version.rb +1 -1
- data/lib/generators/classy_enum/classy_enum_generator.rb +1 -0
- data/lib/generators/rspec/classy_enum_generator.rb +17 -0
- data/lib/generators/rspec/templates/enum_spec.rb +6 -0
- data/lib/generators/test_unit/classy_enum_generator.rb +17 -0
- data/lib/generators/test_unit/templates/enum_test.rb +8 -0
- metadata +21 -35
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e797e8a533bf5f74cca3a2b6c1ac10fb2de623bd
|
4
|
+
data.tar.gz: 7e7049d175554faa74facd0b5a95108131a8f4cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 41c72e09e4a3ba245f58139d5f6f40e22ade540b37947a1c3639d907015c2c5b6b46fbb27b25fe8576e969e0830a0c01e7ca495a4b0ac2c3b161e2215a947ca3
|
7
|
+
data.tar.gz: 1f28e0114ab0c8f06efd71963dcd788b65ab9686230bc0cc578b2e61b774599ab3f9f59189d95dcc6ec3ab7fc68513a5774d328099cb7d3bd1dfd4ba079ab090
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# ClassyEnum Changelog
|
2
2
|
|
3
|
+
## 3.3.0
|
4
|
+
|
5
|
+
* Extends the existing generator to create boilerplate spec/test files.
|
6
|
+
For Rspec, these files are placed in spec/enums/, for TestUnit, they
|
7
|
+
are placed in test/unit/enums/
|
8
|
+
|
3
9
|
## 3.2.1
|
4
10
|
|
5
11
|
* Better support for using `default` and `allow_*` options together
|
data/README.md
CHANGED
@@ -5,29 +5,16 @@
|
|
5
5
|
[![Code Climate](https://codeclimate.com/github/beerlington/classy_enum.png)](https://codeclimate.com/github/beerlington/classy_enum)
|
6
6
|
[![Dependency Status](https://gemnasium.com/beerlington/classy_enum.png)](https://gemnasium.com/beerlington/classy_enum)
|
7
7
|
|
8
|
-
ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality to
|
8
|
+
ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality to Active Record attributes.
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
* [Example Usage](#example-usage)
|
13
|
-
* [Internationalization](#internationalization)
|
14
|
-
* [Using Enum as a Collection](#using-enum-as-a-collection)
|
15
|
-
* [Default Enum Value](#default-enum-value)
|
16
|
-
* [Reference to Owning Object](#back-reference-to-owning-object)
|
17
|
-
* [Serializing as JSON](#serializing-as-json)
|
18
|
-
* [Special Cases](#special-cases)
|
19
|
-
* [Built-in Model Validation](#model-validation)
|
20
|
-
* [Formtastic Support](#formtastic-support)
|
10
|
+
This README is also available in a [user-friendly DocumentUp format](http://beerlington.com/classy_enum/).
|
21
11
|
|
22
12
|
## Rails & Ruby Versions Supported
|
23
13
|
|
24
|
-
*Rails:* 3.0.x - 4.0.0.
|
14
|
+
*Rails:* 3.0.x - 4.0.0.rc2
|
25
15
|
|
26
16
|
*Ruby:* 1.8.7, 1.9.2, 1.9.3 and 2.0.0
|
27
17
|
|
28
|
-
If you need support for Rails 2.3.x, please install [version 0.9.1](https://rubygems.org/gems/classy_enum/versions/0.9.1).
|
29
|
-
Note: This branch is no longer maintained and will not get bug fixes or new features.
|
30
|
-
|
31
18
|
## Installation
|
32
19
|
|
33
20
|
The gem is hosted at [rubygems.org](https://rubygems.org/gems/classy_enum)
|
@@ -36,9 +23,9 @@ The gem is hosted at [rubygems.org](https://rubygems.org/gems/classy_enum)
|
|
36
23
|
|
37
24
|
See the [wiki](https://github.com/beerlington/classy_enum/wiki/Upgrading) for notes about upgrading from previous versions.
|
38
25
|
|
39
|
-
## Example Usage
|
26
|
+
## Getting Started & Example Usage
|
40
27
|
|
41
|
-
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an
|
28
|
+
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an Active Record model called `Alarm` with an attribute called `priority`. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested.
|
42
29
|
|
43
30
|
### 1. Generate the Enum
|
44
31
|
|
@@ -64,7 +51,7 @@ class Priority::High < Priority
|
|
64
51
|
end
|
65
52
|
```
|
66
53
|
|
67
|
-
The class order
|
54
|
+
NOTE: The class order is important because it defines the enum member ordering as well as additional ClassyEnum behavior described below.
|
68
55
|
|
69
56
|
### 2. Customize the Enum
|
70
57
|
|
@@ -94,18 +81,18 @@ class Priority::High < Priority
|
|
94
81
|
end
|
95
82
|
```
|
96
83
|
|
97
|
-
### 3. Setup the
|
84
|
+
### 3. Setup the Active Record model
|
98
85
|
|
99
|
-
My
|
86
|
+
My Active Record Alarm model needs a text field that will store a string representing the enum member. An example model schema might look something like:
|
100
87
|
|
101
88
|
```ruby
|
102
|
-
create_table "alarms", :
|
89
|
+
create_table "alarms", force: true do |t|
|
103
90
|
t.string "priority"
|
104
91
|
t.boolean "enabled"
|
105
92
|
end
|
106
93
|
```
|
107
94
|
|
108
|
-
|
95
|
+
NOTE: Alternatively, you may use an enum type if your database supports it. See
|
109
96
|
[this issue](https://github.com/beerlington/classy_enum/issues/12) for more information.
|
110
97
|
|
111
98
|
Then in my model I've added a line that calls `classy_enum_attr` with a single argument representing the enum I want to associate with my model. I am also delegating the `#send_email?` method to my Priority enum class.
|
@@ -114,14 +101,14 @@ Then in my model I've added a line that calls `classy_enum_attr` with a single a
|
|
114
101
|
class Alarm < ActiveRecord::Base
|
115
102
|
classy_enum_attr :priority
|
116
103
|
|
117
|
-
delegate :send_email?, :
|
104
|
+
delegate :send_email?, to: :priority
|
118
105
|
end
|
119
106
|
```
|
120
107
|
|
121
108
|
With this setup, I can now do the following:
|
122
109
|
|
123
110
|
```ruby
|
124
|
-
@alarm = Alarm.create(:
|
111
|
+
@alarm = Alarm.create(priority: :medium)
|
125
112
|
|
126
113
|
@alarm.priority # => Priority::Medium
|
127
114
|
@alarm.priority.medium? # => true
|
@@ -171,7 +158,7 @@ I18n.locale = :es
|
|
171
158
|
ClassyEnum::Base extends the [Enumerable module](http://ruby-doc.org/core-1.9.3/Enumerable.html)
|
172
159
|
which provides several traversal and searching methods. This can
|
173
160
|
be useful for situations where you are working with the collection,
|
174
|
-
as opposed to the attributes on an
|
161
|
+
as opposed to the attributes on an Active Record object.
|
175
162
|
|
176
163
|
```ruby
|
177
164
|
# Find the priority based on string or symbol:
|
@@ -193,7 +180,7 @@ end
|
|
193
180
|
|
194
181
|
## Default Enum Value
|
195
182
|
|
196
|
-
As with any
|
183
|
+
As with any Active Record attribute, default values can be specified in
|
197
184
|
the database table and will propagate to new instances. However, there
|
198
185
|
may be times when you can't or don't want to set the default value in
|
199
186
|
the database. For these occasions, a default value can be specified like
|
@@ -201,7 +188,7 @@ so:
|
|
201
188
|
|
202
189
|
```ruby
|
203
190
|
class Alarm < ActiveRecord::Base
|
204
|
-
classy_enum_attr :priority, :
|
191
|
+
classy_enum_attr :priority, default: 'medium'
|
205
192
|
end
|
206
193
|
|
207
194
|
Alarm.new.priority # => Priority::Medium
|
@@ -213,16 +200,16 @@ runtime.
|
|
213
200
|
|
214
201
|
```ruby
|
215
202
|
class Alarm < ActiveRecord::Base
|
216
|
-
classy_enum_attr :priority, :
|
203
|
+
classy_enum_attr :priority, default: ->(enum){ enum.max }
|
217
204
|
end
|
218
205
|
|
219
206
|
Alarm.new.priority # => Priority::High
|
220
207
|
```
|
221
208
|
|
222
|
-
## Back
|
209
|
+
## Back Reference to Owning Object
|
223
210
|
|
224
211
|
In some cases you may want an enum class to reference the owning object
|
225
|
-
(an instance of the
|
212
|
+
(an instance of the Active Record model). Think of it as a `belongs_to`
|
226
213
|
relationship, where the enum belongs to the model.
|
227
214
|
|
228
215
|
By default, the back reference can be called using `#owner`.
|
@@ -263,7 +250,7 @@ end
|
|
263
250
|
In the above examples, high priority alarms are only emailed if the owning alarm is enabled.
|
264
251
|
|
265
252
|
```ruby
|
266
|
-
@alarm = Alarm.create(:
|
253
|
+
@alarm = Alarm.create(priority: :high, enabled: true)
|
267
254
|
|
268
255
|
# Should this alarm send an email?
|
269
256
|
@alarm.send_email? # => true
|
@@ -276,18 +263,18 @@ In the above examples, high priority alarms are only emailed if the owning alarm
|
|
276
263
|
By default, the enum will be serialized as a string representing the value:
|
277
264
|
|
278
265
|
```ruby
|
279
|
-
@alarm = Alarm.create(:
|
266
|
+
@alarm = Alarm.create(priority: :high, enabled: true)
|
280
267
|
@alarm.to_json.should == "{\"alarm\":{\"priority\":\"high\"}}"
|
281
268
|
```
|
282
269
|
|
283
|
-
This behavior can be overridden by using the
|
270
|
+
This behavior can be overridden by using the `serialize_as_json: true` option in your Active Record model:
|
284
271
|
|
285
272
|
```ruby
|
286
273
|
class Alarm < ActiveRecord::Base
|
287
|
-
classy_enum_attr :priority, :
|
274
|
+
classy_enum_attr :priority, serialize_as_json: true
|
288
275
|
end
|
289
276
|
|
290
|
-
@alarm = Alarm.create(:
|
277
|
+
@alarm = Alarm.create(priority: :high, enabled: true)
|
291
278
|
@alarm.to_json.should == "{\"alarm\":{\"priority\":{}}}"
|
292
279
|
```
|
293
280
|
|
@@ -297,21 +284,21 @@ What if your enum class name is not the same as your model's attribute name? No
|
|
297
284
|
|
298
285
|
```ruby
|
299
286
|
class Alarm < ActiveRecord::Base
|
300
|
-
classy_enum_attr :alarm_priority, :
|
287
|
+
classy_enum_attr :alarm_priority, enum: 'Priority'
|
301
288
|
end
|
302
289
|
|
303
|
-
@alarm = Alarm.create(:
|
290
|
+
@alarm = Alarm.create(alarm_priority: :medium)
|
304
291
|
@alarm.alarm_priority # => Priority::Medium
|
305
292
|
```
|
306
293
|
|
307
294
|
## Model Validation
|
308
295
|
|
309
|
-
An
|
296
|
+
An Active Record validator `validates_inclusion_of :field, in: ENUM` is automatically added to your model when you use `classy_enum_attr`.
|
310
297
|
|
311
298
|
If your enum only has members low, medium, and high, then the following validation behavior would be expected:
|
312
299
|
|
313
300
|
```ruby
|
314
|
-
@alarm = Alarm.new(:
|
301
|
+
@alarm = Alarm.new(priority: :really_high)
|
315
302
|
@alarm.valid? # => false
|
316
303
|
@alarm.priority = :high
|
317
304
|
@alarm.valid? # => true
|
@@ -321,22 +308,40 @@ To allow nil or blank values, you can pass in `:allow_nil` and `:allow_blank` as
|
|
321
308
|
|
322
309
|
```ruby
|
323
310
|
class Alarm < ActiveRecord::Base
|
324
|
-
classy_enum_attr :priority, :
|
311
|
+
classy_enum_attr :priority, allow_nil: true
|
325
312
|
end
|
326
313
|
|
327
|
-
@alarm = Alarm.new(:
|
314
|
+
@alarm = Alarm.new(priority: nil)
|
328
315
|
@alarm.valid? # => true
|
329
316
|
```
|
330
317
|
|
331
|
-
##
|
318
|
+
## Form Usage
|
332
319
|
|
333
|
-
|
334
|
-
|
335
|
-
|
320
|
+
ClassyEnum includes a `select_options` helper method to generate an array of enum options
|
321
|
+
that can be used by Rails' form builders such as SimpleForm and
|
322
|
+
Formtastic.
|
323
|
+
|
324
|
+
```erb
|
325
|
+
# SimpleForm
|
336
326
|
|
337
|
-
|
327
|
+
<%= simple_form_for @alarm do |f| %>
|
328
|
+
<%= f.input :priority, as: :select, collection: Priority.select_options %>
|
329
|
+
<%= f.button :submit %>
|
330
|
+
<% end %>
|
331
|
+
```
|
338
332
|
|
339
|
-
|
333
|
+
```erb
|
334
|
+
# Formtastic
|
335
|
+
|
336
|
+
<%= semantic_form_for @alarm do |f| %>
|
337
|
+
<%= f.input :priority, as: :select, collection: Priority.select_options %>
|
338
|
+
<%= f.button :submit %>
|
339
|
+
<% end %>
|
340
|
+
```
|
341
|
+
|
342
|
+
Built-in Formtastic support has been removed as of ClassyEnum 2.0. It is still
|
343
|
+
available but needs to be enabled manually. To enable support visit
|
344
|
+
[the wiki](https://github.com/beerlington/classy_enum/wiki/Formtastic-Support)
|
340
345
|
|
341
346
|
## Copyright
|
342
347
|
|
data/lib/classy_enum/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rspec
|
2
|
+
module Generators
|
3
|
+
class ClassyEnumGenerator < Rails::Generators::NamedBase
|
4
|
+
desc "Generate a ClassyEnum spec in spec/enums/"
|
5
|
+
|
6
|
+
argument :name, :type => :string, :required => true, :banner => 'EnumName'
|
7
|
+
argument :values, :type => :array, :default => [], :banner => 'value1 value2 value3 etc...'
|
8
|
+
|
9
|
+
source_root File.expand_path("../templates", __FILE__)
|
10
|
+
|
11
|
+
def copy_files # :nodoc:
|
12
|
+
empty_directory 'spec/enums'
|
13
|
+
template "enum_spec.rb", "spec/enums/#{file_name}_spec.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module TestUnit
|
2
|
+
module Generators
|
3
|
+
class ClassyEnumGenerator < Rails::Generators::NamedBase
|
4
|
+
desc "Generate a ClassyEnum test in test/enums/"
|
5
|
+
|
6
|
+
argument :name, :type => :string, :required => true, :banner => 'EnumName'
|
7
|
+
argument :values, :type => :array, :default => [], :banner => 'value1 value2 value3 etc...'
|
8
|
+
|
9
|
+
source_root File.expand_path("../templates", __FILE__)
|
10
|
+
|
11
|
+
def copy_files # :nodoc:
|
12
|
+
empty_directory 'test/unit/enums'
|
13
|
+
template "enum_test.rb", "test/unit/enums/#{file_name}_test.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,94 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
5
|
-
prerelease:
|
4
|
+
version: 3.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Peter Brown
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-06-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '3.0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec-rails
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '2.11'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '2.11'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: sqlite3
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '1.3'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '1.3'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: json
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '1.6'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '1.6'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: debugger
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - '>='
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: A utility that adds class based enum functionality to ActiveRecord attributes
|
@@ -121,6 +110,10 @@ files:
|
|
121
110
|
- lib/classy_enum/version.rb
|
122
111
|
- lib/generators/classy_enum/classy_enum_generator.rb
|
123
112
|
- lib/generators/classy_enum/templates/enum.rb
|
113
|
+
- lib/generators/rspec/classy_enum_generator.rb
|
114
|
+
- lib/generators/rspec/templates/enum_spec.rb
|
115
|
+
- lib/generators/test_unit/classy_enum_generator.rb
|
116
|
+
- lib/generators/test_unit/templates/enum_test.rb
|
124
117
|
- spec/classy_enum/active_record_spec.rb
|
125
118
|
- spec/classy_enum/base_spec.rb
|
126
119
|
- spec/classy_enum/collection_spec.rb
|
@@ -132,33 +125,26 @@ files:
|
|
132
125
|
- spec/spec_helper.rb
|
133
126
|
homepage: http://github.com/beerlington/classy_enum
|
134
127
|
licenses: []
|
128
|
+
metadata: {}
|
135
129
|
post_install_message:
|
136
130
|
rdoc_options: []
|
137
131
|
require_paths:
|
138
132
|
- lib
|
139
133
|
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
-
none: false
|
141
134
|
requirements:
|
142
|
-
- -
|
135
|
+
- - '>='
|
143
136
|
- !ruby/object:Gem::Version
|
144
137
|
version: '0'
|
145
|
-
segments:
|
146
|
-
- 0
|
147
|
-
hash: 633185140317949872
|
148
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
-
none: false
|
150
139
|
requirements:
|
151
|
-
- -
|
140
|
+
- - '>='
|
152
141
|
- !ruby/object:Gem::Version
|
153
142
|
version: '0'
|
154
|
-
segments:
|
155
|
-
- 0
|
156
|
-
hash: 633185140317949872
|
157
143
|
requirements: []
|
158
144
|
rubyforge_project:
|
159
|
-
rubygems_version:
|
145
|
+
rubygems_version: 2.0.0
|
160
146
|
signing_key:
|
161
|
-
specification_version:
|
147
|
+
specification_version: 4
|
162
148
|
summary: A class based enumerator utility for Ruby on Rails
|
163
149
|
test_files:
|
164
150
|
- spec/classy_enum/active_record_spec.rb
|