classy_enum 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +8 -0
- data/README.textile +18 -30
- data/VERSION +1 -1
- data/classy_enum.gemspec +6 -6
- data/generators/classy_enum_generator.rb +1 -1
- data/generators/templates/enum.rb +7 -0
- data/lib/classy_enum.rb +48 -54
- data/lib/classy_enum/attributes.rb +7 -5
- data/lib/classy_enum/semantic_form_builder.rb +7 -7
- data/lib/generators/classy_enum/classy_enum_generator.rb +1 -1
- data/lib/generators/classy_enum/templates/enum.rb +7 -0
- data/spec/classy_enum_attributes_spec.rb +25 -3
- data/spec/classy_enum_semantic_form_builder_spec.rb +74 -0
- data/spec/classy_enum_spec.rb +31 -25
- data/spec/spec_helper.rb +2 -4
- metadata +15 -7
- data/generators/templates/enum.erb +0 -15
- data/lib/generators/classy_enum/templates/enum.erb +0 -15
- data/spec/classy_enum_semantic_form_builder.rb +0 -32
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -31,6 +31,7 @@ GEM
|
|
31
31
|
arel (1.0.1)
|
32
32
|
activesupport (~> 3.0.0)
|
33
33
|
builder (2.1.2)
|
34
|
+
columnize (0.3.2)
|
34
35
|
diff-lcs (1.1.2)
|
35
36
|
erubis (2.6.6)
|
36
37
|
abstract (>= 1.0.0)
|
@@ -39,6 +40,7 @@ GEM
|
|
39
40
|
activesupport (>= 2.3.0)
|
40
41
|
i18n (>= 0.4.0)
|
41
42
|
i18n (0.4.1)
|
43
|
+
linecache (0.43)
|
42
44
|
mail (2.2.7)
|
43
45
|
activesupport (>= 2.3.6)
|
44
46
|
mime-types
|
@@ -76,6 +78,11 @@ GEM
|
|
76
78
|
rspec-expectations (= 2.0.0)
|
77
79
|
rspec-rails (2.0.1)
|
78
80
|
rspec (~> 2.0.0)
|
81
|
+
ruby-debug (0.10.4)
|
82
|
+
columnize (>= 0.1)
|
83
|
+
ruby-debug-base (~> 0.10.4.0)
|
84
|
+
ruby-debug-base (0.10.4)
|
85
|
+
linecache (>= 0.3)
|
79
86
|
sqlite3-ruby (1.3.1)
|
80
87
|
thor (0.14.3)
|
81
88
|
treetop (1.4.8)
|
@@ -90,4 +97,5 @@ DEPENDENCIES
|
|
90
97
|
rails (~> 3.0)
|
91
98
|
rspec (~> 2.0)
|
92
99
|
rspec-rails (~> 2.0)
|
100
|
+
ruby-debug
|
93
101
|
sqlite3-ruby
|
data/README.textile
CHANGED
@@ -10,13 +10,7 @@ h2. Requirements
|
|
10
10
|
|
11
11
|
h2. Installation
|
12
12
|
|
13
|
-
The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum
|
14
|
-
|
15
|
-
It can be installed with:
|
16
|
-
|
17
|
-
<pre>
|
18
|
-
gem install classy_enum
|
19
|
-
</pre>
|
13
|
+
The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum and can be installed with: @gem install classy_enum@
|
20
14
|
|
21
15
|
h2. Example Usage
|
22
16
|
|
@@ -24,23 +18,23 @@ The most common use for ClassyEnum is to replace database lookup tables where th
|
|
24
18
|
|
25
19
|
The fastest way to get up and running with ClassyEnum is to use the built-in Rails generator like so:
|
26
20
|
|
21
|
+
Rails 2.3.x:
|
22
|
+
|
27
23
|
<pre>
|
28
24
|
script/generate classy_enum AlarmPriority low medium high
|
29
25
|
</pre>
|
30
26
|
|
31
|
-
|
27
|
+
Rails 3.x
|
32
28
|
|
33
29
|
<pre>
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
module InstanceMethods
|
38
|
-
end
|
30
|
+
rails g classy_enum AlarmPriority low medium high
|
31
|
+
</pre>
|
39
32
|
|
40
|
-
|
41
|
-
end
|
33
|
+
A new file will be created at app/enums/alarm_priority.rb that will look like:
|
42
34
|
|
43
|
-
|
35
|
+
<pre>
|
36
|
+
class AlarmPriority < ClassyEnum::Base
|
37
|
+
enum_classes :low, :medium, :high
|
44
38
|
end
|
45
39
|
|
46
40
|
class AlarmPriorityLow
|
@@ -55,21 +49,15 @@ end
|
|
55
49
|
|
56
50
|
That is the default setup, but can be changed to fit your needs, like so...
|
57
51
|
|
58
|
-
Using the
|
59
|
-
|
60
|
-
*It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*
|
52
|
+
Using the @enum_classes@ method, 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 class has a method called @email?@. By default this method returns false, but is overridden for high priority alarms and returns true.
|
61
53
|
|
62
54
|
<pre>
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
false
|
69
|
-
end
|
55
|
+
class AlarmPriority < ClassyEnum::Base
|
56
|
+
enum_classes :low, :medium, :high
|
57
|
+
|
58
|
+
def email?
|
59
|
+
false
|
70
60
|
end
|
71
|
-
|
72
|
-
include ClassyEnum
|
73
61
|
end
|
74
62
|
|
75
63
|
class AlarmPriorityHigh
|
@@ -79,9 +67,9 @@ class AlarmPriorityHigh
|
|
79
67
|
end
|
80
68
|
</pre>
|
81
69
|
|
82
|
-
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
|
70
|
+
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 class defined above. The second argument is optional and specifies which Alarm attribute will be used as an enumerable.
|
83
71
|
|
84
|
-
In this case, I am using the
|
72
|
+
In this case, I am using the class AlarmPriority, but the name of my attribute is priority. By default, it will use the name of class as the attribute name. If I wanted to do @alarm.alarm_priority@, I would not have included the second argument.
|
85
73
|
|
86
74
|
<pre>
|
87
75
|
class Alarm < ActiveRecord::Base
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.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.6.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-11-
|
12
|
+
s.date = %q{2010-11-13}
|
13
13
|
s.description = %q{A utility that adds class based enum functionality to ActiveRecord attributes}
|
14
14
|
s.email = %q{github@lette.us}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,15 +27,15 @@ Gem::Specification.new do |s|
|
|
27
27
|
"VERSION",
|
28
28
|
"classy_enum.gemspec",
|
29
29
|
"generators/classy_enum_generator.rb",
|
30
|
-
"generators/templates/enum.
|
30
|
+
"generators/templates/enum.rb",
|
31
31
|
"init.rb",
|
32
32
|
"lib/classy_enum.rb",
|
33
33
|
"lib/classy_enum/attributes.rb",
|
34
34
|
"lib/classy_enum/semantic_form_builder.rb",
|
35
35
|
"lib/generators/classy_enum/classy_enum_generator.rb",
|
36
|
-
"lib/generators/classy_enum/templates/enum.
|
36
|
+
"lib/generators/classy_enum/templates/enum.rb",
|
37
37
|
"spec/classy_enum_attributes_spec.rb",
|
38
|
-
"spec/
|
38
|
+
"spec/classy_enum_semantic_form_builder_spec.rb",
|
39
39
|
"spec/classy_enum_spec.rb",
|
40
40
|
"spec/spec_helper.rb"
|
41
41
|
]
|
@@ -46,7 +46,7 @@ Gem::Specification.new do |s|
|
|
46
46
|
s.summary = %q{A class based enumerator utility for Ruby on Rails}
|
47
47
|
s.test_files = [
|
48
48
|
"spec/classy_enum_attributes_spec.rb",
|
49
|
-
"spec/
|
49
|
+
"spec/classy_enum_semantic_form_builder_spec.rb",
|
50
50
|
"spec/classy_enum_spec.rb",
|
51
51
|
"spec/spec_helper.rb"
|
52
52
|
]
|
data/lib/classy_enum.rb
CHANGED
@@ -6,74 +6,68 @@ if Gem.available? 'formtastic'
|
|
6
6
|
end
|
7
7
|
|
8
8
|
module ClassyEnum
|
9
|
-
|
10
|
-
|
9
|
+
|
10
|
+
class Base
|
11
|
+
def self.enum_classes(*options)
|
12
|
+
self.send(:attr_reader, :enum_classes)
|
13
|
+
|
14
|
+
self.const_set("OPTIONS", options) unless self.const_defined? "OPTIONS"
|
15
|
+
|
16
|
+
self.extend ClassMethods
|
17
|
+
|
18
|
+
options.each_with_index do |option, index|
|
19
|
+
|
20
|
+
klass = Class.new(self) do
|
21
|
+
attr_reader :to_s, :to_sym, :index
|
22
|
+
|
23
|
+
@index = index + 1
|
24
|
+
@option = option
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@to_s = self.class.instance_variable_get('@option').to_s
|
28
|
+
@to_sym = @to_s.to_sym
|
29
|
+
@index = self.class.instance_variable_get('@index')
|
30
|
+
end
|
31
|
+
|
32
|
+
def name
|
33
|
+
@to_s.titleize
|
34
|
+
end
|
35
|
+
|
36
|
+
def <=> other
|
37
|
+
@index <=> other.index
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
klass_name = "#{self}#{option.to_s.camelize}"
|
42
|
+
Object.const_set(klass_name, klass) unless Object.const_defined? klass_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module ClassMethods
|
11
48
|
|
12
|
-
def
|
49
|
+
def build(option)
|
13
50
|
return nil if option.nil?
|
14
|
-
|
51
|
+
return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? option.to_sym
|
52
|
+
Object.const_get("#{self}#{option.to_s.camelize}").new
|
15
53
|
end
|
16
54
|
|
55
|
+
# Alias of build
|
56
|
+
def find(option); build(option); end;
|
57
|
+
|
17
58
|
def all
|
18
|
-
self::OPTIONS.map {|e|
|
59
|
+
self::OPTIONS.map {|e| build(e) }
|
19
60
|
end
|
20
61
|
|
21
62
|
# Uses the name field for select options
|
22
|
-
def
|
23
|
-
|
63
|
+
def select_options
|
64
|
+
all.map {|e| [e.name, e.to_s] }
|
24
65
|
end
|
25
66
|
|
26
67
|
def valid_options
|
27
68
|
self::OPTIONS.map(&:to_s).join(', ')
|
28
69
|
end
|
29
|
-
|
30
|
-
# Alias of new
|
31
|
-
def find(option)
|
32
|
-
new(option)
|
33
|
-
end
|
34
70
|
|
35
71
|
end
|
36
|
-
|
37
|
-
def self.included(other)
|
38
|
-
other.extend SuperClassMethods
|
39
|
-
|
40
|
-
other.const_set("OPTION_HASH", Hash.new) unless other.const_defined? "OPTION_HASH"
|
41
|
-
|
42
|
-
other::OPTIONS.each do |option|
|
43
|
-
|
44
|
-
klass = Class.new do
|
45
|
-
self.send(:attr_reader, :to_s, :to_sym, :index, :base_class)
|
46
|
-
|
47
|
-
def initialize(base_class, option, index)
|
48
|
-
@to_s = option.to_s.downcase
|
49
|
-
@to_sym = @to_s.to_sym
|
50
|
-
@index = index + 1
|
51
|
-
@base_class = base_class
|
52
|
-
end
|
53
|
-
|
54
|
-
def name
|
55
|
-
to_s.titleize
|
56
|
-
end
|
57
|
-
|
58
|
-
def <=> other
|
59
|
-
@index <=> other.index
|
60
|
-
end
|
61
|
-
|
62
|
-
include other::InstanceMethods if other.const_defined?("InstanceMethods")
|
63
|
-
extend other::ClassMethods if other.const_defined?("ClassMethods")
|
64
|
-
end
|
65
|
-
|
66
|
-
Object.const_set("#{other}#{option.to_s.camelize}", klass)
|
67
|
-
|
68
|
-
instance = klass.new(other, option, other::OPTIONS.index(option))
|
69
|
-
|
70
|
-
other::OPTION_HASH[option] = instance
|
71
|
-
|
72
|
-
ClassyEnum.const_set(option.to_s.upcase, instance) unless ClassyEnum.const_defined?(option.to_s.upcase)
|
73
|
-
end
|
74
72
|
|
75
|
-
end
|
76
|
-
|
77
73
|
end
|
78
|
-
|
79
|
-
|
@@ -7,14 +7,16 @@ module ClassyEnum
|
|
7
7
|
|
8
8
|
klass = klass.to_s.camelize.constantize
|
9
9
|
|
10
|
-
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
|
11
|
-
self.send(:validates_inclusion_of, method, :in => klass.all, :allow_nil => true)
|
12
|
-
|
13
10
|
self.instance_eval do
|
14
11
|
|
12
|
+
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
|
13
|
+
validates_each [method], :allow_nil => true do |record, attr_name, value|
|
14
|
+
record.errors.add(attr_name, "must be one of #{klass.all.map(&:to_sym).join(', ')}") unless klass.all.map(&:to_s).include? value.to_s
|
15
|
+
end
|
16
|
+
|
15
17
|
# Define getter method
|
16
18
|
define_method method do
|
17
|
-
klass.
|
19
|
+
klass.build(super())
|
18
20
|
end
|
19
21
|
|
20
22
|
# Define setter method
|
@@ -29,4 +31,4 @@ module ClassyEnum
|
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
ActiveRecord::Base.send :extend, ClassyEnum::Attributes
|
34
|
+
ActiveRecord::Base.send :extend, ClassyEnum::Attributes
|
@@ -2,13 +2,13 @@ module ClassyEnum
|
|
2
2
|
class SemanticFormBuilder < Formtastic::SemanticFormBuilder
|
3
3
|
def enum_select_input(method, options)
|
4
4
|
enum_class = object.send(method)
|
5
|
-
|
5
|
+
|
6
6
|
if enum_class.nil?
|
7
|
-
enum_class = method.to_s.capitalize.constantize rescue
|
8
|
-
options[:collection] = enum_class.
|
7
|
+
enum_class = method.to_s.capitalize.constantize rescue Error.invalid_classy_enum_object(method)
|
8
|
+
options[:collection] = enum_class.select_options
|
9
9
|
else
|
10
|
-
|
11
|
-
options[:collection] = enum_class.
|
10
|
+
Error.invalid_classy_enum_object unless enum_class.respond_to? :enum_classes
|
11
|
+
options[:collection] = enum_class.class.superclass.select_options
|
12
12
|
options[:selected] = enum_class.to_s
|
13
13
|
end
|
14
14
|
|
@@ -19,8 +19,8 @@ module ClassyEnum
|
|
19
19
|
end
|
20
20
|
|
21
21
|
module Error
|
22
|
-
def invalid_classy_enum_object
|
23
|
-
raise "#{method}
|
22
|
+
def self.invalid_classy_enum_object(method)
|
23
|
+
raise "#{method} is not a ClassyEnum object"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -21,8 +21,30 @@ describe "A Dog" do
|
|
21
21
|
@dog.breed.class.should == BreedGoldenRetriever
|
22
22
|
end
|
23
23
|
|
24
|
-
it "should
|
25
|
-
@dog.breed.
|
24
|
+
it "should respond to enum_classes" do
|
25
|
+
@dog.breed.should respond_to('enum_classes')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be valid with a valid option" do
|
29
|
+
@dog.should be_valid
|
30
|
+
end
|
31
|
+
|
32
|
+
context "with an invalid breed option" do
|
33
|
+
before { @dog.breed = :golden_doodle }
|
34
|
+
|
35
|
+
it "should not be valid with an invalid option" do
|
36
|
+
@dog.should_not be_valid
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have an error for the breed" do
|
40
|
+
@dog.valid?
|
41
|
+
@dog.errors.should include(:breed)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should have an error message containing the right options" do
|
45
|
+
@dog.valid?
|
46
|
+
@dog.errors[:breed].should include("must be one of #{Breed.all.map(&:to_sym).join(', ')}")
|
47
|
+
end
|
26
48
|
end
|
27
49
|
|
28
50
|
end
|
@@ -39,6 +61,6 @@ describe "A Thing" do
|
|
39
61
|
end
|
40
62
|
|
41
63
|
it "should have a base class of Breed" do
|
42
|
-
@thing.dog_breed.
|
64
|
+
@thing.dog_breed.should respond_to('enum_classes')
|
43
65
|
end
|
44
66
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
describe 'using enum_select input' do
|
4
|
+
include FormtasticSpecHelper
|
5
|
+
|
6
|
+
Formtastic::SemanticFormHelper.builder = ClassyEnum::SemanticFormBuilder
|
7
|
+
|
8
|
+
# Copied from how formtastic tests its form helpers
|
9
|
+
before do
|
10
|
+
@output_buffer = ""
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when building a form with a classy_enum select" do
|
14
|
+
before(:each) do
|
15
|
+
@output = semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
16
|
+
concat(builder.input(:breed, :as => :enum_select))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should produce a form tag" do
|
21
|
+
@output.should =~ /<form/
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should produce an unselected option tag for Golden Retriever" do
|
25
|
+
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
26
|
+
@output.should =~ regex
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should produce an selected option tag for Snoop" do
|
30
|
+
regex = Regexp.new("<option value=\\\"snoop\\\" selected=\\\"selected\\\">Snoop")
|
31
|
+
@output.should =~ regex
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when building a form with a classy_enum select, but the existing value is nil" do
|
36
|
+
before(:each) do
|
37
|
+
@output = semantic_form_for(Dog.new, :url => "/") do |builder|
|
38
|
+
concat(builder.input(:breed, :as => :enum_select))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should produce a form tag" do
|
43
|
+
@output.should =~ /<form/
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should produce an unselected option tag for Golden Retriever" do
|
47
|
+
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
48
|
+
@output.should =~ regex
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should produce an unselected option tag for Snoop" do
|
52
|
+
regex = Regexp.new("<option value=\\\"snoop\\\">Snoop")
|
53
|
+
@output.should =~ regex
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should raise an error if the attribute is not a ClassyEnum object" do
|
58
|
+
lambda do
|
59
|
+
@output = semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
60
|
+
concat(builder.input(:id, :as => :enum_select))
|
61
|
+
end
|
62
|
+
end.should raise_error("id is not a ClassyEnum object")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should raise an error if the attribute is not a ClassyEnum object and its value is nil" do
|
66
|
+
lambda do
|
67
|
+
@output = semantic_form_for(Dog.new, :url => "/") do |builder|
|
68
|
+
concat(builder.input(:id, :as => :enum_select))
|
69
|
+
end
|
70
|
+
end.should raise_error("id is not a ClassyEnum object")
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
data/spec/classy_enum_spec.rb
CHANGED
@@ -1,21 +1,15 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
class TestEnum < ClassyEnum::Base
|
4
|
+
enum_classes :one, :two, :three
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
false
|
9
|
-
end
|
6
|
+
def self.test_class_method?
|
7
|
+
false
|
10
8
|
end
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
false
|
15
|
-
end
|
10
|
+
def test_instance_method?
|
11
|
+
false
|
16
12
|
end
|
17
|
-
|
18
|
-
include ClassyEnum
|
19
13
|
end
|
20
14
|
|
21
15
|
class TestEnumTwo
|
@@ -28,7 +22,7 @@ class TestEnumTwo
|
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
31
|
-
describe
|
25
|
+
describe TestEnum do
|
32
26
|
|
33
27
|
TestEnum::OPTIONS.each do |option|
|
34
28
|
it "should define a TestEnum#{option.to_s.capitalize} class" do
|
@@ -37,22 +31,22 @@ describe ClassyEnum do
|
|
37
31
|
end
|
38
32
|
|
39
33
|
it "should return an array of enums" do
|
40
|
-
TestEnum.all.should ==
|
34
|
+
TestEnum.all.map(&:class).should == [TestEnumOne, TestEnumTwo, TestEnumThree]
|
41
35
|
end
|
42
36
|
|
43
37
|
it "should return an array of enums for a select tag" do
|
44
|
-
TestEnum.
|
38
|
+
TestEnum.select_options.should == TestEnum::OPTIONS.map {|o| [TestEnum.build(o).name, TestEnum.build(o).to_s] }
|
45
39
|
end
|
46
40
|
|
47
41
|
it "should return a type error when adding an invalid option" do
|
48
|
-
TestEnum.
|
42
|
+
TestEnum.build(:invalid_option).class.should == TypeError
|
49
43
|
end
|
50
44
|
|
51
45
|
context "with a collection of enums" do
|
52
46
|
before(:each) do
|
53
|
-
@one = TestEnum.
|
54
|
-
@two = TestEnum.
|
55
|
-
@three = TestEnum.
|
47
|
+
@one = TestEnum.build(:one)
|
48
|
+
@two = TestEnum.build(:two)
|
49
|
+
@three = TestEnum.build(:three)
|
56
50
|
|
57
51
|
@unordered = [@one, @three, @two]
|
58
52
|
end
|
@@ -73,12 +67,24 @@ describe ClassyEnum do
|
|
73
67
|
it "should find an enum by string" do
|
74
68
|
TestEnum.find("one").class.should == TestEnumOne
|
75
69
|
end
|
76
|
-
|
70
|
+
|
77
71
|
end
|
78
72
|
|
79
|
-
describe "
|
80
|
-
before
|
73
|
+
describe "A ClassyEnum instance" do
|
74
|
+
before { @enum = TestEnum.build(:one) }
|
81
75
|
|
76
|
+
it "should build a TestEnum class" do
|
77
|
+
@enum.class.should == TestEnumOne
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should instantiate a member" do
|
81
|
+
TestEnumOne.new.should be_a(TestEnumOne)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should be a TestEnum" do
|
85
|
+
@enum.should be_a(TestEnum)
|
86
|
+
end
|
87
|
+
|
82
88
|
it "should convert to a string" do
|
83
89
|
@enum.to_s.should == "one"
|
84
90
|
end
|
@@ -100,14 +106,14 @@ describe "An ClassyEnumValue" do
|
|
100
106
|
end
|
101
107
|
|
102
108
|
it "should create the same instance with a string or symbol" do
|
103
|
-
@enum_string = TestEnum.
|
109
|
+
@enum_string = TestEnum.build("one")
|
104
110
|
|
105
|
-
@enum.should == @enum_string
|
111
|
+
@enum.class.should == @enum_string.class
|
106
112
|
end
|
107
113
|
end
|
108
114
|
|
109
115
|
describe "An ClassyEnumValue" do
|
110
|
-
before(:each) { @enum = TestEnum.
|
116
|
+
before(:each) { @enum = TestEnum.build(:two) }
|
111
117
|
|
112
118
|
it "should override the default instance methods" do
|
113
119
|
@enum.test_instance_method?.should be_true
|
data/spec/spec_helper.rb
CHANGED
@@ -22,10 +22,8 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
include ClassyEnum
|
25
|
+
class Breed < ClassyEnum::Base
|
26
|
+
enum_classes :golden_retriever, :snoop
|
29
27
|
end
|
30
28
|
|
31
29
|
class Dog < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 7
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 6
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.6.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Peter Brown
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-13 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
28
30
|
segments:
|
29
31
|
- 2
|
30
32
|
- 0
|
@@ -39,6 +41,7 @@ dependencies:
|
|
39
41
|
requirements:
|
40
42
|
- - ~>
|
41
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
42
45
|
segments:
|
43
46
|
- 2
|
44
47
|
- 0
|
@@ -53,6 +56,7 @@ dependencies:
|
|
53
56
|
requirements:
|
54
57
|
- - ~>
|
55
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 13
|
56
60
|
segments:
|
57
61
|
- 1
|
58
62
|
- 1
|
@@ -67,6 +71,7 @@ dependencies:
|
|
67
71
|
requirements:
|
68
72
|
- - ">="
|
69
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
70
75
|
segments:
|
71
76
|
- 0
|
72
77
|
version: "0"
|
@@ -80,6 +85,7 @@ dependencies:
|
|
80
85
|
requirements:
|
81
86
|
- - ">="
|
82
87
|
- !ruby/object:Gem::Version
|
88
|
+
hash: 5
|
83
89
|
segments:
|
84
90
|
- 2
|
85
91
|
- 3
|
@@ -106,15 +112,15 @@ files:
|
|
106
112
|
- VERSION
|
107
113
|
- classy_enum.gemspec
|
108
114
|
- generators/classy_enum_generator.rb
|
109
|
-
- generators/templates/enum.
|
115
|
+
- generators/templates/enum.rb
|
110
116
|
- init.rb
|
111
117
|
- lib/classy_enum.rb
|
112
118
|
- lib/classy_enum/attributes.rb
|
113
119
|
- lib/classy_enum/semantic_form_builder.rb
|
114
120
|
- lib/generators/classy_enum/classy_enum_generator.rb
|
115
|
-
- lib/generators/classy_enum/templates/enum.
|
121
|
+
- lib/generators/classy_enum/templates/enum.rb
|
116
122
|
- spec/classy_enum_attributes_spec.rb
|
117
|
-
- spec/
|
123
|
+
- spec/classy_enum_semantic_form_builder_spec.rb
|
118
124
|
- spec/classy_enum_spec.rb
|
119
125
|
- spec/spec_helper.rb
|
120
126
|
has_rdoc: true
|
@@ -131,6 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
137
|
requirements:
|
132
138
|
- - ">="
|
133
139
|
- !ruby/object:Gem::Version
|
140
|
+
hash: 3
|
134
141
|
segments:
|
135
142
|
- 0
|
136
143
|
version: "0"
|
@@ -139,6 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
146
|
requirements:
|
140
147
|
- - ">="
|
141
148
|
- !ruby/object:Gem::Version
|
149
|
+
hash: 3
|
142
150
|
segments:
|
143
151
|
- 0
|
144
152
|
version: "0"
|
@@ -151,6 +159,6 @@ specification_version: 3
|
|
151
159
|
summary: A class based enumerator utility for Ruby on Rails
|
152
160
|
test_files:
|
153
161
|
- spec/classy_enum_attributes_spec.rb
|
154
|
-
- spec/
|
162
|
+
- spec/classy_enum_semantic_form_builder_spec.rb
|
155
163
|
- spec/classy_enum_spec.rb
|
156
164
|
- spec/spec_helper.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module <%= class_name %>
|
2
|
-
OPTIONS = [<%= args.map {|a| ":#{a}"}.join(", ") %>]
|
3
|
-
|
4
|
-
module InstanceMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
include ClassyEnum
|
11
|
-
end
|
12
|
-
<% args.each do |arg| %>
|
13
|
-
class <%= class_name + arg.camelize %>
|
14
|
-
end
|
15
|
-
<% end %>
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module <%= class_name %>
|
2
|
-
OPTIONS = [<%= values.map {|a| ":#{a}"}.join(", ") %>]
|
3
|
-
|
4
|
-
module InstanceMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
include ClassyEnum
|
11
|
-
end
|
12
|
-
<% values.each do |arg| %>
|
13
|
-
class <%= class_name + arg.camelize %>
|
14
|
-
end
|
15
|
-
<% end %>
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
describe 'using enum_select input' do
|
4
|
-
include FormtasticSpecHelper
|
5
|
-
|
6
|
-
Formtastic::SemanticFormHelper.builder = ClassyEnum::SemanticFormBuilder
|
7
|
-
|
8
|
-
context "when building a form with a classy_enum select" do
|
9
|
-
before(:each) do
|
10
|
-
@output_buffer = ""
|
11
|
-
@output = semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
12
|
-
concat(builder.input(:breed, :as => :enum_select))
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should produce a form tag" do
|
17
|
-
@output.should =~ /<form/
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should produce an unselected option tag for Golden Retriever" do
|
21
|
-
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
22
|
-
@output.should =~ regex
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should produce an selected option tag for Snoop" do
|
26
|
-
regex = Regexp.new("<option value=\\\"snoop\\\" selected=\\\"selected\\\">Snoop")
|
27
|
-
@output.should =~ regex
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|