classy_enum 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/classy_enum.gemspec +8 -3
- data/generators/classy_enum_generator.rb +10 -0
- data/generators/templates/enum.erb +15 -0
- data/lib/classy_enum.rb +2 -34
- data/lib/classy_enum/classy_enum_attributes.rb +30 -0
- data/spec/classy_enum_attributes_spec.rb +49 -0
- metadata +8 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.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.0
|
8
|
+
s.version = "0.1.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-09-
|
12
|
+
s.date = %q{2010-09-30}
|
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 = [
|
@@ -24,8 +24,12 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"classy_enum.gemspec",
|
27
|
+
"generators/classy_enum_generator.rb",
|
28
|
+
"generators/templates/enum.erb",
|
27
29
|
"init.rb",
|
28
30
|
"lib/classy_enum.rb",
|
31
|
+
"lib/classy_enum/classy_enum_attributes.rb",
|
32
|
+
"spec/classy_enum_attributes_spec.rb",
|
29
33
|
"spec/classy_enum_spec.rb",
|
30
34
|
"spec/spec.opts",
|
31
35
|
"spec/spec_helper.rb"
|
@@ -36,7 +40,8 @@ Gem::Specification.new do |s|
|
|
36
40
|
s.rubygems_version = %q{1.3.7}
|
37
41
|
s.summary = %q{A class based enumerator utility for Ruby on Rails}
|
38
42
|
s.test_files = [
|
39
|
-
"spec/
|
43
|
+
"spec/classy_enum_attributes_spec.rb",
|
44
|
+
"spec/classy_enum_spec.rb",
|
40
45
|
"spec/spec_helper.rb"
|
41
46
|
]
|
42
47
|
|
data/lib/classy_enum.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "classy_enum/classy_enum_attributes"
|
2
|
+
|
1
3
|
class ClassyEnumValue < Object
|
2
4
|
|
3
5
|
attr_reader :to_s, :index
|
@@ -68,38 +70,4 @@ module ClassyEnum
|
|
68
70
|
|
69
71
|
end
|
70
72
|
|
71
|
-
module ClassyEnumAttributes
|
72
|
-
module ClassMethods
|
73
|
-
def classy_enum_attr(klass, method=nil)
|
74
|
-
|
75
|
-
method ||= klass
|
76
|
-
|
77
|
-
klass = klass.to_s.camelize.constantize
|
78
|
-
|
79
|
-
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
|
80
|
-
self.send(:validates_inclusion_of, method, :in => klass.all)
|
81
|
-
|
82
|
-
self.instance_eval do
|
83
|
-
|
84
|
-
# Define getter method
|
85
|
-
define_method method do
|
86
|
-
klass.new(super)
|
87
|
-
end
|
88
|
-
|
89
|
-
# Define setter method
|
90
|
-
define_method "#{method}=" do |value|
|
91
|
-
super value.to_s
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def self.included(other)
|
100
|
-
other.extend ClassMethods
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
73
|
|
105
|
-
ActiveRecord::Base.send :include, ClassyEnumAttributes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ClassyEnumAttributes
|
2
|
+
|
3
|
+
def classy_enum_attr(klass, method=nil)
|
4
|
+
|
5
|
+
method ||= klass
|
6
|
+
|
7
|
+
klass = klass.to_s.camelize.constantize
|
8
|
+
|
9
|
+
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
|
10
|
+
self.send(:validates_inclusion_of, method, :in => klass.all)
|
11
|
+
|
12
|
+
self.instance_eval do
|
13
|
+
|
14
|
+
# Define getter method
|
15
|
+
define_method method do
|
16
|
+
klass.new(super)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Define setter method
|
20
|
+
define_method "#{method}=" do |value|
|
21
|
+
super value.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Base.send :extend, ClassyEnumAttributes
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define(:version => 1) do
|
6
|
+
create_table :dogs, :force => true do |t|
|
7
|
+
t.string :breed
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :things, :force => true do |t|
|
11
|
+
t.string :dog_breed
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Breed
|
16
|
+
OPTIONS = [:golden_retriever, :snoop]
|
17
|
+
|
18
|
+
module Defaults
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
include ClassyEnum
|
23
|
+
end
|
24
|
+
|
25
|
+
class Dog < ActiveRecord::Base
|
26
|
+
classy_enum_attr :breed
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "A Dog" do
|
30
|
+
|
31
|
+
before(:each) { @dog = Dog.new(:breed => :golden_retriever) }
|
32
|
+
|
33
|
+
it "should have an enumerable breed" do
|
34
|
+
@dog.breed.class.should == BreedGoldenRetriever
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
class Thing < ActiveRecord::Base
|
40
|
+
classy_enum_attr :breed, :dog_breed
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "A Thing" do
|
44
|
+
before(:each) { @thing = Thing.new(:dog_breed => :snoop) }
|
45
|
+
|
46
|
+
it "should have an enumerable dog breed as breed" do
|
47
|
+
@thing.dog_breed.class.should == BreedSnoop
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.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-09-
|
18
|
+
date: 2010-09-30 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -66,8 +66,12 @@ files:
|
|
66
66
|
- Rakefile
|
67
67
|
- VERSION
|
68
68
|
- classy_enum.gemspec
|
69
|
+
- generators/classy_enum_generator.rb
|
70
|
+
- generators/templates/enum.erb
|
69
71
|
- init.rb
|
70
72
|
- lib/classy_enum.rb
|
73
|
+
- lib/classy_enum/classy_enum_attributes.rb
|
74
|
+
- spec/classy_enum_attributes_spec.rb
|
71
75
|
- spec/classy_enum_spec.rb
|
72
76
|
- spec/spec.opts
|
73
77
|
- spec/spec_helper.rb
|
@@ -106,5 +110,6 @@ signing_key:
|
|
106
110
|
specification_version: 3
|
107
111
|
summary: A class based enumerator utility for Ruby on Rails
|
108
112
|
test_files:
|
113
|
+
- spec/classy_enum_attributes_spec.rb
|
109
114
|
- spec/classy_enum_spec.rb
|
110
115
|
- spec/spec_helper.rb
|