Module: ClassyEnum::ClassMethods

Included in:
Base
Defined in:
lib/classy_enum/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#allObject

Returns an array of all instantiated enums

Example

# Create an Enum with some elements
class Priority < ClassyEnum::Base
  enum_classes :low, :medium, :high
end

Priority.all # => [PriorityLow.new, PriorityMedium.new, PriorityHigh.new]


100
101
102
# File 'lib/classy_enum/class_methods.rb', line 100

def all
  self.enum_options.map {|e| build(e) }
end

#build(value, options = {}) ⇒ Object

Build a new ClassyEnum child instance

Example

# Create an Enum with some elements
class Priority < ClassyEnum::Base
  enum_classes :low, :medium, :high
end

Priority.build(:low) # => PriorityLow.new


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/classy_enum/class_methods.rb', line 69

def build(value, options={})
  return value if value.blank?
  return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self.enum_options.include? value.to_sym

  # Temp hack until 3.0 to allow both namespaced and non-namespaced classes
  begin
    klass = "#{self}::#{value.to_s.camelize}".constantize
  rescue NameError
    klass = "#{self}#{value.to_s.camelize}".constantize
  end

  object = klass.new
  object.owner = options[:owner]
  object.serialize_as_json = options[:serialize_as_json]
  object
end

#enum_classes(*enums) ⇒ Object

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/classy_enum/class_methods.rb', line 20

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 = enums.map(&:to_sym)
    self.base_class = self

    # # Use ActiveModel::AttributeMethods to define attribute? methods
    attribute_method_suffix '?'
    define_attribute_methods enums
  end
end

#find(value, options = {}) ⇒ Object



86
87
88
89
# File 'lib/classy_enum/class_methods.rb', line 86

def find(value, options={})
  ActiveSupport::Deprecation.warn("find is deprecated, and will be removed in ClassyEnum 3.0. Use build(:member) instead.", caller)
  build(value, options)
end

#inherited(klass) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/classy_enum/class_methods.rb', line 35

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

  if klass.name.start_with? "#{base_class.name}::"
    enum = klass.name.split('::').last.underscore.to_sym
  else
    ActiveSupport::Deprecation.warn("The enum class name #{klass} is deprecated. ClassyEnum 3.0 will require subclasses to use the parent class as a namespace. Change it to #{base_class}::#{klass.name.gsub(base_class.name, '')}", caller)
    enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym
  end

  index = self.enum_options.index(enum) + 1

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

    attr_accessor :owner, :serialize_as_json
  end
end

#select_optionsObject

Returns a 2D array for Rails select helper options. Also used internally for Formtastic support

Example

# Create an Enum with some elements
class Priority < ClassyEnum::Base
  enum_classes :low, :really_high
end

Priority.select_options # => [["Low", "low"], ["Really High", "really_high"]]


114
115
116
# File 'lib/classy_enum/class_methods.rb', line 114

def select_options
  all.map {|e| [e.name, e.to_s] }
end

#valid_optionsObject

Returns a comma separated list of valid enum options. Also used internally for ActiveRecord model validation error messages

Example

# Create an Enum with some elements
class Priority < ClassyEnum::Base
  enum_classes :low, :medium, :high
end

Priority.valid_options # => "low, medium, high"


128
129
130
131
# File 'lib/classy_enum/class_methods.rb', line 128

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