classy_enum 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +34 -2
- data/VERSION +1 -1
- data/classy_enum.gemspec +2 -2
- data/generators/templates/enum.erb +3 -3
- data/lib/classy_enum.rb +5 -3
- data/spec/classy_enum_spec.rb +26 -8
- metadata +4 -4
data/README.textile
CHANGED
@@ -16,14 +16,46 @@ h2. Example Usage
|
|
16
16
|
|
17
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
18
|
|
19
|
+
The fastest way to get up and running with ClassyEnum is to use the built-in Rails generator like so:
|
20
|
+
|
21
|
+
@script/generate classy_enum AlarmPriority low medium high@
|
22
|
+
|
23
|
+
A new file will be created at app/enums/alarm_priority.rb that will look like:
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
module AlarmPriority
|
27
|
+
|
28
|
+
OPTIONS = [:low, :medium, :high]
|
29
|
+
|
30
|
+
module Defaults
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
include ClassyEnum
|
35
|
+
end
|
36
|
+
|
37
|
+
class AlarmPriorityLow
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class AlarmPriorityMedium
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class AlarmPriorityHigh
|
46
|
+
|
47
|
+
end
|
48
|
+
</pre>
|
49
|
+
|
50
|
+
That is the default setup, but can be changed to fit your needs, like so...
|
51
|
+
|
19
52
|
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
53
|
|
21
54
|
*It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*
|
22
55
|
|
23
|
-
Create a file in app/models called alarm_priority.rb
|
24
|
-
|
25
56
|
<pre>
|
26
57
|
module AlarmPriority
|
58
|
+
|
27
59
|
OPTIONS = [:low, :medium, :high]
|
28
60
|
|
29
61
|
module Defaults
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/classy_enum.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{classy_enum}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
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-
|
12
|
+
s.date = %q{2010-10-01}
|
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 = [
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module <%= class_name %>
|
2
|
-
|
3
2
|
OPTIONS = [<%= args.map {|a| ":#{a}"}.join(", ") %>]
|
4
3
|
|
5
|
-
module
|
4
|
+
module InstanceMethods
|
5
|
+
end
|
6
6
|
|
7
|
+
module ClassMethods
|
7
8
|
end
|
8
9
|
|
9
10
|
include ClassyEnum
|
10
11
|
end
|
11
12
|
<% args.each do |arg| %>
|
12
13
|
class <%= class_name + arg.camelize %>
|
13
|
-
|
14
14
|
end
|
15
15
|
<% end %>
|
data/lib/classy_enum.rb
CHANGED
@@ -53,9 +53,11 @@ module ClassyEnum
|
|
53
53
|
other.const_set("OPTION_HASH", Hash.new)
|
54
54
|
|
55
55
|
other::OPTIONS.each do |option|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
|
57
|
+
klass = Class.new(ClassyEnumValue) do
|
58
|
+
include other::InstanceMethods if other.const_defined?("InstanceMethods")
|
59
|
+
extend other::ClassMethods if other.const_defined?("ClassMethods")
|
60
|
+
end
|
59
61
|
|
60
62
|
Object.const_set("#{other}#{option.to_s.camelize}", klass)
|
61
63
|
|
data/spec/classy_enum_spec.rb
CHANGED
@@ -3,8 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
module TestEnum
|
4
4
|
OPTIONS = [:one, :two, :three]
|
5
5
|
|
6
|
-
module
|
7
|
-
def
|
6
|
+
module InstanceMethods
|
7
|
+
def test_instance_method?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def test_class_method?
|
8
14
|
false
|
9
15
|
end
|
10
16
|
end
|
@@ -13,7 +19,11 @@ module TestEnum
|
|
13
19
|
end
|
14
20
|
|
15
21
|
class TestEnumTwo
|
16
|
-
def
|
22
|
+
def self.test_class_method?
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_instance_method?
|
17
27
|
true
|
18
28
|
end
|
19
29
|
end
|
@@ -73,8 +83,12 @@ describe "An ClassyEnumValue" do
|
|
73
83
|
@enum.name.should == "One"
|
74
84
|
end
|
75
85
|
|
76
|
-
it "should inherit the
|
77
|
-
@enum.
|
86
|
+
it "should inherit the default instance methods" do
|
87
|
+
@enum.test_instance_method?.should be_false
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should inherit the default class methods" do
|
91
|
+
TestEnumOne.test_class_method?.should be_false
|
78
92
|
end
|
79
93
|
|
80
94
|
it "should create the same instance with a string or symbol" do
|
@@ -87,7 +101,11 @@ end
|
|
87
101
|
describe "An ClassyEnumValue" do
|
88
102
|
before(:each) { @enum = TestEnum.new(:two) }
|
89
103
|
|
90
|
-
it "should override the
|
91
|
-
@enum.
|
104
|
+
it "should override the default instance methods" do
|
105
|
+
@enum.test_instance_method?.should be_true
|
92
106
|
end
|
93
|
-
|
107
|
+
|
108
|
+
it "should override the default class methods" do
|
109
|
+
TestEnumTwo.test_class_method?.should be_true
|
110
|
+
end
|
111
|
+
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
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-
|
18
|
+
date: 2010-10-01 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|