Skip to content

Commit

Permalink
Removing enum_classes DSL method
Browse files Browse the repository at this point in the history
This is no longer necessary now that all classes use explicit
inheritance from either ClassyEnum::Base or its subclasses
  • Loading branch information
beerlington committed Jul 13, 2012
1 parent 6a8f4d8 commit 3a491f2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 57 deletions.
64 changes: 22 additions & 42 deletions lib/classy_enum/class_methods.rb
@@ -1,53 +1,34 @@
module ClassyEnum
module ClassMethods
def inherited(klass)
if self == ClassyEnum::Base
klass.class_eval do
class_attribute :enum_options, :base_class

# Macro for defining enum members within a ClassyEnum class.
# Accepts an array of symbols or strings which are converted to
# ClassyEnum members as descents of their parent class.
#
# ==== Example
# # Define an enum called Priority with three child classes
# class Priority < ClassyEnum::Base
# enum_classes :low, :medium, :high
# end
#
# The child classes will be defined with the following constants:
# PriorityLow, PriorityMedium, and PriorityHigh
#
# These child classes can be instantiated with either:
# Priority.build(:low) or PriorityLow.new
#
def enum_classes(*enums)
ActiveSupport::Deprecation.warn('enum_classes is deprecated, and will be removed in ClassyEnum 3.0. It is no longer needed.', caller)

self.class_eval do
class_attribute :enum_options, :base_class
self.enum_options = []
self.base_class = klass

self.enum_options = enums.map(&:to_sym)
self.base_class = self
attribute_method_suffix '?'
end
else

# # Use ActiveModel::AttributeMethods to define attribute? methods
attribute_method_suffix '?'
define_attribute_methods enums
end
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) }
end

def inherited(klass)
return if self == ClassyEnum::Base

# 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) }
end
# Convert from MyEnumClassNumberTwo to :number_two
enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym

enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym
index = self.enum_options.index(enum) + 1
enum_options << enum
define_attribute_method enum

klass.class_eval do
@index = index
@option = enum
klass.class_eval do
@index = enum_options.size
@option = enum

attr_accessor :owner, :serialize_as_json
attr_accessor :owner, :serialize_as_json
end
end
end

Expand Down Expand Up @@ -113,7 +94,6 @@ def select_options
#
# Priority.valid_options # => "low, medium, high"
def valid_options
ActiveSupport::Deprecation.warn("valid_options is deprecated, and will be removed in ClassyEnum 3.0. Use all.join(', ') instead.", caller)
self.enum_options.map(&:to_s).join(', ')
end

Expand Down
1 change: 0 additions & 1 deletion spec/active_record_spec.rb
@@ -1,7 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class Color < ClassyEnum::Base
enum_classes :white, :black
end

class ColorWhite < Color; end;
Expand Down
1 change: 0 additions & 1 deletion spec/classy_enum_owner_reference_spec.rb
@@ -1,7 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class CatBreed < ClassyEnum::Base
enum_classes :abyssian, :bengal, :birman, :persian
owner :cat

def breed_color
Expand Down
16 changes: 4 additions & 12 deletions spec/classy_enum_spec.rb
@@ -1,8 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class TestEnum < ClassyEnum::Base
enum_classes :one, :two, :three

def self.test_class_method?
false
end
Expand Down Expand Up @@ -66,17 +64,11 @@ class TestEnumThree < TestEnum
end

describe "A ClassyEnum element" do
it "should instantiate a member" do
TestEnumOne.new.should be_a(TestEnumOne)
end
subject { TestEnumOne }

it "should inherit the default class methods" do
TestEnumOne.test_class_method?.should be_false
end

it "should compare different elements based on their index" do
TestEnumOne.new.should == TestEnumOne.new
end
its(:new) { should be_a(TestEnumOne) }
its(:new) { should == TestEnumOne.new }
it { should_not be_a_test_class_method }
end

describe "A ClassyEnum instance" do
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -48,7 +48,6 @@
end

class Breed < ClassyEnum::Base
enum_classes :golden_retriever, :snoop, :husky
end

class BreedGoldenRetriever < Breed
Expand Down

0 comments on commit 3a491f2

Please sign in to comment.