classy_enum 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.textile +77 -0
- data/VERSION +1 -1
- data/classy_enum.gemspec +4 -4
- data/lib/classy_enum.rb +1 -1
- metadata +6 -6
- data/README.rdoc +0 -7
data/LICENSE
CHANGED
data/README.textile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
h1. classy_enum
|
2
|
+
|
3
|
+
ClassyEnum adds class-based enumerator functionality to your ActiveRecord model's attributes.
|
4
|
+
|
5
|
+
h2. Installation
|
6
|
+
|
7
|
+
ClassyEnum has only been tested with Rails 2.3.8, but should work with any version of Rails 2.3.x. The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum
|
8
|
+
|
9
|
+
It can be installed with:
|
10
|
+
|
11
|
+
<pre>
|
12
|
+
gem install classy_enum
|
13
|
+
</pre>
|
14
|
+
|
15
|
+
h2. Example Usage
|
16
|
+
|
17
|
+
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 ActiveRecord 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.
|
18
|
+
|
19
|
+
Using the OPTIONS constant, I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it. In my example, each enum value has a method called @email?@. By default this method returns false, but is overridden for high priority alarms and returns true.
|
20
|
+
|
21
|
+
*It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*
|
22
|
+
|
23
|
+
Create a file in app/models called alarm_priority.rb
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
module AlarmPriority
|
27
|
+
OPTIONS = [:low, :medium, :high]
|
28
|
+
|
29
|
+
module Defaults
|
30
|
+
def email?
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
include ClassyEnum
|
36
|
+
end
|
37
|
+
|
38
|
+
class AlarmPriorityHigh
|
39
|
+
def email?
|
40
|
+
true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
</pre>
|
44
|
+
|
45
|
+
Then in my ActiveRecord model, Alarm, I've added a line that calls @classy_enum_attr@. The first argument is required, and is the name of the module defined above. The second argument is optional and specifies which Alarm attribute will be used as an enumerable.
|
46
|
+
|
47
|
+
In this case, I am using the module AlarmPriority, but the name of my attribute is priority. By default, it will use the name of module as the attribute name. If I wanted to do @alarm.alarm_priority@, I would not have included the second argument.
|
48
|
+
|
49
|
+
<pre>
|
50
|
+
class Alarm < ActiveRecord::Base
|
51
|
+
classy_enum_attr :alarm_priority, :priority
|
52
|
+
|
53
|
+
delegate :email?, :to => :priority
|
54
|
+
end
|
55
|
+
</pre>
|
56
|
+
|
57
|
+
With this setup, I can now do the following:
|
58
|
+
|
59
|
+
<pre>
|
60
|
+
@alarm = Alarm.create(:priority => :medium)
|
61
|
+
|
62
|
+
@alarm.priority => AlarmPriorityMedium
|
63
|
+
|
64
|
+
@alarm.email? => false
|
65
|
+
|
66
|
+
@alarm.update_attribute(:priority, :high)
|
67
|
+
|
68
|
+
@alarm.email? => true
|
69
|
+
</pre>
|
70
|
+
|
71
|
+
h2. Notes
|
72
|
+
|
73
|
+
An ActiveRecord validator @validates_inclusion_of :field, :in => ENUM.all@ is automatically added to your model when you use @classy_enum_attr@.
|
74
|
+
|
75
|
+
h2. Copyright
|
76
|
+
|
77
|
+
Copyright (c) 2010 Peter Brown. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/classy_enum.gemspec
CHANGED
@@ -5,22 +5,22 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{classy_enum}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter Brown"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-27}
|
13
13
|
s.description = %q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
|
14
14
|
s.email = %q{github@lette.us}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
22
|
"LICENSE",
|
23
|
-
"README.
|
23
|
+
"README.textile",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"classy_enum.gemspec",
|
data/lib/classy_enum.rb
CHANGED
@@ -61,7 +61,7 @@ module ClassyEnum
|
|
61
61
|
|
62
62
|
other::OPTION_HASH[option] = other::OPTION_HASH[option.to_s.downcase] = instance
|
63
63
|
|
64
|
-
ClassyEnum.const_set(option.to_s.upcase, instance)
|
64
|
+
ClassyEnum.const_set(option.to_s.upcase, instance) unless ClassyEnum.const_defined?(option.to_s.upcase)
|
65
65
|
end
|
66
66
|
|
67
67
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Peter Brown
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-27 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -57,12 +57,12 @@ extensions: []
|
|
57
57
|
|
58
58
|
extra_rdoc_files:
|
59
59
|
- LICENSE
|
60
|
-
- README.
|
60
|
+
- README.textile
|
61
61
|
files:
|
62
62
|
- .document
|
63
63
|
- .gitignore
|
64
64
|
- LICENSE
|
65
|
-
- README.
|
65
|
+
- README.textile
|
66
66
|
- Rakefile
|
67
67
|
- VERSION
|
68
68
|
- classy_enum.gemspec
|