classy_enum 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +10 -0
- data/VERSION +1 -1
- data/classy_enum.gemspec +6 -3
- data/lib/classy_enum/attributes.rb +6 -8
- data/lib/classy_enum/base.rb +52 -0
- data/lib/classy_enum/class_methods.rb +63 -0
- data/lib/classy_enum/instance_methods.rb +105 -0
- data/lib/classy_enum/semantic_form_builder.rb +2 -2
- data/lib/classy_enum.rb +3 -173
- data/spec/classy_enum_attributes_spec.rb +18 -42
- data/spec/classy_enum_semantic_form_builder_spec.rb +47 -49
- data/spec/classy_enum_spec.rb +22 -2
- data/spec/spec_helper.rb +6 -3
- metadata +8 -5
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -39,7 +39,14 @@ GEM
|
|
39
39
|
actionpack (>= 2.3.0)
|
40
40
|
activesupport (>= 2.3.0)
|
41
41
|
i18n (>= 0.4.0)
|
42
|
+
gemcutter (0.6.1)
|
43
|
+
git (1.2.5)
|
42
44
|
i18n (0.4.1)
|
45
|
+
jeweler (1.4.0)
|
46
|
+
gemcutter (>= 0.1.0)
|
47
|
+
git (>= 1.2.5)
|
48
|
+
rubyforge (>= 2.0.0)
|
49
|
+
json_pure (1.5.1)
|
43
50
|
linecache (0.43)
|
44
51
|
mail (2.2.7)
|
45
52
|
activesupport (>= 2.3.6)
|
@@ -83,6 +90,8 @@ GEM
|
|
83
90
|
ruby-debug-base (~> 0.10.4.0)
|
84
91
|
ruby-debug-base (0.10.4)
|
85
92
|
linecache (>= 0.3)
|
93
|
+
rubyforge (2.0.4)
|
94
|
+
json_pure (>= 1.1.7)
|
86
95
|
sqlite3-ruby (1.3.1)
|
87
96
|
thor (0.14.3)
|
88
97
|
treetop (1.4.8)
|
@@ -94,6 +103,7 @@ PLATFORMS
|
|
94
103
|
|
95
104
|
DEPENDENCIES
|
96
105
|
formtastic (~> 1.1)
|
106
|
+
jeweler (= 1.4.0)
|
97
107
|
rails (~> 3.0)
|
98
108
|
rspec (~> 2.0)
|
99
109
|
rspec-rails (~> 2.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.1
|
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.7.
|
8
|
+
s.version = "0.7.1"
|
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{2011-
|
12
|
+
s.date = %q{2011-02-06}
|
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 = [
|
@@ -31,6 +31,9 @@ Gem::Specification.new do |s|
|
|
31
31
|
"init.rb",
|
32
32
|
"lib/classy_enum.rb",
|
33
33
|
"lib/classy_enum/attributes.rb",
|
34
|
+
"lib/classy_enum/base.rb",
|
35
|
+
"lib/classy_enum/class_methods.rb",
|
36
|
+
"lib/classy_enum/instance_methods.rb",
|
34
37
|
"lib/classy_enum/semantic_form_builder.rb",
|
35
38
|
"lib/generators/classy_enum/classy_enum_generator.rb",
|
36
39
|
"lib/generators/classy_enum/templates/enum.rb",
|
@@ -42,7 +45,7 @@ Gem::Specification.new do |s|
|
|
42
45
|
s.homepage = %q{http://github.com/beerlington/classy_enum}
|
43
46
|
s.rdoc_options = ["--charset=UTF-8"]
|
44
47
|
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version = %q{1.
|
48
|
+
s.rubygems_version = %q{1.5.0}
|
46
49
|
s.summary = %q{A class based enumerator utility for Ruby on Rails}
|
47
50
|
s.test_files = [
|
48
51
|
"spec/classy_enum_attributes_spec.rb",
|
@@ -18,26 +18,24 @@ module ClassyEnum
|
|
18
18
|
# class Alarm < ActiveRecord::Base
|
19
19
|
# classy_enum_attr :priority, :alarm_priority
|
20
20
|
# end
|
21
|
-
def classy_enum_attr(
|
21
|
+
def classy_enum_attr(enum, attribute=nil)
|
22
22
|
|
23
|
-
|
23
|
+
attribute ||= enum
|
24
24
|
|
25
|
-
klass =
|
25
|
+
klass = enum.to_s.camelize.constantize
|
26
26
|
|
27
27
|
self.instance_eval do
|
28
28
|
|
29
29
|
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
|
30
|
-
|
31
|
-
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
|
32
|
-
end
|
30
|
+
validates_inclusion_of attribute, :in => klass.all, :message => "must be one of #{klass.valid_options}"
|
33
31
|
|
34
32
|
# Define getter method that returns a ClassyEnum instance
|
35
|
-
define_method
|
33
|
+
define_method attribute do
|
36
34
|
klass.build(super())
|
37
35
|
end
|
38
36
|
|
39
37
|
# Define setter method that accepts either string or symbol for member
|
40
|
-
define_method "#{
|
38
|
+
define_method "#{attribute}=" do |value|
|
41
39
|
super(value.to_s)
|
42
40
|
end
|
43
41
|
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ClassyEnum
|
2
|
+
|
3
|
+
class Base
|
4
|
+
|
5
|
+
# Macro for defining enum members within a ClassyEnum class.
|
6
|
+
# Accepts an array of symbols or strings which are converted to
|
7
|
+
# ClassyEnum members as descents of their parent class.
|
8
|
+
#
|
9
|
+
# ==== Example
|
10
|
+
# # Define an enum called Priority with three child classes
|
11
|
+
# class Priority < ClassyEnum::Base
|
12
|
+
# enum_classes :low, :medium, :high
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# The child classes will be defined with the following constants:
|
16
|
+
# PriorityLow, PriorityMedium, and PriorityHigh
|
17
|
+
#
|
18
|
+
# These child classes can be instantiated with either:
|
19
|
+
# Priority.build(:low) or PriorityLow.new
|
20
|
+
#
|
21
|
+
def self.enum_classes(*options)
|
22
|
+
self.const_set("OPTIONS", options) unless self.const_defined? "OPTIONS"
|
23
|
+
|
24
|
+
self.extend ClassyEnum::ClassMethods
|
25
|
+
self.send(:include, ClassyEnum::InstanceMethods)
|
26
|
+
self.send(:include, Comparable)
|
27
|
+
|
28
|
+
options.each_with_index do |option, index|
|
29
|
+
|
30
|
+
klass = Class.new(self) do
|
31
|
+
@index = index + 1
|
32
|
+
@option = option
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
@to_s = self.class.instance_variable_get('@option').to_s
|
36
|
+
@index = self.class.instance_variable_get('@index')
|
37
|
+
end
|
38
|
+
|
39
|
+
# Define methods to test member type (ie member.option?)
|
40
|
+
options.each do |o|
|
41
|
+
self.send(:define_method, "#{o}?", lambda { o == option })
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
klass_name = "#{self}#{option.to_s.camelize}"
|
47
|
+
Object.const_set(klass_name, klass) unless Object.const_defined? klass_name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ClassyEnum
|
2
|
+
module ClassMethods
|
3
|
+
|
4
|
+
# Build a new ClassyEnum child instance
|
5
|
+
#
|
6
|
+
# ==== Example
|
7
|
+
# # Create an Enum with some elements
|
8
|
+
# class Priority < ClassyEnum::Base
|
9
|
+
# enum_classes :low, :medium, :high
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# Priority.build(:low) # => PriorityLow.new
|
13
|
+
def build(option)
|
14
|
+
return option if option.blank?
|
15
|
+
return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? option.to_sym
|
16
|
+
Object.const_get("#{self}#{option.to_s.camelize}").new
|
17
|
+
end
|
18
|
+
|
19
|
+
alias :find :build
|
20
|
+
|
21
|
+
# Returns an array of all instantiated enums
|
22
|
+
#
|
23
|
+
# ==== Example
|
24
|
+
# # Create an Enum with some elements
|
25
|
+
# class Priority < ClassyEnum::Base
|
26
|
+
# enum_classes :low, :medium, :high
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Priority.all # => [PriorityLow.new, PriorityMedium.new, PriorityHigh.new]
|
30
|
+
def all
|
31
|
+
self::OPTIONS.map {|e| build(e) }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns a 2D array for Rails select helper options.
|
35
|
+
# Also used internally for Formtastic support
|
36
|
+
#
|
37
|
+
# ==== Example
|
38
|
+
# # Create an Enum with some elements
|
39
|
+
# class Priority < ClassyEnum::Base
|
40
|
+
# enum_classes :low, :really_high
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# Priority.select_options # => [["Low", "low"], ["Really High", "really_high"]]
|
44
|
+
def select_options
|
45
|
+
all.map {|e| [e.name, e.to_s] }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns a comma separated list of valid enum options.
|
49
|
+
# Also used internally for ActiveRecord model validation error messages
|
50
|
+
#
|
51
|
+
# ==== Example
|
52
|
+
# # Create an Enum with some elements
|
53
|
+
# class Priority < ClassyEnum::Base
|
54
|
+
# enum_classes :low, :medium, :high
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# Priority.valid_options # => "low, medium, high"
|
58
|
+
def valid_options
|
59
|
+
self::OPTIONS.map(&:to_s).join(', ')
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module ClassyEnum
|
2
|
+
module InstanceMethods
|
3
|
+
# Returns an integer representing the order that this element was defined in.
|
4
|
+
# Also used internally for sorting.
|
5
|
+
#
|
6
|
+
# ==== Example
|
7
|
+
# # Create an Enum with some elements
|
8
|
+
# class Priority < ClassyEnum::Base
|
9
|
+
# enum_classes :low, :medium, :high
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# @priority = PriorityMedium.new
|
13
|
+
# @priority.index # => 2
|
14
|
+
def index
|
15
|
+
@index
|
16
|
+
end
|
17
|
+
|
18
|
+
alias :to_i :index
|
19
|
+
|
20
|
+
# Returns the name or string corresponding to element
|
21
|
+
#
|
22
|
+
# ==== Example
|
23
|
+
# # Create an Enum with some elements
|
24
|
+
# class Priority < ClassyEnum::Base
|
25
|
+
# enum_classes :low, :medium, :high
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @priority = PriorityLow.new
|
29
|
+
# @priority.to_s # => 'low'
|
30
|
+
def to_s
|
31
|
+
@to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns a Symbol corresponding to a string representation of element,
|
35
|
+
# creating the symbol if it did not previously exist
|
36
|
+
#
|
37
|
+
# ==== Example
|
38
|
+
# # Create an Enum with some elements
|
39
|
+
# class Priority < ClassyEnum::Base
|
40
|
+
# enum_classes :low, :medium, :high
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# @priority = PriorityLow.new
|
44
|
+
# @priority.to_sym # => :low
|
45
|
+
def to_sym
|
46
|
+
@to_s.to_sym
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns string representing enum in Rails titleize format
|
50
|
+
#
|
51
|
+
# ==== Example
|
52
|
+
# # Create an Enum with some elements
|
53
|
+
# class Priority < ClassyEnum::Base
|
54
|
+
# enum_classes :low, :medium, :high, :really_high
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# @priority = Priority.build(:really_high)
|
58
|
+
# @priority.name # => "Really High"
|
59
|
+
def name
|
60
|
+
@to_s.titleize
|
61
|
+
end
|
62
|
+
|
63
|
+
# Sort an array of elements based on the order they are defined
|
64
|
+
#
|
65
|
+
# ==== Example
|
66
|
+
# # Create an Enum with some elements
|
67
|
+
# class Priority < ClassyEnum::Base
|
68
|
+
# enum_classes :low, :medium, :high
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# @low = Priority.build(:low)
|
72
|
+
# @medium = Priority.build(:medium)
|
73
|
+
# @high = Priority.build(:high)
|
74
|
+
# priorities = [@low, @high, @medium]
|
75
|
+
# priorities.sort # => [@low, @medium, @high]
|
76
|
+
# priorities.max # => @high
|
77
|
+
# priorities.min # => @low
|
78
|
+
def <=> other
|
79
|
+
@index <=> other.index
|
80
|
+
end
|
81
|
+
|
82
|
+
# Determine if the enum attribute is a particular member.
|
83
|
+
# Accepts a symbol or string representing a member
|
84
|
+
#
|
85
|
+
# ==== Example
|
86
|
+
# # Create an Enum with some elements
|
87
|
+
# class Breed < ClassyEnum::Base
|
88
|
+
# enum_classes :golden_retriever, :snoop
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# # Create an ActiveRecord class using the Breed enum
|
92
|
+
# class Dog < ActiveRecord::Base
|
93
|
+
# classy_enum_attr :breed
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# @dog = Dog.new(:breed => :snoop)
|
97
|
+
# @dog.breed.is? :snoop # => true
|
98
|
+
# @dog.breed.is? 'snoop' # => true
|
99
|
+
# @dog.breed.is? :golden_retriever # => false
|
100
|
+
def is?(obj)
|
101
|
+
obj.to_s == to_s
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -7,7 +7,7 @@ module ClassyEnum
|
|
7
7
|
enum_class = (options[:enum_class] || method).to_s.classify.constantize rescue Error.invalid_classy_enum_object(method)
|
8
8
|
options[:collection] = enum_class.select_options
|
9
9
|
else
|
10
|
-
Error.invalid_classy_enum_object unless enum_class.
|
10
|
+
Error.invalid_classy_enum_object(method) unless enum_class.is_a? ClassyEnum::Base
|
11
11
|
options[:collection] = enum_class.class.superclass.select_options
|
12
12
|
options[:selected] = enum_class.to_s
|
13
13
|
end
|
@@ -20,7 +20,7 @@ module ClassyEnum
|
|
20
20
|
|
21
21
|
module Error # :nodoc: all
|
22
22
|
def self.invalid_classy_enum_object(method)
|
23
|
-
raise "#{method} is not a ClassyEnum object"
|
23
|
+
raise "#{method} is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :#{method}' to your model"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
data/lib/classy_enum.rb
CHANGED
@@ -1,179 +1,9 @@
|
|
1
|
+
require "classy_enum/base"
|
2
|
+
require "classy_enum/class_methods"
|
3
|
+
require "classy_enum/instance_methods"
|
1
4
|
require "classy_enum/attributes"
|
2
5
|
|
3
6
|
if Gem.available? 'formtastic'
|
4
7
|
require 'formtastic'
|
5
8
|
require 'classy_enum/semantic_form_builder'
|
6
9
|
end
|
7
|
-
|
8
|
-
module ClassyEnum
|
9
|
-
|
10
|
-
class Base
|
11
|
-
|
12
|
-
# Macro for defining enum members within a ClassyEnum class.
|
13
|
-
# Accepts an array of symbols or strings which are converted to
|
14
|
-
# ClassyEnum members as descents of their parent class.
|
15
|
-
#
|
16
|
-
# ==== Example
|
17
|
-
# # Define an enum called Priority with three child classes
|
18
|
-
# class Priority < ClassyEnum::Base
|
19
|
-
# enum_classes :low, :medium, :high
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# The child classes will be defined with the following constants:
|
23
|
-
# PriorityLow, PriorityMedium, and PriorityHigh
|
24
|
-
#
|
25
|
-
# These child classes can be instantiated with either:
|
26
|
-
# Priority.build(:low) or PriorityLow.new
|
27
|
-
#
|
28
|
-
def self.enum_classes(*options)
|
29
|
-
self.send(:attr_reader, :enum_classes)
|
30
|
-
|
31
|
-
self.const_set("OPTIONS", options) unless self.const_defined? "OPTIONS"
|
32
|
-
|
33
|
-
self.extend ClassMethods
|
34
|
-
|
35
|
-
options.each_with_index do |option, index|
|
36
|
-
|
37
|
-
klass = Class.new(self) do
|
38
|
-
include InstanceMethods
|
39
|
-
|
40
|
-
attr_reader :to_s, :to_sym, :index
|
41
|
-
|
42
|
-
@index = index + 1
|
43
|
-
@option = option
|
44
|
-
|
45
|
-
def initialize
|
46
|
-
@to_s = self.class.instance_variable_get('@option').to_s
|
47
|
-
@to_sym = @to_s.to_sym
|
48
|
-
@index = self.class.instance_variable_get('@index')
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
klass_name = "#{self}#{option.to_s.camelize}"
|
54
|
-
Object.const_set(klass_name, klass) unless Object.const_defined? klass_name
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
module ClassMethods
|
60
|
-
|
61
|
-
# Build a new ClassyEnum child instance
|
62
|
-
#
|
63
|
-
# ==== Example
|
64
|
-
# # Create an Enum with some elements
|
65
|
-
# class Priority < ClassyEnum::Base
|
66
|
-
# enum_classes :low, :medium, :high
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
# Priority.build(:low) # => PriorityLow.new
|
70
|
-
def build(option)
|
71
|
-
return option if option.blank?
|
72
|
-
return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? option.to_sym
|
73
|
-
Object.const_get("#{self}#{option.to_s.camelize}").new
|
74
|
-
end
|
75
|
-
|
76
|
-
alias :find :build
|
77
|
-
|
78
|
-
# Returns an array of all instantiated enums
|
79
|
-
#
|
80
|
-
# ==== Example
|
81
|
-
# # Create an Enum with some elements
|
82
|
-
# class Priority < ClassyEnum::Base
|
83
|
-
# enum_classes :low, :medium, :high
|
84
|
-
# end
|
85
|
-
#
|
86
|
-
# Priority.all # => [PriorityLow.new, PriorityMedium.new, PriorityHigh.new]
|
87
|
-
def all
|
88
|
-
self::OPTIONS.map {|e| build(e) }
|
89
|
-
end
|
90
|
-
|
91
|
-
# Returns a 2D array for Rails select helper options.
|
92
|
-
# Also used internally for Formtastic support
|
93
|
-
#
|
94
|
-
# ==== Example
|
95
|
-
# # Create an Enum with some elements
|
96
|
-
# class Priority < ClassyEnum::Base
|
97
|
-
# enum_classes :low, :really_high
|
98
|
-
# end
|
99
|
-
#
|
100
|
-
# Priority.select_options # => [["Low", "low"], ["Really High", "really_high"]]
|
101
|
-
def select_options
|
102
|
-
all.map {|e| [e.name, e.to_s] }
|
103
|
-
end
|
104
|
-
|
105
|
-
# Returns a comma separated list of valid enum options.
|
106
|
-
# Also used internally for ActiveRecord model validation error messages
|
107
|
-
#
|
108
|
-
# ==== Example
|
109
|
-
# # Create an Enum with some elements
|
110
|
-
# class Priority < ClassyEnum::Base
|
111
|
-
# enum_classes :low, :medium, :high
|
112
|
-
# end
|
113
|
-
#
|
114
|
-
# Priority.valid_options # => "low, medium, high"
|
115
|
-
def valid_options
|
116
|
-
self::OPTIONS.map(&:to_s).join(', ')
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
module InstanceMethods
|
122
|
-
# Returns string representing enum in Rails titleize format
|
123
|
-
#
|
124
|
-
# ==== Example
|
125
|
-
# # Create an Enum with some elements
|
126
|
-
# class Priority < ClassyEnum::Base
|
127
|
-
# enum_classes :low, :medium, :high, :really_high
|
128
|
-
# end
|
129
|
-
#
|
130
|
-
# @priority = Priority.build(:really_high)
|
131
|
-
# @priority.name # => "Really High"
|
132
|
-
def name
|
133
|
-
@to_s.titleize
|
134
|
-
end
|
135
|
-
|
136
|
-
# Sort an array of elements based on the order they are defined
|
137
|
-
#
|
138
|
-
# ==== Example
|
139
|
-
# # Create an Enum with some elements
|
140
|
-
# class Priority < ClassyEnum::Base
|
141
|
-
# enum_classes :low, :medium, :high
|
142
|
-
# end
|
143
|
-
#
|
144
|
-
# @low = Priority.build(:low)
|
145
|
-
# @medium = Priority.build(:medium)
|
146
|
-
# @high = Priority.build(:high)
|
147
|
-
# priorities = [@low, @high, @medium]
|
148
|
-
# priorities.sort # => [@low, @medium, @high]
|
149
|
-
# priorities.max # => @high
|
150
|
-
# priorities.min # => @low
|
151
|
-
def <=> other
|
152
|
-
@index <=> other.index
|
153
|
-
end
|
154
|
-
|
155
|
-
# Determine if the enum attribute is a particular member.
|
156
|
-
# Accepts a symbol or string representing a member
|
157
|
-
#
|
158
|
-
# ==== Example
|
159
|
-
# # Create an Enum with some elements
|
160
|
-
# class Breed < ClassyEnum::Base
|
161
|
-
# enum_classes :golden_retriever, :snoop
|
162
|
-
# end
|
163
|
-
#
|
164
|
-
# # Create an ActiveRecord class using the Breed enum
|
165
|
-
# class Dog < ActiveRecord::Base
|
166
|
-
# classy_enum_attr :breed
|
167
|
-
# end
|
168
|
-
#
|
169
|
-
# @dog = Dog.new(:breed => :snoop)
|
170
|
-
# @dog.breed.is? :snoop # => true
|
171
|
-
# @dog.breed.is? 'snoop' # => true
|
172
|
-
# @dog.breed.is? :golden_retriever # => false
|
173
|
-
def is?(obj)
|
174
|
-
obj.to_s == to_s
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
end
|
@@ -1,50 +1,34 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe "A Dog Collection" do
|
4
|
-
before(:each) do
|
5
|
-
dog1 = Dog.new(:breed => :golden_retriever)
|
6
|
-
dog2 = Dog.new(:breed => :snoop)
|
7
|
-
|
8
|
-
@dogs = [dog1, dog2]
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should sort by breed" do
|
12
|
-
@dogs.sort_by(&:breed).should == @dogs
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
3
|
describe "A Dog" do
|
17
4
|
|
18
|
-
|
5
|
+
context "with valid breed options" do
|
6
|
+
before { @dog = Dog.new(:breed => :golden_retriever) }
|
19
7
|
|
20
|
-
|
21
|
-
|
22
|
-
|
8
|
+
it "should have a classy enum breed" do
|
9
|
+
@dog.breed.should be_a(BreedGoldenRetriever)
|
10
|
+
end
|
23
11
|
|
24
|
-
|
25
|
-
|
12
|
+
it "should be valid with a valid option" do
|
13
|
+
@dog.should be_valid
|
14
|
+
end
|
26
15
|
end
|
27
16
|
|
28
|
-
it "should
|
29
|
-
|
17
|
+
it "should not be valid with a nil breed" do
|
18
|
+
Dog.new(:breed => nil).should_not be_valid
|
30
19
|
end
|
31
20
|
|
32
|
-
it "should be valid with a
|
33
|
-
|
21
|
+
it "should not be valid with a blank breed" do
|
22
|
+
Dog.new(:breed => "").should_not be_valid
|
34
23
|
end
|
35
24
|
|
36
|
-
context "with
|
37
|
-
before { @dog.breed
|
25
|
+
context "with invalid breed options" do
|
26
|
+
before { @dog = Dog.new(:breed => :fake_breed) }
|
38
27
|
|
39
28
|
it "should not be valid with an invalid option" do
|
40
29
|
@dog.should_not be_valid
|
41
30
|
end
|
42
31
|
|
43
|
-
it "should have an error for the breed" do
|
44
|
-
@dog.valid?
|
45
|
-
@dog.errors.should include(:breed)
|
46
|
-
end
|
47
|
-
|
48
32
|
it "should have an error message containing the right options" do
|
49
33
|
@dog.valid?
|
50
34
|
@dog.errors[:breed].should include("must be one of #{Breed.all.map(&:to_sym).join(', ')}")
|
@@ -53,18 +37,10 @@ describe "A Dog" do
|
|
53
37
|
|
54
38
|
end
|
55
39
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "A Thing" do
|
61
|
-
before(:each) { @thing = Thing.new(:dog_breed => :snoop) }
|
62
|
-
|
63
|
-
it "should have an enumerable dog breed as breed" do
|
64
|
-
@thing.dog_breed.class.should == BreedSnoop
|
65
|
-
end
|
40
|
+
describe "A ClassyEnum that has a different field name than the enum" do
|
41
|
+
before { @dog = OtherDog.new(:other_breed => :snoop) }
|
66
42
|
|
67
|
-
it "should have a
|
68
|
-
@
|
43
|
+
it "should have a classy enum breed" do
|
44
|
+
@dog.other_breed.should be_a(BreedSnoop)
|
69
45
|
end
|
70
46
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe 'using enum_select input' do
|
4
4
|
include FormtasticSpecHelper
|
@@ -11,69 +11,67 @@ describe 'using enum_select input' do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context "when building a form with a classy_enum select" do
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
context "with an object that has the enum set" do
|
15
|
+
before(:each) do
|
16
|
+
@output = semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
17
|
+
concat(builder.input(:breed, :as => :enum_select))
|
18
|
+
end
|
17
19
|
end
|
18
|
-
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
it "should produce an unselected option tag for Golden Retriever" do
|
22
|
+
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
23
|
+
@output.should =~ regex
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
it "should produce a selected option tag for Snoop" do
|
27
|
+
regex = Regexp.new("<option value=\\\"snoop\\\" selected=\\\"selected\\\">Snoop")
|
28
|
+
@output.should =~ regex
|
29
|
+
end
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
it "should not produce a blank selected option tag" do
|
32
|
+
regex = Regexp.new("<option value=\"\"><")
|
33
|
+
@output.should_not =~ regex
|
34
|
+
end
|
32
35
|
end
|
33
|
-
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
context "with an object that has a nil enum" do
|
38
|
+
before(:each) do
|
39
|
+
@output = semantic_form_for(Dog.new, :url => "/") do |builder|
|
40
|
+
concat(builder.input(:breed, :as => :enum_select))
|
41
|
+
end
|
39
42
|
end
|
40
|
-
end
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
it "should produce an unselected option tag for Golden Retriever" do
|
45
|
+
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
46
|
+
@output.should =~ regex
|
47
|
+
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
it "should not produce an selected option tag" do
|
50
|
+
regex = Regexp.new("selected")
|
51
|
+
@output.should_not =~ regex
|
52
|
+
end
|
50
53
|
|
51
|
-
it "should produce an unselected option tag for Snoop" do
|
52
|
-
regex = Regexp.new("<option value=\\\"snoop\\\">Snoop")
|
53
|
-
@output.should =~ regex
|
54
54
|
end
|
55
|
-
end
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
context "with an object with a different attribute name which requires the enum_class" do
|
57
|
+
before(:each) do
|
58
|
+
@output = semantic_form_for(OtherDog.new, :url => "/") do |builder|
|
59
|
+
concat(builder.input(:other_breed, :as => :enum_select, :enum_class => :breed))
|
60
|
+
end
|
61
61
|
end
|
62
|
-
end
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
it "should produce an unselected option tag for Golden Retriever" do
|
64
|
+
regex = Regexp.new("<option value=\\\"golden_retriever\\\">Golden Retriever")
|
65
|
+
@output.should =~ regex
|
66
|
+
end
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
it "should not produce an selected option tag" do
|
69
|
+
regex = Regexp.new("selected")
|
70
|
+
@output.should_not =~ regex
|
71
|
+
end
|
72
72
|
|
73
|
-
it "should produce an unselected option tag for Snoop" do
|
74
|
-
regex = Regexp.new("<option value=\\\"snoop\\\">Snoop")
|
75
|
-
@output.should =~ regex
|
76
73
|
end
|
74
|
+
|
77
75
|
end
|
78
76
|
|
79
77
|
it "should raise an error if the attribute is not a ClassyEnum object" do
|
@@ -81,7 +79,7 @@ describe 'using enum_select input' do
|
|
81
79
|
@output = semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
82
80
|
concat(builder.input(:id, :as => :enum_select))
|
83
81
|
end
|
84
|
-
end.should raise_error("id is not a ClassyEnum object")
|
82
|
+
end.should raise_error("id is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :id' to your model")
|
85
83
|
end
|
86
84
|
|
87
85
|
it "should raise an error if the attribute is not a ClassyEnum object and its value is nil" do
|
@@ -89,7 +87,7 @@ describe 'using enum_select input' do
|
|
89
87
|
@output = semantic_form_for(Dog.new, :url => "/") do |builder|
|
90
88
|
concat(builder.input(:id, :as => :enum_select))
|
91
89
|
end
|
92
|
-
end.should raise_error("id is not a ClassyEnum object")
|
90
|
+
end.should raise_error("id is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :id' to your model")
|
93
91
|
end
|
94
92
|
|
95
93
|
end
|
data/spec/classy_enum_spec.rb
CHANGED
@@ -81,6 +81,10 @@ describe "A ClassyEnum element" do
|
|
81
81
|
it "should inherit the default class methods" do
|
82
82
|
TestEnumOne.test_class_method?.should be_false
|
83
83
|
end
|
84
|
+
|
85
|
+
it "should compare different elements based on their index" do
|
86
|
+
TestEnumOne.new.should == TestEnumOne.new
|
87
|
+
end
|
84
88
|
end
|
85
89
|
|
86
90
|
describe "A ClassyEnum instance" do
|
@@ -90,18 +94,34 @@ describe "A ClassyEnum instance" do
|
|
90
94
|
@enum.class.should == TestEnumOne
|
91
95
|
end
|
92
96
|
|
93
|
-
it "should return true for
|
97
|
+
it "should return true for is?(:one)" do
|
94
98
|
@enum.is?(:one).should be_true
|
95
99
|
end
|
96
100
|
|
97
|
-
it "should return true for
|
101
|
+
it "should return true for is?('one')" do
|
98
102
|
@enum.is?('one').should be_true
|
99
103
|
end
|
100
104
|
|
105
|
+
it "should return true for one?" do
|
106
|
+
@enum.one?.should be_true
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should return false for two?" do
|
110
|
+
@enum.two?.should be_false
|
111
|
+
end
|
112
|
+
|
101
113
|
it "should be a TestEnum" do
|
102
114
|
@enum.should be_a(TestEnum)
|
103
115
|
end
|
104
116
|
|
117
|
+
it "should have an index" do
|
118
|
+
@enum.index.should == 1
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should index as to_i" do
|
122
|
+
@enum.to_i.should == 1
|
123
|
+
end
|
124
|
+
|
105
125
|
it "should convert to a string" do
|
106
126
|
@enum.to_s.should == "one"
|
107
127
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -15,12 +15,12 @@ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":me
|
|
15
15
|
ActiveRecord::Schema.define(:version => 1) do
|
16
16
|
create_table :dogs, :force => true do |t|
|
17
17
|
t.string :breed
|
18
|
-
t.string :other_breed
|
19
18
|
end
|
20
19
|
|
21
|
-
create_table :
|
22
|
-
t.string :
|
20
|
+
create_table :other_dogs, :force => true do |t|
|
21
|
+
t.string :other_breed
|
23
22
|
end
|
23
|
+
|
24
24
|
end
|
25
25
|
|
26
26
|
class Breed < ClassyEnum::Base
|
@@ -29,6 +29,9 @@ end
|
|
29
29
|
|
30
30
|
class Dog < ActiveRecord::Base
|
31
31
|
classy_enum_attr :breed
|
32
|
+
end
|
33
|
+
|
34
|
+
class OtherDog < ActiveRecord::Base
|
32
35
|
classy_enum_attr :breed, :other_breed
|
33
36
|
end
|
34
37
|
|
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: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 1
|
10
|
+
version: 0.7.1
|
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: 2011-
|
18
|
+
date: 2011-02-06 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -116,6 +116,9 @@ files:
|
|
116
116
|
- init.rb
|
117
117
|
- lib/classy_enum.rb
|
118
118
|
- lib/classy_enum/attributes.rb
|
119
|
+
- lib/classy_enum/base.rb
|
120
|
+
- lib/classy_enum/class_methods.rb
|
121
|
+
- lib/classy_enum/instance_methods.rb
|
119
122
|
- lib/classy_enum/semantic_form_builder.rb
|
120
123
|
- lib/generators/classy_enum/classy_enum_generator.rb
|
121
124
|
- lib/generators/classy_enum/templates/enum.rb
|
@@ -153,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
156
|
requirements: []
|
154
157
|
|
155
158
|
rubyforge_project:
|
156
|
-
rubygems_version: 1.
|
159
|
+
rubygems_version: 1.5.0
|
157
160
|
signing_key:
|
158
161
|
specification_version: 3
|
159
162
|
summary: A class based enumerator utility for Ruby on Rails
|