Browse files

Changing ClassyEnum subclass name requirements

Instead of:

ParentChild < Parent
end

Use:

Parent::Child < Parent
end
  • Loading branch information...
1 parent e8111c0 commit 253d925655085cada52c15564a32d3a398d3cb81 @beerlington beerlington committed Jul 15, 2012
View
26 README.md
@@ -47,13 +47,13 @@ A new enum template file will be created at app/enums/priority.rb that will look
class Priority < ClassyEnum::Base
end
-class PriorityLow < Priority
+class Priority::Low < Priority
end
-class PriorityMedium < Priority
+class Priority::Medium < Priority
end
-class PriorityHigh < Priority
+class Priority::High < Priority
end
```
@@ -74,13 +74,13 @@ class Priority < ClassyEnum::Base
end
end
-class PriorityLow < Priority
+class Priority::Low < Priority
end
-class PriorityMedium < Priority
+class Priority::Medium < Priority
end
-class PriorityHigh < Priority
+class Priority::High < Priority
def send_email?
true
end
@@ -116,7 +116,7 @@ With this setup, I can now do the following:
```ruby
@alarm = Alarm.create(:priority => :medium)
-@alarm.priority # => PriorityMedium
+@alarm.priority # => Priority::Medium
@alarm.priority.medium? # => true
@alarm.priority.high? # => false
@alarm.priority.to_s # => 'medium'
@@ -150,7 +150,7 @@ end
# low and medium subclasses omitted
# ...
-class PriorityHigh < Priority
+class Priority::High < Priority
def send_email?
owner.enabled?
end
@@ -168,7 +168,7 @@ end
# low and medium subclasses omitted
# ...
-class PriorityHigh < Priority
+class Priority::High < Priority
def send_email?
alarm.enabled?
end
@@ -216,7 +216,7 @@ class Alarm < ActiveRecord::Base
end
@alarm = Alarm.create(:alarm_priority => :medium)
-@alarm.alarm_priority # => PriorityMedium
+@alarm.alarm_priority # => Priority::Medium
```
If you would like the default getter method to return a string, you can
@@ -229,7 +229,7 @@ end
alarm = Alarm.create(:priority => :high)
alarm.priority # => 'high'
-alarm.priority_type # instance of PriorityHigh enum
+alarm.priority_type # instance of Priority::High enum
```
## Model Validation
@@ -260,13 +260,13 @@ end
While ClassyEnum was designed to be used directly with ActiveRecord, it can also be used outside of it. Here are some examples based on the enum class defined earlier in this document.
-Instantiate an enum member subclass *PriorityLow*
+Instantiate an enum member subclass *Priority::Low*
```ruby
# These statements are all equivalent
low = Priority.build(:low)
low = Priority.build('low')
-low = PriorityLow.new
+low = Priority::Low.new
```
Get a list of the valid enum options
View
12 lib/classy_enum/base.rb
@@ -17,17 +17,17 @@ def inherited(klass)
else
# Ensure subclasses follow expected naming conventions
- unless klass.name.start_with? base_class.name
- raise SubclassNameError, "subclass name must start with #{base_class.name}"
+ unless klass.name.start_with? "#{base_class.name}::"
+ raise SubclassNameError, "subclass must be namespaced with #{base_class.name}::"
end
# Add visit_EnumMember methods to support validates_uniqueness_of with enum field
Arel::Visitors::ToSql.class_eval do
- define_method "visit_#{klass.name}", lambda {|value| quote(value.to_s) }
+ define_method "visit_#{klass.name.split('::').join('_')}", lambda {|value| quote(value.to_s) }
end
- # Convert from MyEnumClassNumberTwo to :number_two
- enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym
+ # Convert from MyEnumClass::NumberTwo to :number_two
+ enum = klass.name.split('::').last.underscore.to_sym
Predicate.define_predicate_method(klass, enum)
@@ -56,7 +56,7 @@ def build(value, options={})
return TypeError.new("#{base_class} #{invalid_message}")
end
- object = ("#{base_class}#{value.to_s.camelize}").constantize.new
+ object = ("#{base_class}::#{value.to_s.camelize}").constantize.new
object.owner = options[:owner]
object.serialize_as_json = options[:serialize_as_json]
object
View
4 spec/active_record_spec.rb
@@ -3,9 +3,9 @@
class Color < ClassyEnum::Base
end
-class ColorWhite < Color; end;
+class Color::White < Color; end;
-class ColorBlack < Color; end;
+class Color::Black < Color; end;
class ActiveDog < ActiveRecord::Base
classy_enum_attr :breed, :suffix => 'type'
View
4 spec/classy_enum/attributes_spec.rb
@@ -6,7 +6,7 @@
subject { Dog.new(:breed => :golden_retriever) }
it { should be_valid }
- its(:breed) { should be_a(BreedGoldenRetriever) }
+ its(:breed) { should be_a(Breed::GoldenRetriever) }
its(:breed_options) { should eql({:enum => :breed, :allow_blank => false}) }
end
@@ -93,5 +93,5 @@
describe "A ClassyEnum that has a different field name than the enum" do
subject { OtherDog.new(:other_breed => :snoop) }
- its(:other_breed) { should be_a(BreedSnoop) }
+ its(:other_breed) { should be_a(Breed::Snoop) }
end
View
28 spec/classy_enum/base_spec.rb
@@ -1,53 +1,53 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-class TestEnum < ClassyEnum::Base
+class ClassyEnumBase < ClassyEnum::Base
end
-class TestEnumOne < TestEnum
+class ClassyEnumBase::One < ClassyEnumBase
end
-class TestEnumTwo < TestEnum
+class ClassyEnumBase::Two < ClassyEnumBase
end
describe ClassyEnum::Base do
context '.build' do
context 'invalid option' do
- subject { TestEnum.build(:invalid_option) }
+ subject { ClassyEnumBase.build(:invalid_option) }
its(:class) { should == TypeError }
end
context 'string option' do
- subject { TestEnum.build("one") }
- it { should be_a(TestEnumOne) }
+ subject { ClassyEnumBase.build("one") }
+ it { should be_a(ClassyEnumBase::One) }
end
context 'symbol option' do
- subject { TestEnum.build(:two) }
- it { should be_a(TestEnumTwo) }
+ subject { ClassyEnumBase.build(:two) }
+ it { should be_a(ClassyEnumBase::Two) }
end
end
context '.invalid_message' do
- TestEnum.invalid_message.should == 'must be one of one, two'
+ ClassyEnumBase.invalid_message.should == 'must be one of one, two'
end
context '#new' do
- subject { TestEnumOne }
- its(:new) { should be_a(TestEnumOne) }
- its(:new) { should == TestEnumOne.new }
+ subject { ClassyEnumBase::One }
+ its(:new) { should be_a(ClassyEnumBase::One) }
+ its(:new) { should == ClassyEnumBase::One.new }
end
context 'Subclass naming' do
it 'should raise an error when invalid' do
lambda {
- class WrongSublcassName < TestEnum; end
+ class WrongSublcassName < ClassyEnumBase; end
}.should raise_error(ClassyEnum::SubclassNameError)
end
end
end
describe ClassyEnum::Base, 'Arel visitor' do
specify do
- Arel::Visitors::ToSql.instance_methods.map(&:to_sym).should include(:visit_TestEnumOne, :visit_TestEnumTwo)
+ Arel::Visitors::ToSql.instance_methods.map(&:to_sym).should include(:'visit_ClassyEnumBase_One', :'visit_ClassyEnumBase_Two')
end
end
View
16 spec/classy_enum/collection_spec.rb
@@ -3,27 +3,27 @@
class ClassyEnumCollection < ClassyEnum::Base
end
-class ClassyEnumCollectionOne < ClassyEnumCollection
+class ClassyEnumCollection::One < ClassyEnumCollection
end
-class ClassyEnumCollectionTwo < ClassyEnumCollection
+class ClassyEnumCollection::Two < ClassyEnumCollection
end
-class ClassyEnumCollectionThree < ClassyEnumCollection
+class ClassyEnumCollection::Three < ClassyEnumCollection
end
describe ClassyEnum::Collection do
subject { ClassyEnumCollection }
- its(:enum_options) { should == [ClassyEnumCollectionOne, ClassyEnumCollectionTwo, ClassyEnumCollectionThree] }
- its(:all) { should == [ClassyEnumCollectionOne.new, ClassyEnumCollectionTwo.new, ClassyEnumCollectionThree.new] }
+ its(:enum_options) { should == [ClassyEnumCollection::One, ClassyEnumCollection::Two, ClassyEnumCollection::Three] }
+ its(:all) { should == [ClassyEnumCollection::One.new, ClassyEnumCollection::Two.new, ClassyEnumCollection::Three.new] }
its(:select_options) { should == [['One', 'one'],['Two', 'two'], ['Three', 'three']] }
end
describe ClassyEnum::Collection, Comparable do
- let(:one) { ClassyEnumCollectionOne.new }
- let(:two) { ClassyEnumCollectionTwo.new }
- let(:three) { ClassyEnumCollectionThree.new }
+ let(:one) { ClassyEnumCollection::One.new }
+ let(:two) { ClassyEnumCollection::Two.new }
+ let(:three) { ClassyEnumCollection::Three.new }
subject { [one, three, two] }
its(:sort) { should eql([one, two, three]) }
View
28 spec/classy_enum/conversion_spec.rb
@@ -3,49 +3,49 @@
class ClassyEnumConversion < ClassyEnum::Base
end
-class ClassyEnumConversionOne < ClassyEnumConversion
+class ClassyEnumConversion::One < ClassyEnumConversion
end
-class ClassyEnumConversionTwo < ClassyEnumConversion
+class ClassyEnumConversion::Two < ClassyEnumConversion
end
describe ClassyEnum::Conversion do
context '#to_i' do
- specify { ClassyEnumConversionOne.new.to_i.should == 1 }
- specify { ClassyEnumConversionTwo.new.to_i.should == 2 }
+ specify { ClassyEnumConversion::One.new.to_i.should == 1 }
+ specify { ClassyEnumConversion::Two.new.to_i.should == 2 }
end
context '#index' do
- specify { ClassyEnumConversionOne.new.index.should == 1 }
- specify { ClassyEnumConversionTwo.new.index.should == 2 }
+ specify { ClassyEnumConversion::One.new.index.should == 1 }
+ specify { ClassyEnumConversion::Two.new.index.should == 2 }
end
context '#to_s' do
- specify { ClassyEnumConversionOne.new.to_s.should == 'one' }
- specify { ClassyEnumConversionTwo.new.to_s.should == 'two' }
+ specify { ClassyEnumConversion::One.new.to_s.should == 'one' }
+ specify { ClassyEnumConversion::Two.new.to_s.should == 'two' }
end
context '#to_sym' do
- specify { ClassyEnumConversionOne.new.to_sym.should == :one }
- specify { ClassyEnumConversionTwo.new.to_sym.should == :two }
+ specify { ClassyEnumConversion::One.new.to_sym.should == :one }
+ specify { ClassyEnumConversion::Two.new.to_sym.should == :two }
end
context '#as_json' do
context 'serialize_as_json is false' do
- specify { ClassyEnumConversionOne.new.as_json.should == 'one' }
- specify { ClassyEnumConversionTwo.new.as_json.should == 'two' }
+ specify { ClassyEnumConversion::One.new.as_json.should == 'one' }
+ specify { ClassyEnumConversion::Two.new.as_json.should == 'two' }
end
context 'serialize_as_json is true' do
specify do
- enum = ClassyEnumConversionOne.new
+ enum = ClassyEnumConversion::One.new
enum.serialize_as_json = true
enum.instance_variable_set('@key', 'value')
enum.as_json.should == {'key' => 'value' }
end
specify do
- enum = ClassyEnumConversionOne.new
+ enum = ClassyEnumConversion::One.new
enum.serialize_as_json = true
enum.instance_variable_set('@key', 'value')
enum.as_json.should == {'key' => 'value' }
View
12 spec/classy_enum/predicate_spec.rb
@@ -3,17 +3,17 @@
class ClassyEnumPredicate < ClassyEnum::Base
end
-class ClassyEnumPredicateOne < ClassyEnumPredicate
+class ClassyEnumPredicate::One < ClassyEnumPredicate
end
-class ClassyEnumPredicateTwo < ClassyEnumPredicate
+class ClassyEnumPredicate::Two < ClassyEnumPredicate
end
describe ClassyEnum::Predicate do
context '#attribute?' do
- specify { ClassyEnumPredicateOne.new.should be_one }
- specify { ClassyEnumPredicateOne.new.should_not be_two }
- specify { ClassyEnumPredicateTwo.new.should_not be_one }
- specify { ClassyEnumPredicateTwo.new.should be_two }
+ specify { ClassyEnumPredicate::One.new.should be_one }
+ specify { ClassyEnumPredicate::One.new.should_not be_two }
+ specify { ClassyEnumPredicate::Two.new.should_not be_one }
+ specify { ClassyEnumPredicate::Two.new.should be_two }
end
end
View
12 spec/classy_enum_inheritance_spec.rb
@@ -4,25 +4,25 @@ class ProjectTier < ClassyEnum::Base
class_attribute :inherited_properties
end
-class ProjectTierOne < ProjectTier
+class ProjectTier::One < ProjectTier
self.inherited_properties = [1,2,3]
end
-class ProjectTierTwo < ProjectTierOne
+class ProjectTier::Two < ProjectTier::One
self.inherited_properties += [4,5,6]
end
describe 'Classy Enum inheritance' do
it 'should inherit from the previous class' do
- ProjectTierOne.inherited_properties.should == [1,2,3]
- ProjectTierTwo.inherited_properties.should == [1,2,3,4,5,6]
+ ProjectTier::One.inherited_properties.should == [1,2,3]
+ ProjectTier::Two.inherited_properties.should == [1,2,3,4,5,6]
end
it 'should instantiate the subclass' do
- ProjectTierTwo.build(:two).should == ProjectTierTwo.new
+ ProjectTier::Two.build(:two).should == ProjectTier::Two.new
end
it 'should have the right index' do
- ProjectTierTwo.new.index.should == 2
+ ProjectTier::Two.new.index.should == 2
end
end
View
8 spec/classy_enum_owner_reference_spec.rb
@@ -8,16 +8,16 @@ def breed_color
end
end
-class CatBreedAbyssian < CatBreed
+class CatBreed::Abyssian < CatBreed
end
-class CatBreedBengal < CatBreed
+class CatBreed::Bengal < CatBreed
end
-class CatBreedBirman < CatBreed
+class CatBreed::Birman < CatBreed
end
-class CatBreedPersian < CatBreed
+class CatBreed::Persian < CatBreed
end
class Cat < ActiveRecord::Base
View
6 spec/spec_helper.rb
@@ -50,13 +50,13 @@
class Breed < ClassyEnum::Base
end
-class BreedGoldenRetriever < Breed
+class Breed::GoldenRetriever < Breed
end
-class BreedSnoop < Breed
+class Breed::Snoop < Breed
end
-class BreedHusky < Breed
+class Breed::Husky < Breed
end
class Dog < ActiveRecord::Base

0 comments on commit 253d925

Please sign in to comment.