Module: ClassyEnum::ClassMethods
- Included in:
- Base
- Defined in:
- lib/classy_enum/class_methods.rb
Instance Method Summary collapse
-
#all ⇒ Object
Returns an array of all instantiated enums.
-
#build(value, options = {}) ⇒ Object
Build a new ClassyEnum child instance.
-
#enum_classes(*enums) ⇒ Object
Macro for defining enum members within a ClassyEnum class.
- #find(value, options = {}) ⇒ Object
- #inherited(klass) ⇒ Object
-
#select_options ⇒ Object
Returns a 2D array for Rails select helper options.
-
#valid_options ⇒ Object
Returns a comma separated list of valid enum options.
Instance Method Details
#all ⇒ Object
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]
87 88 89 |
# File 'lib/classy_enum/class_methods.rb', line 87 def all self..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
63 64 65 66 67 68 69 70 71 |
# File 'lib/classy_enum/class_methods.rb', line 63 def build(value, ={}) return value if value.blank? return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self..include? value.to_sym object = ("#{self}#{value.to_s.camelize}").constantize.new object.owner = [:owner] object.serialize_as_json = [: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. = 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
73 74 75 76 |
# File 'lib/classy_enum/class_methods.rb', line 73 def find(value, ={}) ActiveSupport::Deprecation.warn("find is deprecated, and will be removed in ClassyEnum 3.0. Use build(:member) instead.", caller) build(value, ) end |
#inherited(klass) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym index = self..index(enum) + 1 klass.class_eval do @index = index @option = enum attr_accessor :owner, :serialize_as_json end end |
#select_options ⇒ Object
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. # => [["Low", "low"], ["Really High", "really_high"]]
101 102 103 |
# File 'lib/classy_enum/class_methods.rb', line 101 def all.map {|e| [e.name, e.to_s] } end |
#valid_options ⇒ Object
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. # => "low, medium, high"
115 116 117 118 |
# File 'lib/classy_enum/class_methods.rb', line 115 def ActiveSupport::Deprecation.warn("valid_options is deprecated, and will be removed in ClassyEnum 3.0. Use all.join(', ') instead.", caller) self..map(&:to_s).join(', ') end |